C # Calls PowerShell to control SCVMM. There are many examples on the Internet, which are relatively simple. However, the process of creating a virtual machine is a long time, so generally, during creation, you want to display the creation progress of the current Virtual Machine in real time. At that time, this problem had plagued me for a while, but after finding a method, it was actually very simple.
Environment:
Win server 2008 R2 + Hyper-v + SCVMM2008 R2
Purpose:
C # When you call PowerShell to create a virtual machine in SCVMM, the creation progress is displayed in real time.
When you manually create a vm (Virtual Machine) in SCVMM2008R2, the job interface displays detailed creation progress, including the subtasks, task completion level, status, and other information. The operations on the SCVMM interface are based on Powershell, so there must be a ps script to achieve the above purpose.
In the ps script for creating a VM provided by microsoft, the following content is mentioned (to display part of the PS script, press Enter)
$NewVMTasks = [System.Array]::CreateInstance("Microsoft.SystemCenter.VirtualMachineManager.Task", $NumVMs)
$NewVMs = [System.Array]::CreateInstance("Microsoft.SystemCenter.VirtualMachineManager.VM", $NumVMs)
$i = 0
# Loop that creates each VM asynchronously.
while($NumVMs -gt 0)
{
# Generate a unique VM name.
$VMRnd = $Random.next()
$NewVMName = $VMName+$VMRnd
# Get the ratings for each host and sort the hosts by ratings.
$Ratings = @(Get-VMHostRating -Template $Template -VMHost $VMHosts -DiskSpaceGB $DiskSizeGB -VMName $NewVMName | where { $_.Rating -gt 0} | Sort-Object -property Rating -descending)
if ($Ratings.Count -gt 0)
{
$VMHost = $Ratings[0].VMHost
$VMPath = $Ratings[0].VMHost.VMPaths[0]
# Create a new VM from the template and add an additional VHD
# to the VM.
$NewVMJobGroup = [System.Guid]::NewGuid()
$VMAdditionalVhd | Add-VirtualHardDisk -Bus 0 -Lun 1 -IDE -JobGroup $NewVMJobGroup
$NewVMs