Hyper-V Server 2008 R2 Scripts and Tips

19 Mar

Here’s a small selection of commands I found useful when setting up a Hyper-V Server 2008 R2 box.

Firstly, I’m using a HP DL380 G5 as my main Hyper-V box. To enable the Hypervisor, enter the bios by pressing F9 at startup. Go to “Advanced Options” and then “Processor Options. You then need to enable both “Non Executive Memory Protection” and Intel® Virtualization Technology”. Save this by pressing F10 and then reboot the box.

You can install the management tools on a Vista machine (get them here http://support.microsoft.com/kb/952627) and this will allow you to add/delete and edit existing VMs without having to go though the command line on the hyper-v box.

Should you wish to use the command line, you can access the data and time application by running:

control.exe “timedate.cpl”

And you can also access the Regional/Language options by entering:

control.exe “intl.cpl”

Most other control panel options seem to be disabled within Hyper-V Server 2008 R2. A friend of mine also sent me a very useful script to setup the correct firewall permissions on the box to allow remote access:

echo off
netsh advfirewall set allprofiles settings remotemanagement enable
netsh advfirewall firewall set rule group=”Remote Administration” new enable=yes
netsh advfirewall firewall set rule group=”Remote Desktop” new enable=yes
cscript %windir%\system32\scregedit.wsf /AR 0
cscript %windir%\system32\scregedit.wsf /CS 0
winrm quickconfig –q

I also encountered a few problems when I first made the mode to hyper-v. I hindsight, it’s a good ida to set your Dc/GC VM’s to auto start in the properties box. Had I have done this, I wouldn’t have still been in the office at 11:50 on a Friday night. It’s also a good idea to have a DC on a separate physical box, just in case. I run my backup box with its tape drive as a DC as well as a kinda failsafe.

Anyway, should you need to turn your VM’s on and off from the command line, then you will need to start using the powershell.

You can run the PowerShell by calling “powershell” from the command line. Once into the PowerShell, you will notice that the command line has changed to:

PS c:\Windows\System32\en-US>

Now you’re in, you can start playing. You will need to get hold of some PowerShell scripts, grab them here http://www.codeplex.com/PSHyperv. Useful commands to note are:

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