Anyone have a good Power Off and Power on Script from a list of VM's in a CSV? Possibly one that can monitor progress?
Thanks
Are you intending on stopping/starting those VMs in one call?
Or do you want to do this in batches?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I could do it either way. I plan on using it for a DR Test next month where I have a CSV for the VM's in SRM sorted by priority order. DBs (1) APP(2) Web(3). I'd run the shutdown. Then execute the recovery plans. After a clean up. Run the power up on csv to bring up the DBs, App, Web vm's in that order.
Does the CSV file contain the order in a column?
If the entries are sorted, how would a script know when 1 is finished and where 2 starts?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Quick and simple would be:
$csvFile = [Path To File].csv
if (!Test-Path $csvFile) {
$csvData = Import-CSV $csvFile
foreach ($line in $csvData) {
# Where each $line is a VM name.
Get-VM $line | Stop-VM -confirm:$false
}
}
Stop-VM won't move on until it's complete, and will give you a progress bar on top automatically.
I usually throw in a lot more checks and balances in scripts like this, but that's your call.
Can you run that with -RunAsync? So it does more than one at a time?
Yup! Stop-VM takes the -RunAsync parameter.
You can make your life a little easier by first placing all the Get-Vm objects in an array and passing the array to Stop-VM like so:
$csvFile = [Path To File].csv
$vms = @()
if (!Test-Path $csvFile) {
$csvData = Import-CSV $csvFile
foreach ($line in $csvData) {
# Where each $line is a VM name.
$vms += (Get-VM $line)
}
$vms | Stop-VM -RunAsync:$true -confirm:$false
}