PowerShell for System Center Virtual Machine Manager and Hyper-V

Microsoft System Center Virtual Machine Manager 2008 R2

This little HowTo shows you how can you get tasks on System Center Virtual Machine Manager done, by using Windows Powershell.

Load Powershell Snapin for Virtual Machine Manager:

Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager

Create Virtual Machine:

# Config
# ------------------------------------
# Job Config
$JobGroup = "0000001" # This is used to group command by a job
$SCMVVMServer = "SCVVMServer01" # Name of the SCVMM Server (could also be localhost)
 
# Network Config
$VirtualNetwork = "External" # Name of the Network you want the VM to connect
$VLanEnable = $true # eable VLANs
$VLANID = "1023" # VLAN ID
 
# VMM Config
$Domain = "Contoso"
$Owner = "Ownerusername" # Owner User
$Description = "This is a Server" # Choose a Description
$VMName = "server05" # Name of the VM
$VMHost = "hyperv02" # Name of the existing VM Host
$VMPath = "C:\ProgramData\Microsoft\Windows\Hyper-V"
 
# Virtual Machine Config
$VMOperatingSystem = "64-bit edition of Windows Server 2008 R2 Standard"
$CPU = "1.20 GHz Athlon MP" # CPU
$VMDiskSize = "40960" # Disk Size in MB
$CPUCount = "1"
$MemoryMB = "1024" # Memory Size in MB
$ExpectedCPUUtilization = "20"
$DiskIO = "0"
$CPUMax = "100"
$CPUReserve = "0"
$NetworkUtilization = "0"
$RelativeWeight = "100"
$HighlyAvailable = $false
$NumLock = $false
$BootOrder = "CD", "IdeHardDrive", "PxeBoot", "Floppy"
$LimitCPUFunctionality = $false
$LimitCPUForMigration = $false
 
# Setup Process
# ------------------------------------
Set-VirtualFloppyDrive -RunAsynchronously -VMMServer $SCVMMServer -NoMedia -JobGroup $JobGroup
Set-VirtualCOMPort -NoAttach -VMMServer $SCVMMServer -GuestPort 1 -JobGroup $JobGroup
Set-VirtualCOMPort -NoAttach -VMMServer $SCVMMServer -GuestPort 2 -JobGroup $JobGroup
New-VirtualNetworkAdapter -VMMServer $SCVMMServer -JobGroup $JobGroup -PhysicalAddressType Dynamic -VirtualNetwork $VirtualNetwork -VLanEnabled $VLanEnable -VLANID $VLANID -MACAddressesSpoofingEnabled $false
$CPUType = Get-CPUType -VMMServer $SCVMMServer | where {$_.Name -eq $CPU}
New-HardwareProfile -VMMServer $SCVMMServer -Owner ($Domain + "\" + $Owner) -CPUType $CPUType -Name ("Profile" + $JobGroup) -CPUCount $CPUCount -MemoryMB $MemoryMB -ExpectedCPUUtilization $ExpectedCPUUtilization -DiskIO $DiskIO -CPUMax $CPUMax -CPUReserve $CPUReserve -NetworkUtilization $NetworkUtilization -RelativeWeight $RelativeWeight -HighlyAvailable $HighlyAvailable -NumLock $XMLTask.Feature.NumLock -BootOrder $BootOrder -LimitCPUFunctionality $LimitCPUFunctionality -LimitCPUForMigration $LimitCPUForMigration -JobGroup $JobGroup
New-VirtualDiskDrive -VMMServer $SCVMMServer -IDE -Bus 0 -LUN 0 -JobGroup $JobGroup -Size $VMDiskSize -Dynamic -Filename ($VMName + "_disk_1")
$VMHost = Get-VMHost -VMMServer $SCVMMServer | where {$_.Name -eq $VMHost}
$HardwareProfile = Get-HardwareProfile -VMMServer $SCVMMServer | where {$_.Name -eq ("Profile" + $JobGroup)}
$OperatingSystem = Get-OperatingSystem -VMMServer $SCVMMServer | where {$_.Name -eq $VMOperatingSystem}
 
# Create VM
# ------------------------------------
New-VM -VMMServer $SCVMMServer -Name $VMName -Description $Description -Owner ($Domain + "\" + $Owner) -VMHost $VMHost -Path $VMPath -HardwareProfile $HardwareProfile -JobGroup $JobGroup -RunAsynchronously -OperatingSystem $OperatingSystem -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM

Delete (Remove) Virtual Machine:

# Config
# ------------------------------------
$VMName = "server05" # Name of the VM
 
# Setup Process
# ------------------------------------
$SelectedVM = Get-VM -Name $VMName
 
# Remove/Delete VM
# ------------------------------------
Remove-VM -VM $SelectedVM

Suspend Virtual Machine:

# Config
# ------------------------------------
$VMName = "server05" # Name of the VM
 
# Setup Process
# ------------------------------------
$SelectedVM = Get-VM -Name $VMName
 
# Suspend VM
# ------------------------------------
Suspend-VM -VM $SelectedVM

Resume Virtual Machine:

# Config
# ------------------------------------
$VMName = "server05" # Name of the VM
 
# Setup Process
# ------------------------------------
$SelectedVM = Get-VM -Name $VMName
 
# Resume VM
# ------------------------------------
Resume-VM -VM $SelectedVM

Stop Virtual Machine / Turn off Virtual Machine:

# Config
# ------------------------------------
$VMName = "server05" # Name of the VM
 
# Setup Process
# ------------------------------------
$SelectedVM = Get-VM -Name $VMName
 
# Stop / Turn off VM
# ------------------------------------
Stop-VM -VM $SelectedVM

Start Virtual Machine:

# Config
# ------------------------------------
$VMName = "server05" # Name of the VM
 
# Setup Process
# ------------------------------------
$SelectedVM = Get-VM -Name $VMName
 
# Start VM
# ------------------------------------
Start-VM -VM $SelectedVM

This is a reference how you can do some thing with Powershell in the Virtual Machine Manager. You can do a lot more, like error handling, creating virtual machines from a xml config file etc…