Managing VM state

Managing the VM state involves stopping and starting, or pausing and resuming, a VM. You can also save and restore, as shown in this recipe.

Getting ready

This recipe uses the PSDirect VM created in the Creating a virtual machine recipe. This recipe assumes the PSDirect VM is stopped when you start this recipe. If this VM is running, then first stop it using the Stop-VM cmdlet.

How to do it...

  1. Get the VM's state to check if whether is off:
    Stop-VM -Name PSDirect -WarningAction SilentlyContinue
    Get-VM -Name PSDirect
  2. Start the VM, get its status, then wait until the VM has an IP address assigned and the networking stack is working, then examine the VM's state:
    Start-VM -VMName PSDirect Wait-VM -VMName PSDirect -For IPAddress Get-VM -VMName ...

Get Windows Server 2019 Automation with PowerShell Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.