C # When you call PowerShell to create a virtual machine in SCVMM, the creation progress is displayed in real time.

Source: Internet
Author: User

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.