VMware Cloud Community
mvbenz1
Contributor
Contributor
Jump to solution

PowerCli Assistance

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:

  1. Systems need to be powered on in groups.
  2. Systems need to come up before other systems come up (specific order).

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

  • All Domain Controllers come up

Start-VM *File0* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools

  • All servers with File0 in the name come up

Start-VM *FS0* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools

  • All Servers with FS0 in the name come up

Start-VM *WEB* | Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools

  • All Servers with WEB in the name come up

Start-VM *DB1P* |  Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools

  • All Servers with DB1P in the name come up

Start-VM *DB2P* |  Where-Object {$_.powerstate -eq ‘PoweredOff’} | Wait-Tools

  • All Servers with DB2P in the name come up

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...

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

Reply
0 Kudos
5 Replies
a_p_
Leadership
Leadership
Jump to solution

Discussion moved from PowerShellers to VMware vSphere™ PowerCLI

Reply
0 Kudos
FMON
Enthusiast
Enthusiast
Jump to solution

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)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos
mvbenz1
Contributor
Contributor
Jump to solution

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:

SRM Custom Scripts

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Reply
0 Kudos