次の方法で共有


VMM Script Series, Part 6 – New-VM

I’m publishing some of the scripts I have written for VMM.  These are each based on specific customer asks or conversations that lead me to go sleepless in trying to solve the problems myself.  I am very interested in your feedback, especially if you would like to offer a change to improve efficiency or effectiveness.

New-VM

Not a lot to this one folks!  In fact when I was looking through my scripts library, I was surprised I never published this one in the past.  I use this script the most frequently of all.

The goal when I wrote this was to simplify deploying a new VM to as few steps as possible.  I have used a lot of static variables in the script so it only asks for a server name.  This means your template and profiles need to have already been created.  If you want to fancy it up, I would start by prompting the user to input additional values at run time.

Notes

In each of the scripts for this series, I have noted any manually assigned variables at the top of the script just under the header.  For this script you will want to update your VMM server name.

In each of the scripts for this series, I have added a line to load the VMM snap-in.  This way it can be run from any machine, including a workstation, where the VMM console is installed by right clicking on the file and choosing “run”.  If you run the script from within VMM you will get a few lines of error text that the snap-in is already loaded.  You can either ignore this, or comment out the line if it bothers you.

Screen Shots

 image

Script

disclaimer: this script is not supported in any way. I have posted the code rather than the .ps1 file so that you can review it, modify it to make it your own, and test it before trusting it in a production environment. now this is your code, I am not responsible for its use.

  1: # ------------------------------------------------------------------------------
  2: # New-VM
  3: # ------------------------------------------------------------------------------
  4: # blogs.technet.com/offcampus
  5: # version 1.0
  6: #
  7: # Description
  8: #  Useful for quickly creating a VM from template in SCVMM
  9: #
  10: # ------------------------------------------------------------------------------
  11:  
  12:  
  13: # All variables in the script with customizable values are mapped here for convenience
  14: $VMMServerName = "v-vmm-01"
  15: $HostGroupName = "All Hosts"
  16: $ProfileName = "Mid"
  17: $TemplateVHDSize = 40
  18: $NetworkName = "External1"
  19: $TemplateName = "Windows Server 2008 R2 Ent Full - MID"
  20: $TimeZone = 20
  21:  
  22:  
  23: # Load Snap-Ins
  24: Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager 
  25:  
  26: # Begin Script
  27: $Name = Read-Host "Please type a name for the new server"
  28:  
  29: $VMMServer = get-vmmserver -computername $VMMServerName
  30: $VMHostRating = Get-VMHostRating -VMHostGroup $HostGroupName -HardwareProfile $ProfileName -DiskSpaceGB $TemplateVHDSize -VMName $Name
  31: $JobGroupId = [Guid]::NewGuid().ToString()
  32: New-VirtualNetworkAdapter -JobGroup $JobGroupId -VirtualNetwork $NetworkName
  33: New-VM -JobGroup $JobGroupId -Template $TemplateName -Name $Name -VMHost $VMHostRating[0].Name -ComputerName $Name -ProductKey "XXXXX-XXXXX-XXXXX-XXXXX-XXXXX" -Path $VMHostRating[0].vmhost.vmpaths[0] -TimeZone $TimeZone -RunAsynchronously -RunAsSystem -StartAction NeverAutoTurnOnVM -StopAction SaveVM