How to manage a Hyper-V core with powershell on the Hyper-V machine

by Ron 24. februari 2011 06:23

Virtual computers have always impressed me. Back in the days I had an Amiga 1200 with a Blizzard 1230VI expansion card. I emulated a Macintosh on this computer. Nowadays I have a Macbook and a copy of Windows Server 2003 Web Edition on it (VMware Fusion 2.0). I also have a bootcamp partition on which Hyper-V Core is installed. On this Hyper-V core a copy of Server 2008 WEB is installed. The only free option to manage the Hyper-V Core is using a MMC plugin in Windows Vista or Server 2008 server edition. I am using Powershell on the Hyper-V server. To use the powershell you must have a RDC with the Hyper-V machine.

Powershell

I experimented a lot with Powershell lately. I read somewhere that notepad can be run from the Hyper-V core. Woow, I thought, that is great, that solves the problem I had to get PS1 (Powershell) files on the Hyper-V core. I used a USB stick in the past, but that was very unhandy. The possibility to run notepad makes it possible to copy scripts from the clipboard of my Macbook, and paste them in a notepad document on the Hyper-V Core. In that way I am able to safe them in the C: drive of the Hyper-V core. And voila, then I will be able to execute these PS1 scripts from the Powershell!

notepad

The first script I run was a script to shutdown and boot the Windows 2008 Web edition from Hyper-V. I used the scripts on this website:

http://community.spiceworks.com/how_to/show/1793

And it worked! :-)

So, that was the first step. To make life easier, I wanted to install on the hyper-V server the PowerShell Management Library for Hyper-V at http://pshyperv.codeplex.com/.

I unzipped the files in a directory on the C drive. To use the library you have to install it. You need to re-install after rebooting the Hyper-V machine.

PS: C:\dir_with_library> .\install.cmd

To get the files on the Hyper-V server, I connect the Hyper-V server to a share on the Windows 2008 server. In this share the files are available that can be downloaded from http://pshyperv.codeplex.com.

To set up a share in the Windows 2008 server, you go to computer management and there is an option for adding shares. After setting up this share, I connect the Hyper-V server to this share with the following command in Powershell:

$net = new-object -ComObject WScript.Network
$net.MapNetworkDrive("u:", "\\server\share", $false, "domain\user", "password")

 

After this, you are able to install the files from this new share U:\.

 

With this library installed, it is possible to do all kind of things. For example find the path of the virtual images, export virtual images, take snapshots, etc.:

Finding a VM
Get-VM, Choose-VM , Get-VMHost

Connecting to a VM
New-VMConnectSession

Discovering and manipulating Machine states
Get-VMState , Set-VMState , Convert-VmState, 
Ping-VM , Test-VMHeartBeat, Shutdown-VM , Start-VM, Stop-VM, Suspend-VM
Get-VMKVP, Add-KVP, Remove-KVP, Get-VMJPEG

Backing up, exporting and snapshotting VMs
Export-VM , Import-VM, Get-VMSnapshot, Choose-VMSnapshot , Apply-VMSnapshot , New-VMSnapshot ,Remove-VMSnapshot, Rename-VMSnapShot, Update-VMSnapshot, Get-VMSnapshotTree, Get-VmBackupScript

Adding and removing VMs, configuring motherboard settings.
New-VM , Remove-VM , Set-VM , Get-VMCPUCount, Set-VMCPUCount, Get-VMMemory, Set-VMMemory, Set-VMSerialPort

Manipulating Disk controllers, drives and disk images
Get-VMDiskController
Add-VMSCSIController , Remove-VMSCSIcontroller
Get-VMDriveByController , Add-VMDRIVE , Remove-VMdrive
Get-VMDiskByDrive, Add-VMDISK , Set-VMDisk, Get-VMDisk
Get-VMFloppyDisk , Add-VMFloppyDisk
Add-VMNewHardDisk

Manipluating Network Interface Cards
Get-VMNic , List-VMNic , Choose-VMNIC, Add-VMNIC, Remove-VMNIC , Set-VMNICAddress , Set-VMNICConnection , Get-VMNicport , 
Get-VMnicSwitch, Choose-VMSwitch, New-VMSwitchPort, Get-VMByMACaddress, Choose-VMExternalEthernet,
New-VMExternalSwitch, New-VMInternalSwitch,New-VmPrivateSwitch

Working with VHD files
Get-VHDDefaultPath, Get-VHDInfo, New-VHD, Compact-VHD, Test-VHD,Convert-VHD,Merge-VHD,Mount-VHD, Unmount-VHD

Exporting a virtual machine VHD file to my Macbook

It would be great to be able to store a backup of a virtual machine running in Hyper-V on my Macbook. Then it will be able to run this VHD in Mac Fusion or I can import it in another Hyper-V core machine.

First I needed to do the export job:

PS C:\> export-vm VM c:\ -CopyState -Force

This created the VHD in the C:\ directory. -CopyState is required to copy the VHD to C:\

To see the progess type (please note that the InstanceID is between '':

PS C:\> Test-wmiJob '\\MS_HYPER-V\root\virtualization:Msvm_ConcreteJob.InstanceID="91321CDB-9AF7-49AF-BB08-E08EAB03BC11"'

When ready, it is time to get the file on the Macbook. To do this I shared a drive in MacosX with SMB and added the user that has access to the Hyper-V core. Done!

Happy computing!