Automation

 View Only
  • 1.  Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 03, 2016 06:18 PM

    Hi Community Members,

    I have been working with variations of scripts that will allow you to gracefully shutdown our Virtual Environment.  Since we have many Infrastructure machines running as VM's,( i.e, Domain Controllers, DHCP Servers, VCenter Servers), we wanted to be able to shutdown the machines in the appropriate order based on a Tiered Structure. 

    We currently have a great script, (thanks to resources within this community),  that identifies all of the machines that are powered on, exports that information and works with a second script to power up only the machines that we previously powered down. The trouble is we have to name each machine that we want to, (or dont want to shut down). the line in the script looks like this:


    #Get-VM -Location $cluster | Where-Object { $_.PowerState -eq “PoweredOn” } | where { $_.Name -notlike “*DB*” -and $_.Name -notlike “*VC*” -and $_.Name -notlike “*DC*” }

    A colleague thought that it would be a great idea to add a custom attribute,  then have the shutdown script look for that specific attribute and Shutdown/Suspend that machine. 

    So we created an annotation for all machines, lets call it "SD_Procedure" and give it a value of 1, 2, or 3 based on the order that want the machines to shut down, (tier 3 shutdown first, then Tier 2 then Tier 1, etc.).  I can pull these machines with the following line:

    #Get-VM |  get-annotation -Name SD_Proedure | where-object {$_.Value -eq "2"}

    My trouble is that I want to identify the machines that are currently powered on as well as the ones that with a specific Value in the annotation field, and shut that machine down. I cant figure how these should be combined to get the correct result.

    Can someone help?



  • 2.  RE: Looking to shutdown VMs that contain a specific annotation.
    Best Answer

    Posted Feb 03, 2016 06:28 PM

    You can do something  like this, first filter out the powered on VMs, then check the annotation for those that come through

    $poweredontier3 = Get-VM -Location $cluster |

        Where { $_.PowerState -eq “PoweredOn” } |

        where{Get-Annotation -Entity $_ -Name esp-attr | where-object { $_.Value -eq "3"}} 



  • 3.  RE: Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 03, 2016 07:14 PM

    LucD its an honor to have you reply. 

    If I try your recommendation in the context of the script but got an error about "object not found".  I tried this:

    $poweredontier3 = Get-VM -Location $cluster | Where { $_.PowerState -eq “PoweredOn” } | Get-Annotation -Name esp-attr | where-object { $_.Value -eq "3"}

    #Power off Tier 3 VMs.

    foreach ($tier3 in $poweredontier3)

    {

      Write-Host “Processing $guest…” -ForegroundColor Green

      Write-Host “Checking for VMware Tools Install” -ForegroundColor Green

      $guestinfo = get-view -Id $tier3.Id

      if ($guestinfo.config.Tools.ToolsVersion -eq 0)

      {

      Write-Host “No VMware tools detected in $tier3, these will be powered off” -ForegroundColor Yellow

      Stop-VM $tier3 -Confirm:$false

      }

      else

      {

      Write-Host “VMware tools detected. I will attempt to gracefully shutdown tier 3 $tier3”

      $vmshutdown = $tier3 | Suspend-VMGuest -Confirm:$false

      Sleep 5

      }

    }

    It seems that I am missing something simple...



  • 4.  RE: Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 03, 2016 08:19 PM

    Can you give the complete error message ?

    A screenshot is fine.



  • 5.  RE: Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 03, 2016 08:49 PM
      |   view attached

    Here is the error message.



  • 6.  RE: Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 04, 2016 05:44 AM

    Oops, the 2nd Where-clause was not correct. I updated it above.

    Please try again.



  • 7.  RE: Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 04, 2016 03:46 PM
      |   view attached

    Tried the corrected Where statement but now I get a new error. "Value cannot be found for the mandatory parameter Entity".  Pulling my hair out.



  • 8.  RE: Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 04, 2016 05:27 PM

    My mistake (again), I forgot to include the Entity on the Get-Annotation.

    Code is corrected



  • 9.  RE: Looking to shutdown VMs that contain a specific annotation.

    Posted Feb 04, 2016 06:02 PM

    That was the missing element  "-Entity $_".

    Hope someone else will find this useful.

    Thanks a Million LucD!