PowerShell Friday: stopping VMs
This weeks PowerShell Friday is about stopping VMs with PowerCLI.
If you want to gracefully shutdown your VM, which I think you will do most of the time you are better of using Shutdown-VMGuest . This will use the VMware tools to gracefully shutdown your VM. Just as with starting VMs you have the option of specifying the VM on the command line or get it from the pipeline.
This command will shutdown all VMs starting with an ‘A’
Shutdown-VMGuest -VM A*
Checking if the VM is running
If you try to stop VMs that aren’t running you get errors and your commands don’t continue. To check if a VM is running and then stopping it can be done with testing the state:
Get-VMGuest -VM A* | Where-Object {$_.State -eq "Running"} | Shutdown-VMGuest
Don’t ask for permission, ask for forgiveness
A lot of, if not all, cmdlets in PowerCLI obey the -Confirm. With it you can tell the cmdlet you are executing if you want it to ask for permission, or just execute the command. The complete line above would look like this:
Get-VMGuest -VM A* | Where-Object {$_.State -eq "Running"} | Shutdown-VMGuest -Confirm:$False
If you’re not sure, try the -WhatIf parameter, or think some more before you execute the command. PowerCLI then will simulate what would happen if the cmdlet was executed, without actually doing anything.
Stopping VMs without VMware tools
If your not running VMware tools in the VM you can stop the VM with the cmdlet Stop-VM
Stop-VM -VM MyVM
It will stop your VM without saving anything, so be absolutely sure if you need this. Again, if you use the -Confirm parameter the VM will just be stopped without asking for permission.
If that doesn’t stop the VM you can always use the kill switch
Stop-VM -VM MyVM -Kill
If that didn’t do the trick…
Other articles in the series PowerShell Friday:
- PowerShell Friday: Getting Started with PowerShell and PowerCLI
- PowerShell Friday: Connecting to vCenter
- PowerShell Friday: Starting VMs
- PowerShell Friday: stopping VMs
- PowerShell Friday: Creating Virtual Machines
- PowerShell Friday: Snapshots
- PowerShell Friday: Adding CPU’s with PowerCLI
- PowerShell Friday: Adding Memory with PowerCLI
- PowerShell Friday: ExtensionData
- PowerShell Friday: Retrieving IP addresses for VMs
- PowerShell Friday: Copying files with Copy-VMGuestFile
- PowerShell Friday: Setting Reservations with PowerCLI
- PowerShell Friday: Enabling SSH with PowerCLI
- PowerShell Friday: Christmas Special
- PowerShell Friday: Configuring vSphere MTU Size
- PowerShell Friday: Load PowerCLI from your own script
- PowerShell Friday: Using the Cisco ACI API
Tags In
Anne Jan Elsinga
Related Posts
Leave a Reply Cancel reply
You must be logged in to post a comment.