I am looking for a way to script the turn on of VMs in a specific order using wild cards. Some of the requirements are as follows:
What I am looking at is a few lines of code where a set of servers is defined using wild cards. In the below example I would like all DCs to come up first if they are in a powered off state, wait for tools to load (system is actually up) then move on to the next step and do the same for file servers with a naming format of *File0*, then servers in the format *FS0*, etc... Each line of code should not execute until the Wait-Tools comes back good. Main reason for this is certain systems need to be up before others or some apps don't work right.
Start-VM *DC0* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools
Start-VM *File0* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools
Start-VM *FS0* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools
Start-VM *WEB* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools
Start-VM *DB1P* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools
Start-VM *DB2P* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools
When I use the WhatIf parameters it tells me all of the servers it would power on but does not go beyond the Start-VM Command so I cannot tell if the Where clause or Wait-Tools command do anything. I don't have a sandbox to play in yet so I figured I would ask the experts and see if I am on the right track first.
Thx!
Mike...
You would first need to "get" the VM ebfore chekcing the powerstate, and then power on the ones that aren't.
Something like this
Get-VM *DC0* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Start-VM | Wait-Tools
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Discussion moved from PowerShellers to VMware vSphere™ PowerCLI
See if this helps:
$doathing = $false # <Set this to $true to do a thing! set to $false to see what it would do!
$vmGroups = @{'Group01'='dc0'
'Group02'='file0'
'Group03'='fs0'
'Group04'='web'}
$i = 1
do
{
#decipher what things we are doing things to!
$groupNumber = "{0:D2}" -f [int]$i
$groupName = "Group" + $groupNumber
$groupVms = Get-VM | where {$_.Name -match $vmGroups.$groupName -and $_.powerstate -eq 'PoweredOff'}
#start the Vms!
foreach ($Vm in $groupVms)
{
Write-Host "Starting $($Vm.Name)"
if ($doathing -eq $true){$Vm | Start-VM}else{Write-Host "Didn't start VM $($Vm.Name) because doathing is false!"}
}
#wait for tools on each Vm!
foreach ($Vm in $groupVms)
{
Write-Host "Waiting for tools on $($Vm.Name)"
if ($doathing -eq $true){$Vm | Wait-Tools}else{Write-Host "Didn't wait for tools on VM $($Vm.Name) because doathing is false!"}
}
$i++
}
while($i -le $vmGroups.Count)
You would first need to "get" the VM ebfore chekcing the powerstate, and then power on the ones that aren't.
Something like this
Get-VM *DC0* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Start-VM | Wait-Tools
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
both solutions look good but this one was simplest.
I have another question more related to running this script in SRM if anyone can chime in on that it would be great:
I saw that thread, but I'm afraid I don't have an answer to those questions right now.
Will have to spend some more lab time on SRM :smileycry:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference