as900w
Hot Shot
Hot Shot

How to check VMware Tools status of VM use PowerCLI

Jump to solution

I want to writer a script to shutdown VM.

Use Connect-VMServer connection to vCenter Server:

$credential = Get-Credential

Connect-VIServer -Server <vCenter Server FQDN or IP> -Credential $credential

Use Get-VM to get VM list  that power state is PoweredOn

Get-VM | Where-Object{$_.PowerState -eq "PoweredOn"} | Export-Csv -Path C:\VMList.csv -NoTypeInformation

$VMList = Import-Csv -Path C:\VMList.csv

( I want to combine these two statements,but it's failed. )

I want to check the  VMware Tools atatus of VM in the $VMList.

What should I do?

I want to use "if and else" statement:

If Tools status is OK, using Shutdown-VMGuest to Shut down VM.

slse using stop-VM to shut down VM

What should I do?

1 Solution

Accepted Solutions
ThompsG
Virtuoso
Virtuoso

Hi there,

Perhaps try something like this:

# Creates a variable that contains VMs which are in the PoweredOn state 

$VMList = Get-VM | Where { $_.PowerState -eq "PoweredOn" }

# Names of VMs that will not be shutdown or powered off

$VMExclusionList = "vm1","vm2","vm3"

 

# Loops through the variable an object at a time 

Foreach ( $VM in $VMList ) { 

     # Checks if the name of the current VM is in the ExclusionList and skips if it is

       If ( $VMExclusionList -notcontains $VM.Name ) {

      

          # Retrieve the status of the VMware Tools 

          $ToolsStatus = ($VM | Get-View).Guest.ToolsStatus 

        

          # Check status of tools and if not installed then issue Stop to VM otherwise Shutdown VM 

          If ( $ToolsStatus -eq "toolsNotInstalled" ) { 

        

               Stop-VM $VM

        

          } Else {

    

               Shutdown-VMGuest $VM

          } 

     }

}

Just update the $VMExclusionList variable with names of the VMs you don't want to shutdown or poweroff.

Kind regards.

View solution in original post

0 Kudos
8 Replies
ThompsG
Virtuoso
Virtuoso

Hi as900w,

Try the following (all standard disclaimers and warranty excemptions apply):

# Creates a variable that contains VMs which are in the PoweredOn state 

$VMList = Get-VM | Where { $_.PowerState -eq "PoweredOn"

# Loops through the variable an object at a time  

Foreach ( $VM in $VMList ) { 

     # Retrieve the status of the VMware Tools 

     $ToolsStatus = ($VM | Get-View).Guest.ToolsStatus 

     # Check status of tools and if not installed then issue Stop to VM otherwise Shutdown VM 

     If ( $ToolsStatus -eq "toolsNotInstalled" ) { 

          Stop-VMGuest $VM  

     } Else

          Shutdown-VMGuest $VM 

     }

}

NOTE: Test to confirm it does as you expect before unleashing in production

Kind regards.

0 Kudos
as900w
Hot Shot
Hot Shot

Thanks for your help.

it's OK.

If I want to exclude some VM in the list.

What should I do?

0 Kudos
ThompsG
Virtuoso
Virtuoso

Hi there,

Perhaps try something like this:

# Creates a variable that contains VMs which are in the PoweredOn state 

$VMList = Get-VM | Where { $_.PowerState -eq "PoweredOn" }

# Names of VMs that will not be shutdown or powered off

$VMExclusionList = "vm1","vm2","vm3"

 

# Loops through the variable an object at a time 

Foreach ( $VM in $VMList ) { 

     # Checks if the name of the current VM is in the ExclusionList and skips if it is

       If ( $VMExclusionList -notcontains $VM.Name ) {

      

          # Retrieve the status of the VMware Tools 

          $ToolsStatus = ($VM | Get-View).Guest.ToolsStatus 

        

          # Check status of tools and if not installed then issue Stop to VM otherwise Shutdown VM 

          If ( $ToolsStatus -eq "toolsNotInstalled" ) { 

        

               Stop-VM $VM

        

          } Else {

    

               Shutdown-VMGuest $VM

          } 

     }

}

Just update the $VMExclusionList variable with names of the VMs you don't want to shutdown or poweroff.

Kind regards.

0 Kudos
as900w
Hot Shot
Hot Shot

It's OK.

Thanks a lot!

as900w
Hot Shot
Hot Shot

The -Confirm:$False  is added after stop-vm command.

But it's failed.

0 Kudos
ThompsG
Virtuoso
Virtuoso

Was this for all VMs that don’t have the tools or just a couple?

0 Kudos
as900w
Hot Shot
Hot Shot

this tools is for all vm

0 Kudos
ThompsG
Virtuoso
Virtuoso

Hi as900w,

I'll test in a LAB and revert when I can.

Kind regards.

0 Kudos