Hello. Im somewhat new to PowerCLI. Im looking for a way to set the VM restart priority for 1000+ machines (spread across multiple clusters and vCenters). The priority is based on the first two letters of a machine's name.
For example a machine named GPmymachine01 is considered a production machine and its restart priority would be set as high. Any other G*mymachine name is considered a test machine and its restart priority would be set as low.
I've considered setting each machine individually (e.g. Set-VM mymachine -HARestartPriority High), but doing that with 1000+ machines in a dynamic environment would be time intensive and not necessarily reusable.
I think I need some type of IF statement, but I'm not very familiar with coding/scripting techniques to know how to accomplish this. Any help is very much appreciated. Something like this?.....
Get-VM | {
if ( vm name starts with GP ) {# set restart priority High}
else {# set restart priority low}
}
A simple solution that uses RegEx matching could be something like this.
Do you also have VM that do not start with 'G'? And what priority do they need?
$vms | where{$_.Name -match '^gp'} | Set-VM -HARestartPriority High -Confirm:$false
$vms | where{$_.Name -match '^g(?!p)'} | Set-VM -HARestartPriority Low Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
A simple solution that uses RegEx matching could be something like this.
Do you also have VM that do not start with 'G'? And what priority do they need?
$vms | where{$_.Name -match '^gp'} | Set-VM -HARestartPriority High -Confirm:$false
$vms | where{$_.Name -match '^g(?!p)'} | Set-VM -HARestartPriority Low Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Wow, thank you for the quick reply! I just tested your examples and they are perfect.
I do have VMs that do not start with 'G'
And should their restart priority be changed as well, or left as is?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Left as is for now; their naming schemes dont lend themselves to easy grouping. Thanks again!