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 PSDirect
  3. Suspend and view the PSDirect VM:
    Suspend-VM -VMName PSDirect
    Get-VM -VMName PSDirect
  4. Resume the VM:
    Resume-VM -VMName PSDirect
    Get-VM -VMName PSDirect
  5. Save the VM and check its status:
    Save-VM -VMName PSDirect
    Get-VM -VMName PSDirect
  6. Resume the saved VM and view the status:
    Start-VM -VMName PSDirect
    Get-Vm -VMName PSDirect
  7. Restart the VM:
    Restart-VM -VMName PSDirect -Force
    Get-VM     -VMName PSDirect
  8. Wait for the VM to get an IP address:
    Wait-VM    -VMName PSDirect -For IPaddress
    Get-VM     -VMName PSDirect
  9. Perform a hard power off on the VM:
    Stop-VM -VMName PSDirect -TurnOff
    Get-VM  -VMname PSDirect

How it works...

This recipe shows you how to manage VM state. In step 1, you view the properties of the VM, which is not running. As you can see from the screenshot, the PSDirect VM is turned off and is not running (and has an uptime of 00:00:00), as follows:

How it works...

With step 2, you start the PSDirect VM and retrieve the VM's status, which looks like this:

How it works...

Next, in step 3, you suspend the PSDirect VM, which looks like this:

How it works...

In step 4, you resume the PSDirect VM, which looks like this:

How it works...

With step 5, you save a VM and view the status, like this:

How it works...

With step 6, you start the saved PSDirect VM and view the VM's details, which looks like this:

How it works...

With step 7, you forcibly shut down the VM, which looks like this:

How it works...

In step 8, you wait until the PSDirect VM is up and running to re-view the VM's status, which looks like this:

How it works...

There's more...

With step 2, you start the VM and retrieve the VM's status. Next, in step 3, you suspend then resume a VM.

With step 3 and step 4, you suspend then resume the PSDirect VM. While the VM is suspended, the VM is not active and therefore receives and sends no network traffic. The VM's memory is maintained, as is the current state, and the VM can be resumed at any moment.

In step 6 you save the PSDirect VM. When you save a VM, Hyper-V saves the VM's memory to disk and the VM's virtual disks are not used. Saving a VM is similar to pausing it, except that with a saved VM, all the VM's memory is written to disk then released.

In step 7, you perform a forced shutdown of the PSDirect VM. This is equivalent to pulling the power from a running computer then restarting it. When you do this, all VM state is lost, and it is possible to introduce disk corruption due to data being still in memory and not written to disk prior to the power off. While Windows and the most-used Windows filesystems (NTFS and ReFS) are fairly resilient to errors, you should avoid hard shutdown if possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.150.123