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?
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.
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.
Thanks for your help.
it's OK.
If I want to exclude some VM in the list.
What should I do?
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.
It's OK.
Thanks a lot!
The -Confirm:$False is added after stop-vm command.
But it's failed.
Was this for all VMs that don’t have the tools or just a couple?
this tools is for all vm
Hi as900w,
I'll test in a LAB and revert when I can.
Kind regards.