VMware Cloud Community
rtjohns1
Contributor
Contributor
Jump to solution

Looking to shutdown VMs that contain a specific annotation.

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?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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"}} 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

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"}} 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rtjohns1
Contributor
Contributor
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Can you give the complete error message ?

A screenshot is fine.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rtjohns1
Contributor
Contributor
Jump to solution

Here is the error message.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Please try again.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rtjohns1
Contributor
Contributor
Jump to solution

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.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Code is corrected


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
rtjohns1
Contributor
Contributor
Jump to solution

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

Hope someone else will find this useful.

Thanks a Million LucD!

0 Kudos