PowerShell Workflow is one heavy duty feature which comes bundled with PowerShell v3, From my personal experiences i feel that its best used when you need to sequence you tasks, the best example being to run your tasks in parallel.Here’s a an example of how you can use a powershell workflow to automate VM provisioning using Hyper-V cmdlets and Failover Cluster Cmdlets using differential disks.
To start off i need a csv file which contains the list of VM’s which i need to be created along with their specs (VMName,Memory,VMPath,Hyper-V Host Name, Virtual Switch Name).
So here you can see that im creating 9 VM’s with above specs.
Also note in PSv3 i don’t need to import my modules anymore, powershell does it by itself and all cmdlets are exposed.
workflow Create-VMS { # Import the list of VM's from csv file $vminfo = Import-Csv -path C:Virtualmachines.csv # Using Foreach loop in parallel to start each thread in parallel Foreach -parallel ($vm in $vminfo) { $VMName = $vm.VMName [int64]$Memory = $vm.Memory $Vmpath = $vm.VMpath $HyperV_Host = $vm.'HyperV_host' $VMSwitchName = $vm.VMSwitchName # Create VHD's based on Differencing disk New-VHD -Path "$Vmpath$VMName$VMName.vhdx" -ParentPath "C:ClusterStorageVolume1win2012-fixed-diff.vhdx" -Differencing # Create VM based on parameters obtained from csv file New-VM -Path "$Vmpath$VMName" -Name $VMName -VHDPath "$Vmpath$VMName$VMName.vhdx" -MemoryStartupBytes ([int64]$memory*1024*1024*1024) -ComputerName $HyperV_Host # Connecting the Virtual Switch to the VM Connect-VMNetworkAdapter -VMName $VMName -SwitchName $VMSwitchName -ComputerName $HyperV_Host # Make the VM highly available by adding it as clustered resource Add-ClusterVirtualMachineRole -VirtualMachine $VMName # Power On the VM Start-VM $VMName -ComputerName $HyperV_Host } }
Below you can see that i invoked this workflow in my Powershell ISE and it invokes the tasks in parallel.
Create-VMS
And here’s the end result, VM’s showing up in Hyper-V Manager and also made Highly Available.
Hope this post helps you out in your Datacenter Automation.
Hyper-V Manager