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
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
ShowDVDMediaCollisions
This script is designed to gather data on how each VM is physically connected to optical media and present it in such a way that an admin could quickly track down conflicts between two VMs. This resolves the case of a VM that is unable to start because the connection to DVD media is already in use. For each VM that is mapped to a physical DVD/CD drive, the script provides which host it is on and which drive it is connected to.
This presents the data as one line per VM mapped to a physical drive. That satisfies my need for this issue but if you would prefer to output an array of VMs with the host and drive information as properties. See the Get-NetworkMap script for an example.
2: # Show DVD Media Collisions
8: # Useful for displaying Optical Media mapped to more than 1 VM on the same host
12: write-host $args
13:
14: # All variables in the script with customizable values are mapped here for convenience
15: $VMMServerName = "v-vmm-01"
16:
17: # Load Snap-Ins
18: Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager
19:
20: # Begin Script
21: Get-VMMServer $VMMServerName | out-null
22: $VM = Get-VM
23:
24: $LocationVMMap = @{}
25: foreach ($entry in $VM)
26: {
27:
28: foreach($connection in $entry.VirtualDVDDrives){If ($connection.HostDrive -ne $null){write-host "$($entry.Name) on $($entry.VMHost) is connected to DVD drive $($connection.HostDrive)"}}
29: #$entry.Name | %{$LocationVMMap += @($entry.VMHost, )}
30: }
Special thank you to Rajesh Ravindranath who helped me out a lot with this script.
Refresh All
This post is a double-hitter. I am posting two versions of the same script.
VMM is a fairly passive management tool. On a regular cycle, or due to specific events, VMM collects data about the objects it is managing. This includes things like files in the library, virtual machines, hosts, clusters, and information about the VMM server itself. The problem is if you open the Hyper-V Console or Failover Cluster console directly and make changes, or manually copy files to the Library via Windows Explorer, you must wait for VMM to update before your changes would be reflected. In most cases, this is a non-issue because the cycles are short but when doing a lot of work and making quick changes outside VMM I decided I needed a script that would just tell VMM to go update NOW!
I ended up implementing this in two flavors, “Quick” and “Full”. The issue causing me to fork the road is synchronous vs. asynchronous handling of each refresh job. If you enumerate everything VMM knows about and tell them each to refresh asynchronously (everyone update right now, don’t wait for your neighbor to complete) then some jobs will be unable to complete because the objects are part of some of other object. An example would be refreshing VMs while the host where they reside is refreshing, or refreshing a host that is part of a cluster that is already updating. I have even encountered conflicts between two concurrently updating hosts that share some configuration details.
The workaround is one script that process every single object VMM knows about and synchronously refresh each of them one at a time. As you can imagine, this process is very thorough but it takes a lot of time. The second script updates just the library and cluster and runs asynchronously. Even in our environment, a 6 node cluster (soon to be 8) with 3 library servers, running Quick brings the console up to speed in short order.
Simple is good. As always, I ignore the snap-in error when running from console. The script returns each VM name and the location for the conflicting file.
2: # Refresh All
8: # Useful for quickly refreshing VMM all library and host information
12: # All variables in the script with customizable values are mapped here for convenience
13: $VMMServerName = "v-vmm-01"
14:
15: # Load Snap-Ins
16: Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager
17:
18: # Begin Script
19: Get-VMMServer $VMMServerName | out-null
20: Get-LibraryShare | % {Refresh-LibraryShare $_}
21: Get-VMHostCluster | % {Refresh-VMHostCluster $_}
22: Get-VMHost | % {Refresh-VMHost $_}
23: Get-VirtualizationManager | % {Refresh-VirtualizationManager $_}
24: Get-VM | % {Refresh-VM $_}
2: # Quick Refresh
8: # Useful for quickly refreshing VMM library and cluster
20: Get-LibraryShare | % {Refresh-LibraryShare $_ -RunAsynchronously}
21: Get-VMHostCluster | % {Refresh-VMHostCluster $_ -RunAsynchronously}
Show VHD Collisions
This script looks for two VMs each mapped to the same VHD. This can cause issues when starting VMs and it is not simple to identify which VMs are in conflict.
Just to comment, there are very legitimate reasons for a VHD to be mapped to multiple VMs. If you had a very large set of data that needed to be accessed from two servers but those servers were not online at the same time, such as using a VHD as a container for transferring a large file copy rather than SMB. Now that R2 has hot-swap storage this is less of an issue than it was on RTM, so the examples of legitimate reasons are dwindling…
2: # Show Storage Collisions
8: # Useful for displaying any VHD or Pass-Through Disk mapped to more than 1 VM
20: $VM = Get-VM
22: $LocationVMMap = @{}
23: foreach ($entry in $VM)
24: {
25: $vhds = @(Get-VirtualHardDisk -vm $entry.Name)
26: If ($vhds) {$vhds | %{$LocationVMMap[$_.Location] += @($entry.Name)}}
27: }
29: if ($($LocationVMMap.Keys | ?{($LocationVMMap[$_]).Count –gt 1}) -lt 0) {write-host "No Collisions."}
30: Else {$LocationVMMap.Keys | ?{($LocationVMMap[$_]).Count –gt 1} | %{Write-Host “$($LocationVMMap[$_] –join ‘, ‘)" -nonewline -foregroundcolor yellow; Write-Host " collide on VHD file " -nonewline; Write-Host "$_.” -foregroundcolor yellow}}
Get-NetworkMap
This script is designed to gather data on how each VM is physically connected and present it in such a way that an admin could quickly track down the network path for troubleshooting purposes. For each VM, the script provides which host it is on, the virtual network name, vlan id, adapter type, the name of the physical device where the virtual network is assigned, and the physical network connection name for that device.
If the VM has more than one network adapter, each VM is displayed within the properties of that VM in the output. Commas separate the individual values so if you have several adapters you may need to consider modifying the script to output in a more friendly way. With 2-3 adapters, tracing the data across fields and keeping it straight is trivial.
I tried a lot of approaches at how this data should be presented and how the script should run. I settled on treating the script like a commandlet and passing command line arguments. So all you need to do is open PS somewhere that you have the console installed and run the script by passing it your VMM server name. The nice thing about this is the output can be stored to a new array to be worked in to other solutions.
example, “$netinfo = get-networkmap.ps1 v-vmm-01”
or dig a lot deeper, such as reporting the physical MAC address where the network for each VM is bound.
foreach ($record in $netinfo){write-host ""$record.name" - "$(foreach ($nic in $record.device){$nic.macaddress})""}
That’s all there is to it! See the very first listing, multiple values for each field because more than 1 adapter is assigned.
2: # Network Map
8: # Useful for displaying the physical network adapter information for each VM
10: # Command Arguements
11: # The script expects the VMM server name to be provided as an arguement
12: #
13: # ------------------------------------------------------------------------------
15: # All variables in the script with customizable values are mapped here for convenience
16: $VMMServerName = $args[0]
18: # Load Snap-Ins
19: Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager
20:
21: # Begin Script
22: Get-VMMServer $VMMServerName | out-null
23: $VM = Get-VM
24:
25: $NetworkMap = @()
26: foreach ($entry in $VM)
27: {
28: $nic = (Get-VirtualNetworkAdapter -vm $entry.Name)
29: $hostadapters = foreach ($adapter in $nic) {get-virtualnetwork -vmhost $entry.VMHost -name $adapter.VirtualNetwork}
30: $connection = foreach ($network in $hostadapters) {$network.VMHostNetworkAdapters}
31: $hostnetworkadapters = $hostadapters.VMHostNetworkAdapters
32:
33: $netinfo = "" | Select-Object Name, HostName, VirtualNetwork, Type, VLAN, Device, Connection
34: $netinfo.Name=$entry.Name
35: $netinfo.HostName=$entry.VMHost
36: $netinfo.VirtualNetwork=foreach($virtualnetwork in $hostadapters){$virtualnetwork.Name}
37: $netinfo.Type=foreach($type in $nic){$type.VirtualNetworkAdapterType}
38: $netinfo.VLAN=foreach($vlan in $nic){$vlan.VLanId}
39: $netinfo.Device=foreach($device in $hostadapters){$device.VMHostNetworkAdapters}
40: $netinfo.Connection=foreach($nc in $connection){$nc.connectionname}
41: $NetworkMap += $netinfo
42: }
43:
44: $NetworkMap