Automation

 View Only
  • 1.  PowerCli Assistance

    Posted Dec 19, 2016 09:05 PM

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



  • 2.  RE: PowerCli Assistance

    Posted Dec 19, 2016 09:22 PM

    Discussion moved from PowerShellers to VMware vSphere™ PowerCLI



  • 3.  RE: PowerCli Assistance

    Posted Dec 19, 2016 10:25 PM

    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)



  • 4.  RE: PowerCli Assistance
    Best Answer

    Posted Dec 20, 2016 06:06 AM

    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



  • 5.  RE: PowerCli Assistance

    Posted Feb 16, 2017 05:56 PM

    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



  • 6.  RE: PowerCli Assistance

    Posted Feb 16, 2017 07:26 PM

    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: