Hi Everyone,
I'm looking for the Power cli script to shutdown the specific VM's from lists at the Cluster level and once the changes on Hosts i.e. EVC mode enabled
then all the powered off VM's needs to power on back. Thanks in advance
thanks
vmk
Try like this
# Part 1
$vmNames = Get-Content -Path .\vmnames.txt
$vms = Get-VM -Name $vmNames |
where{$_.PowerState -eq 'PoweredOn'}
Shutdown-VMGuest -VM $vms -Confirm:$false
while((Get-VM -Name $vmNames).PowerState -contains 'PoweredOn'){
sleep 5
}
$vms | Select Name | Export-Csv -Path .\vm-powered-off.csv -NoTypeInformation -UseCulture
# Part 2
$vmNames = Import-Csv -Path .\vm-powered-off.csv -UseCulture
Get-VM -Name $vmNames.Name |
Start-VM -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
How is the list of VMs provided?
Text file, CSV file...?
And if CSV, what is the layout?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I'll prefer the text file E.g. C:\Temp\Lists.txt
However, if you have better choice then please advice. Let me know if missed out anything here.
Thanks
vmk
Try like this.
It uses a text file, one name per line.
Note that the table is for Intel EVC modes, should you be using AMD, you will have to change the table.
See KB1003212
$clusterName = 'MyCluster'
$evcModesIntel = @'
Level,Name
0,Merom
1,Penryn
2,Nehalem
3,Westmere
4,SandyBridge
5,IvyBridge
6,Haswell
7,Broadwell
8,Skylake
'@
$evcTab = @{}
ConvertFrom-Csv -InputObject $evcModesIntel -Delimiter ',' | %{
$evcTab.Add("intel-$($_.Name)",$_.Level)
}
$vmNames = Get-Content -Path .\vmnames.txt
$vms = Get-Cluster -Name $clusterName |
Get-VM -Name $vmNames |
where{$_.PowerState -eq 'PoweredOn'}
Shutdown-VMGuest -VM $vms -Confirm:$false
while((Get-VM -Name $vmNames).PowerState -contains 'PoweredOn'){
sleep 5
}
$maxEvcModeLevel = Get-Cluster -Name $clusterName | Get-VMHost | %{$evcTab.Item($_.MaxEvcMode)} |
Measure-Object -Maximum | select -ExpandProperty Maximum
$maxEvcMode = $evcTab.GetEnumerator() | where{$_.Value -eq $maxEvcModeLevel} | select -ExpandProperty Name
Get-Cluster -Name $clusterName |
Set-Cluster -EVCMode $maxEvcMode -Confirm:$false
Start-VM -VM $vms -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Few questions, Does the script enable the EVC mode also ? if yes, then do i need to give option like Westmere, IVyBridge etc... I dont have to manual EVC enable ?
Hardware is
Thank you
vmk
LucD,
Thankyou. As of now, we want only to shut down and power on the VM's once we enable the EVC mode manually.
Now, we want to exclude the EVC mode from power cli, only power on and power off the VM's on the cluster level. We will do perfom EVC manual.
Thanks
vmk
The script select the maximum available EVC mode on your cluster, and sets that one.
If it is only a power off/power on you want, leave out the EVC part.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You mean like this
$vmNames = Get-Content -Path C:\TEMP\vmlist.txt | where{$_ -ne ''}
{
$vms = Get-Cluster -Name $clusterName |
Get-VM -Name $vmNames |
where{$_.PowerState -eq 'PoweredOn'}
Shutdown-VMGuest -VM $vms -Confirm:$false
while((Get-VM -Name $vmNames).PowerState -contains 'PoweredOn'){
sleep 5
}
What about if i want seprately i.e power off the all the VM's from the lists and once my work done then i want to power on all the Bulk i.e. 1500 spread across the cluster.
In that case you would have to store the names of the VMs you powered off somewhere in an external format.
For example, script 1 to power off
$clusterName = 'MyCluster'
$vmNames = Get-Content -Path .\vmnames.txt
$vms = Get-Cluster -Name $clusterName |
Get-VM -Name $vmNames |
where{$_.PowerState -eq 'PoweredOn'}
Shutdown-VMGuest -VM $vms -Confirm:$false
while((Get-VM -Name $vmNames).PowerState -contains 'PoweredOn'){
sleep 5
}
$vms | Select Name | Export-Csv -Path .\vm-powered-off.csv -NoTypeInformation -UseCulture
And then later on, when you are done, you run script 2
$vmNames = Import-Csv -Path .\vm-powered-off.csv -UseCulture
Get-VM -Name $vmNames.Name |
Start-VM -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
Does the script will power off all the VM's in sequence from the lists.txt or it will fire off all VM's at the same time (or it wait for ist VM's to shutdown then proceed further )? Also for powering on the VM's, do i need to save the VM's names in CSV format ?
I'm going to test now
Thanks
vmk
Normally it will follow the same order as in the .txt file, but that is not 100% guaranteed.
You would need code in the script to control the order.
The Shutdown-VMGuest will start the guest OS shutdown inside the VM and then come back.
If you want to make sure that VM is completely powered off, you'll have to add a loop to test for that after each Shutdown-VMGuest cmdlet.
The CSV is there because I wasn't sure if the stop and start would happen at the same time, i.e. in one run of the script.
If the 2 parts are run separately I needed a medium to pass the selected VMs from part 1 to part 2.
This doesn't have to be a CSV file, it can be whatever format that you write/read.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I'm going to test in test environment but i found that in script i need to give Cluster name everytime whenever i run the script. Is it possible to implement the script across the cluster since i'm picking up specific VM's given in the lists vmliste.txt
please confirm.
regards
vmk
Try like this
# Part 1
$vmNames = Get-Content -Path .\vmnames.txt
$vms = Get-VM -Name $vmNames |
where{$_.PowerState -eq 'PoweredOn'}
Shutdown-VMGuest -VM $vms -Confirm:$false
while((Get-VM -Name $vmNames).PowerState -contains 'PoweredOn'){
sleep 5
}
$vms | Select Name | Export-Csv -Path .\vm-powered-off.csv -NoTypeInformation -UseCulture
# Part 2
$vmNames = Import-Csv -Path .\vm-powered-off.csv -UseCulture
Get-VM -Name $vmNames.Name |
Start-VM -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Awesome!! It worked.
couple of questions, i have, if you remember i have asked scheduling the VM's upgrade (HW compatibility) in another thread. I have scheduled the VM's HW upgrade but powering on it only upgraded the VM's to version 13 which was running on ESXi6.5 U2 but it didnt upgraded the VM's - version 8/9 running on ESXi5.5. Any reason ?
I think your script says version 13 thats why it didn't touched. can we add both for ESX5.5. and 6.5 ?
$do = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$do.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$do.ScheduledHardwareUpgradeInfo.UpgradePolicy =[VMware.Vim.ScheduledHardwareUpgradeInfoHardwareUpgradePolicy]::onSoftPowerOff
$do.ScheduledHardwareUpgradeInfo.VersionKey = “vmx-13”
foreach($vmName in (Get-Content -Path C:\TEMP\vmliste.txt)){
$vm = Get-VM -Name $vmName
$vm.ExtensionData.ReconfigVM_Task($do)
}
thanks
vmk
LucD,
Well, i think i need to run the script twice . Example one for V13 and Version 8/9. right ?
Thank you if you have some other idea to combine both then let me know. Thank you again for making the task easy.
regards,
vmk
According to KB2007240 you could use version 10 for 5.5.
Something like this
$do = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$do.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$do.ScheduledHardwareUpgradeInfo.UpgradePolicy =[VMware.Vim.ScheduledHardwareUpgradeInfoHardwareUpgradePolicy]::onSoftPowerOff
foreach($vmName in (Get-Content -Path C:\TEMP\vmliste.txt)){
$vm = Get-VM -Name $vmName
if($vm.VMHost.Version -match '6.5'){
$do.ScheduledHardwareUpgradeInfo.VersionKey = “vmx-13”
}
elseif($vm.VMHost.Version -match '5.5'){
$do.ScheduledHardwareUpgradeInfo.VersionKey = “vmx-10”
}
$vm.ExtensionData.ReconfigVM_Task($do)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you !! Will test this option too, since we have VM's running on both the ESXi 5.5. & ESXi 6.5
LucD,
Last week, I have performed the upgrade for VMware tools & VM version 13, but during post upgrade, there were around 25 VM's post-upgrade were not coming up with error "The destination host is not compatible with the hardware version to which the virtual machine is scheduled to be upgraded " I fixed it using below KB article.
http://www.running-system.com/error-destination-host-not-compatible/
https://kb.vmware.com/s/article/2080107?lang=en_US
Now today, we were unable to migrate VM's to new Cluster and the VMotion is failing with error " There are compatibility issues that need your attention."
I guess and as per my understanding - the First issue was due to VM's was already having VM version 13 and the script changed the VMX configuration while executing since no upgrade was required and 2nd error is due to the VM was running on EVC mode disabled while doing the VM version upgrade and we are trying to migrate to New Cluster having EVC mode enabled which is not allowing.
Before I start the prod VM's since we will have short windows, I want to isolate the issue. Kindly help.
thanks
vmk
Didn't those VMs that were already on version 13 report this, when you ran the report?
For the 2nd part, I assume you ran a report that listed the EVC mode?
Or are you looking for such a report?
Get-VM | Select Name,
@{N='VMHost';E={$_.VMHost.Name}},
@{N='MinEVCModeKey';E={$_.ExtensionData.Runtime.MinRequiredEVCModeKey}},
@{N='Cluster';E={(Get-Cluster -VM $_).Name}},
@{N='ClusterEVCMode';E={(Get-Cluster -VM $_).EVCMode}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
No, i didnt run the report. I want to confirm from you, is the reason for VM boot failure because of the script changed the script ?
However, for the 2nd part, I want to confirm that the VM's not able to migrate after an upgrade? My analysis is correct?
thanks
vmk