VMware Cloud Community
shahb
Contributor
Contributor
Jump to solution

Poweroff and Poweron 1000 VMs in Parallel

Hi

I've a maintenance is coming up for which I need to shutdown 1000+ VMs and once the Maintenance is complete, Need to start those VMs back on.

I've a list of 1000 VMs and I can run in for loop but that will take hours to shutdown these VMs and I only have 2 hours of Maintenance window, Can someone please help ?

Here is the script which I got from the community and it works as expected but it runs serially.

foreach ($vmlist in (Get-Content -Path C:\vmlist1.txt)){

$vm = Get-VM -Name $vmlist

Start-VM -VM $vm -Confirm:$false -RunAsync

}

Thanks & Regards

59 Replies
shahb
Contributor
Contributor
Jump to solution

yup.. you nailed it LuCD..

But it did not shutdown the VMs.

I see the line is there -

Get-VM -Name $VMName | Shutdown-VMGuest -Confirm:$false

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

We will need to check what the output of the background jobs is.

Can you try this version?

It should return the background job output.

$script = {

   param(

   [string]$VMName,

   [string]$vcId

   )


   Connect-VIServer -Session $vcId


   Get-VM -Name $VMName | Shutdown-VMGuest -Confirm:$false

}


$fileName = '.\input.csv'


$MaxJobs = 10


$jobs = @()


Import-Csv -Path $fileName | ForEach-Object -Process {

   if ($jobs.Count -le $MaxJobs) {

   Write-Host "Job for $($_.VMName)"

   $job = (Start-Job -Verbose -ScriptBlock $script -ArgumentList $_.VMName, $global:DefaultVIServer.SessionId)

   Write-Host "`tStarted job $($job.Id)"

   $jobs += $job

   Write-Host "`tRunning jobs $($jobs.Count)"

   }

   else {

   Receive-Job -Job $jobs -Wait

   $jobs = @()

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

Here is the output -

pastedImage_0.png

0 Kudos
shahb
Contributor
Contributor
Jump to solution

It did not power off the VMs

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I added some output to the background jobs.

$script = {

   param(

   [string]$VMName,

   [string]$vcId

   )


   Write-Host "VM $VMName"

   Connect-VIServer -Session $vcId


   Get-VM -Name $VMName | Shutdown-VMGuest -Confirm:$false -Verbose

}


$fileName = '.\input.csv'


$MaxJobs = 10


$jobs = @()


Import-Csv -Path $fileName | ForEach-Object -Process {

   if ($jobs.Count -le $MaxJobs) {

   Write-Host "Job for $($_.VMName)"

   $job = (Start-Job -Verbose -ScriptBlock $script -ArgumentList $_.VMName, $global:DefaultVIServer.SessionId)

   Write-Host "`tStarted job $($job.Id)"

   $jobs += $job

   Write-Host "`tRunning jobs $($jobs.Count)"

   }

   else {

   Receive-Job -Job $jobs -Wait

   $jobs = @()

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

LuCD Sir,

I tired your new code.

It didn't powered down the VMs

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did all the jobs complete?

What does it say when you do a Get-Job?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

Job failed -

pastedImage_0.png

0 Kudos
shahb
Contributor
Contributor
Jump to solution

I don't see the Jobs submitted as vCenter task doesn't show this jobs , not even in failed state

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Status is failed, that means they encountered an error.
But the job also states there is more info.

Are you sure Receive-Job didn't return anything?
What if you do Receive-Job -Id 15 -Keep


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

All Jobs failed -

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

But can you do the Receive-Job I just gave?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's not how it works.
I said Receive-Job -Id 15 -Keep


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

Sorry Sir,  I thought since the job already failed, it will return error . Sorry about that. here is the output -

pastedImage_0.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ah, now we are getting somewhere.
The script can't find the PowerCLI cmdlets.

Where and how did you install PowerCLI?

What PowerCLI and PowerShell versions are you using?

What is in the $env:PSModulePath variable?

Run this from a PowerShell prompt.

$PSVersionTable

Get-Module -Name VMware* -ListAvailable

$env:PSModulePath


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

I can run connect-VIServer from VMWare PowerCli and that's how I first connected to v Center.

I can run this script without any issue -

# Connect to vCenter

Connect-VIServer -Server rwcvc01 -U shahb5

foreach($vmlist in (Get-Content -Path C:\Scripts\MDC\vmlist1.txt)){

$vm = Get-VM -Name $vmlist

if($vm.Guest.State -eq "Running"){

Shutdown-VMGuest -VM $vm -Confirm:$false -RunAsync

}

else{

       Stop-VM -VM $vm -Confirm:$false -RunAsync

   }

}

pastedImage_0.png

pastedImage_1.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you by any chance running a PowerCLI pre-6.5 R1 version?

What does Get-PowerCLIVersion return?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership
Jump to solution

And you didn't run the Get-Module line


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
shahb
Contributor
Contributor
Jump to solution

No, I didn't ran Get-Module

pastedImage_0.png

0 Kudos