WavebreakmediaMicro - Fotolia
You can certainly manage Hyper-V hosts and VMs with Hyper-V Manager or System Center Virtual Machine Manager, but in some cases, it's easier to use PowerShell. With this scripting language and interactive command line, you can perform a number of actions, from simply importing a VM and performing a health check to more complex tasks, like enabling replication and creating checkpoints. Follow these five expert tips, and you'll be well on your way to becoming a Hyper-V PowerShell pro.
Import and export Hyper-V VMs
If you need to import and export VMs and you don't have the Hyper-V role installed, you can install Hyper-V PowerShell modules on a management server. To export a single VM, use the Export-VM command. This command creates a folder on the path specified with three subfolders -- snapshots, virtual hard disks and VMs -- which contain all of your VM files. You also have the option to export all of the VMs running on a Hyper-V host or to specify a handful of VMs to export by creating a text file with the VM names and executing a short script using that file. To import a single VM, use the Import-VM command. The import process will register the VM with Hyper-V and check for compatibility with the target host. If the VM is already registered, the existing VM with the same globally unique identifier will be deleted, and the VM will be registered again.
Check Hyper-V host and VM health
You can perform a complete health check for Hyper-V hosts and VMs by using PowerShell commands. When it comes to checking the health of your Hyper-V hosts, there are a lot of elements to consider, including the Hyper-V OS and its service pack, memory and CPU usages, Hyper-V uptime and total, used and available memory. If you want to perform a health check for a standalone host, you can use individual Hyper-V PowerShell commands. To perform a health check for a cluster, use Get-ClusterNode to generate a report. When performing a VM health check, consider the following factors: VM state, integration services version, uptime, whether the VM is clustered, virtual processors, memory configuration and dynamic memory status. You can use Get-VM to obtain this information and a script using the command to check the health status of VMs in a cluster.
Enable Hyper-V replication
Hyper-V replication helps keep VM workloads running in the event of an issue at the production site by replicating those workloads to the disaster recovery site and bringing them online there when need be. To configure Hyper-V replication, you need at least two Hyper-V hosts running Windows Server 2012 or later. There are a few steps involved, but it's a pretty straightforward process. First, you need to run a script on the replica server to configure the Hyper-V replica and enable required firewall rules. Then, execute a script on the primary server to enable replication for a specific VM -- we'll name it SQLVM, in this case. Finally, initiate the replication with Start-VMInitialReplication –VMName SQLVM. After you've completed this process, the VM on the replica server will be turned off, while the one on the primary server will continue to provide services.
Create Hyper-V checkpoints
If you'd like to test applications or just play it safe in case a problem arises, enable Hyper-V checkpoints on your VMs so you can roll back changes to a specific point in time. The option to take point-in-time images is disabled by default, but you can enable it for a single VM with the following Hyper-V PowerShell command: Set-VM. In order to use production checkpoints, you'll have to also configure the VM to do so. One you enable and configure the VM to use checkpoints, you can use CheckPoint-VM to create a checkpoint, and the entry will include the date and time it was taken. Unfortunately, the above command won't work on its own to create checkpoints for VMs on remote Hyper-V hosts, but you can use a short script to create a checkpoint in this instance. To restore a checkpoint, simply stop the VM, and then use the Restore-VMSnapshot command.
Use Port ACL rules in Hyper-V
Port Access Control Lists (ACLs) are an easy way to isolate VM traffic from other VMs. To use this feature, you'll need Windows Server 2012 or later, and your VMs must be connected to a Hyper-V switch. You can create and manage Port ACL rules using just a few Hyper-V PowerShell commands, but you need to gather some information first. Figure out the source of the traffic, the direction of the traffic -- inbound, outbound or both -- and whether you want to block or allow traffic. Then, you can execute the Add-VMNetworkAdapterACL command with those specific parameters. You can also list all of the Port ACL rules for a VM with the Get-VMNetworkAdapterACL command. To remove a Port ACL rule associated with a VM, use Remove-VMNetworkAdapterACL. As a time-saver, combine the two previous PowerShell cmdlets to remove all of the VM's Port ACL rules.
Deep dive into Windows PowerShell
Manage cached credentials with PowerShell
Use PowerShell to enable automated software deployment
Learn what's new in PowerShell