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
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
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.
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
Here is the output -
It did not power off the VMs
I added some output to the background jobs.
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
LuCD Sir,
I tired your new code.
It didn't powered down the VMs
Did all the jobs complete?
What does it say when you do a Get-Job?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Job failed -
I don't see the Jobs submitted as vCenter task doesn't show this jobs , not even in failed state
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
All Jobs failed -
But can you do the Receive-Job I just gave?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That's not how it works.
I said Receive-Job -Id 15 -Keep
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry Sir, I thought since the job already failed, it will return error . Sorry about that. here is the output -
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.
Get-Module -Name VMware* -ListAvailable
$env:PSModulePath
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
}
}
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
And you didn't run the Get-Module line
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No, I didn't ran Get-Module