hyper-v scripts and snips from the field rob mcshinsky dartmouth hitchcock medical center...
TRANSCRIPT
Hyper-V Scripts and Snips From The Field
Rob McShinsky
Dartmouth Hitchcock Medical CenterVirtuallyAware.com
Who Am I?
• Sr. System Engineer – Dartmouth Hitchcock Medical Center
• Microsoft MVP (Virtual Machine)
• Advisory Board Member and Contributor for TechTarget
• Blogger – VirtuallyAware.com
• Twitter - @VirtuallyAware
Virt
ually
Aw
are.
com
PowerShell’s Role In Hyper-V?
• If you are serious about running Hyper-V, you NEED to know how to utilize the various PowerShell CMDlets (Hyper-V, SCVMM)
• Get Information about your VMs or overall environment for reporting or troubleshooting
• Set values on one or hundreds of VMs
• Perform coordinated tasks based on certain information gathered
Virt
ually
Aw
are.
com
What Are We Going To Talk About?
• PowerShell CMDLets for Hyper-V in Windows 2008 R2, System Center Virtual Machine Manger, and Hyper-V 3.0 in Windows Server 8
• Powershell for Administration. Sometimes Quick and Dirty, but gets the job done.
• How PowerShell can fill most any management gap that might be found in the Hyper-V Manager or SCVMM console.
• These scripts are meant to be used as is or as starting points and examples to customize within your Hyper-V Environment.
• Feel free to ask questions or disagree.
Virt
ually
Aw
are.
com
How To Get The PowerShell Modules
Hyper-V (RTM, R2, R2 SP1) Not included by default.• http://pshyperv.codeplex.com/releases• http://www.VirtuallyAware.com (Scripts download, right hand side of
page)• Launch by:
– Opening a PowerShell window and typing “Import-Module HyperV”
System Center Virtual Machine Manager (SCVMM)• Built-in when SCVMM console is installed• Launch by:
– Clicking on Powershell Icon on top of console or by typing – Opening a PowerShell “Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager”
Hyper-V 3.0• Built-in• Launch by:
– opening a Powershell window and typing “Import-Module Hyper-V”
Virt
ually
Aw
are.
com
Get VM name from VM ID
• Troubleshooting: Many times, eventlog entries will identify the VM that had the issue by its VM ID.
• Translating the VM ID to the name of the VM allows you to narrow your troubleshooting
Virt
ually
Aw
are.
com
• (Hyper-V)get-vm | ? {$_.name -eq "EABF5B51-446F-463D-9E16-06841A7B1648"} | fl elementname
• (SCVMM)get-vm | ? {$_.VMid -eq "EABF5B51-446F-463D-9E16-06841A7B1648"} | ft name
• (Hyper-V 3.0) – Same as SCVMMget-vm | ? {$_.VMid -eq "0bd618d9-8e39-4336-9d9b-0bdd4664d8b8”}| ft name
Use Case: SCVMM Console Showing Incorrect VM Performance Stats
Finding misconfigured VMs with Emulated network cards
• Another common setup mistake by admins.• Using default create “New Virtual Machine” option uses
Emulated (non-optimized/parent partition routed) virtual network adapter.
• Optimizing your network traffic of your VMs and reducing Host/parent partition cpu utilization would require changing these to Synthetic network adapters.
Virt
ually
Aw
are.
com
• Examples to list VMs with Emulated virtual network adapters.
• (Hyper-V)Get-VMNIC -server <HOSTNAME> -legacy | ft VMElementName, ResourceSubTypeOrGet-VMNIC -server <HOSTNAME>| ? {$_.ResourceSubType -like "*emulated*"} | ft VMElementName, ResourceSubType
• (SCVMM)Get-VirtualNetworkAdapter -all | where {$_.virtualnetworkadaptertype -eq "emulated" } |ft name,VirtualNetworkAdapterType,ethernetaddress
• (Hyper-V 3.0)Get-VMNetworkAdapter -all | ? {$_.name -like "legacy*"} | ft VMname,Name
Note: Virtual Server 2005 VMs will all come back as Emulated. To differentiate look at some other attribute in the Get-VirtualNetworkAdapter . Many times default MAC Address will be different between VS 2005, Vmware and Hyper-V.
• Example with ‘ethernetaddress’ attribute listed to filter out VS 2005 VMs (SCVMM)
Get-VirtualNetworkAdapter -all | where {$_.virtualnetworkadaptertype -eq "emulated" -and $_.ethernetaddress -like "00:15*"} |ft name,VirtualNetworkAdapterType,ethernetaddress
Finding VM from MAC address
• At times if you using dynamic assignment of MAC addresses there can be times when a duplicate MAC Address if found.
• As a result of a P2V where the MAC Address of the physical hardware is carried forward to the VM and then at some later date the physical hardware gets repurposed.
• Commonly a popup and eventlog entry will be seen that states that there was a MAC Address conflict, giving you the MAC Address in the event.
Virt
ually
Aw
are.
com
• Example to find VM name from MAC Address
• (Hyper-V)Get-VMNIC -server <SERVERNAME> | ? {$_.address -eq "00155DF3CFD5"} | ft vmelementname, address
• (SCVMM)Get-VirtualNetworkAdapter -all | ? {$_.ethernetaddress -eq "00:03:ff:07:e7:dc"} | ft name,ethernetaddress
• (Hyper-V 3.0)Get-VMNetworkAdapter -all |? {$_.macaddress -eq "00155DF24800"} | ft vmname,macaddress
Finding VMs with Snapshots
• Snapshots can pose a hidden disk space utilization problem if left unmanaged.
• Full volumes can affect all VMs sharing a particular volume(Pause – Critical)
• Demo
Virt
ually
Aw
are.
com
• (Hyper-V)Get-VMSnapshot -server <SERVERNAME> | ft elementname,systemname
Note: SystemName returns VMID. Use previous script to get vm name from VMID.
• (SCVMM)get-vm | where {$_.LastRestoredVMCheckpoint -like "*"} | ft name, LastRestoredVMCheckpoint, hostname
• (Hyper-V 3.0)get-vm | Get-VMSnapshot | ft vmname,name,creationtime
Finding VMs with Unmerged VHD files
• Hyper-V Checkpoints/Snapshot “files” do not disappear when deleted from the management console.
• A common admin mistake is to remove the checkpoint/snapshot, but forget to shutdown the VM to allow the merging of the AVHD changed to merge into the parent VHD.
• Result is that the .AVHD file can keep dynamically building using up valuable disk space.
Virt
ually
Aw
are.
com
• (Hyper-V)get-vmdisk -server <SERVERNAME> | ? {$_.diskpath -like "*.avhd"} | ft vmelementname, diskpath
• (SCVMM)get-vm | get-virtualharddisk | ? {$_.location -like "*.avhd"} | ft name, location, hostname -a
• (Hyper-V 3.0)Not a problem in Hyper-V 3.0 since Checkpoints/Snapshots Live Merge without downtime!!!!
To see that the .AVHD(X) location Before deleting the Checkpoint/Snapshot.get-vm | Get-VMHardDiskDrive | ft vmname, path -a
Finding VMs with ISOs mounted (and dismounting)
• Cleaning up ISO files that are mounted to VMs is just a good practice as it can cause problems with Migrating VMs if Delegation is not setup correctly.
• You may also have a file lock on a remote ISO file that you want to move or delete that is attached to a VM. But which one?
Virt
ually
Aw
are.
com
• (Hyper-V)
• Get VMs with ISO mountedGet-VMDisk -server <SERVERNAME>| ? {$_.diskpath -like "*.iso"} | ft vmelementname,diskpath –a
• (SCVMM)
• Get VMs with ISO mountedget-vm | get-virtualdvddrive |? {$_.iso -like "*"} | ft name, iso -a
• Remove all ISO files from VMsget-vm | get-virtualdvddrive |? {$_.iso -like "*"} | set-virtualdvddrive -nomedia
• Remove VMGuest ISO Only (All maybe to broad)get-vm | get-virtualdvddrive |? {$_.iso -like "vmg*"} | set-virtualdvddrive -nomedia
• (Hyper-V 3.0)
• Get VMs with ISO mountedget-vm| get-VMDvdDrive | ? {$_.path –like”*.iso”} | ft vmname, path
• Remove all ISO files from VMsget-vm |get-VMDvdDrive | ? {$_.path -like "*} | Set-VMDvdDrive -path $null
• Remove VMGuest ISO Only (All maybe to broad)get-vm |get-VMDvdDrive | ? {$_.path -like “*vmguest.iso”} | Set-VMDvdDrive -path $null
Integration Services Installs
• Coordination of installation of Integration Components for all VMs on a Particular Host in mass (SCVMM)
• Upgraded a Host that now needs all VM Integration Components install.
• Administrator error. Not installing or updating Integration Components
• Older Template from another version of Hyper-V.
Virt
ually
Aw
are.
com
• (SCVMM)
$SH = Read-Host "Enter Source Host Name to Update all VMs"
$VMM = Get-VMMServer “VMM_SERVERNAME" #Set this to the name of your SCVMM server
$VMs = Get-Vm | ? {$_.hostname -like "$SH*"} #Get all the VMs on the SCVMM Server
foreach ($VM in $VMs) {
if ($VM.Status -eq ‘Running’) { #Only Work with those Vms that are Off
Write-Host ‘Starting with ‘$VM.Name
Shutdown-VM $VM #Machine needs to be off for this to work
Set-VM –VM $VM –InstallVirtualizationGuestServices $TRUE
Start-VM $VM
}
}
Integration Services Installs
• Coordination of installation of Integration Components for a list of VMs (SCVMM)
• Targeted VMs on Various Hosts.
Virt
ually
Aw
are.
com
• (SCVMM)
Foreach ($VM in get-content servers.txt) {
if ($VM.Status -eq ‘Running’) { #Only Work with those Vms that are Off
Write-Host ‘Starting with ‘$VM.Name
Shutdown-VM $VM #Machine needs to be off for this to work
Set-VM –VM $VM –InstallVirtualizationGuestServices $TRUE
Start-VM $VM
}
}
Live Migrate VMs
• Live Migration is works very well but with Hyper-V in Windows Server 2008 R2 SP1, you are limited to one live migration off the host.
• Can change that, but with PowerShell we an automate the synchronous Live Migration from one host to another.
• SCVMM comes with a Maintenance Mode option, but what if you wanted to preserve the mix of VMs that are currently on a node of your Hyper-V cluster
Virt
ually
Aw
are.
com
• Utilizes the FailoverCluster CMDLet
Import-Module FailoverClusters
$CL = Read-Host "Enter Cluster Alias Name"$SH = Read-Host "Enter Source Host Name"$DH = Read-Host "Enter Destination Host Name"
get-cluster "$CL" | Get-Clusternode "$SH" | Get-ClusterGroup | Move-ClusterVirtualMachineRole -node "$DH"
• (SCVMM)
Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager
$VMM = Read-Host "Enter Name of VMM Server"$SH = Read-Host "Enter Source Host Name"$DH = Read-Host "Enter Destination Host Name"
Get-VMMServer -computername $VMMGet-VM | ? {$_.hostname -like "$SH*"} | Move-VM -VMHost "$DH*"
Live Migrate Particular VMs
• There are times when you might want to only Live Migrate particular VMs on a host.
• Need to find common attributes between all the VMs you want to Live Migrate from one host to another
• CSV Redirection and VM placement demo.
Virt
ually
Aw
are.
com
• (SCVMM)
Add-PSSnapin Microsoft.SystemCenter.VirtualMachineManager
$VMM = Read-Host "Enter Name of VMM Server"$SH = Read-Host "Enter Source Host Name"$DH = Read-Host "Enter Destination Host Name"$Vol = Read-Host "Enter Volume/CSV Volume to Move VMs to Destination Host"
Get-VMMServer -computername $VMMGet-VM | ? {$_.hostname -like "$SH*"} | ? {$_.Location -like "*$Vol*"} | Move-VM -VMHost "$DH*"
What Else Can You Do?• A Lot more
• Look for CMDLets associated with VMs, Hosts, Clusters and Experiment! (Remember to Import the Module or Add Snapin first.
• (Hyper-V and SCVMM)
Help Get
Help Add
Help New
Help Set-VM -full
Help Move-VM -full
Virt
ually
Aw
are.
com
Articles• SCVMM PowerShell scripts simplify administrative tasks
• Top Hyper-V PowerShell cmdlets for basic tasks
• SCVMM Console Showing Incorrect VM Performance Stats
• Using SCVMM PowerShell Cmdlets To Customize Hyper-V Live Migrations
• Overcoming Hyper-V Live Migration Limitations with PowerShell CMDLets
• Add Possible Owner to a Cluster Shared Volume
• Script Snip: “I am trying to find out how I can get the number of processors for a non-active VM”
• SCVMM Script Snip
Virt
ually
Aw
are.
com
Contact and Blogs
• My Bog– VirtuallyAware.com
• Twitter - @VirtuallyAware
• Email – [email protected]
• Other Hyper-V 3.0 Resources:– Virtualization Team - http://blogs.technet.com/b/virtualization/– Ben Armstrong - http://blogs.msdn.com/b/virtual_pc_guy/– Aiden Finn - http://www.aidanfinn.com/– Hyper-V.nu – http://www.hyper-v.nu– Thomas Maurer - http://www.thomasmaurer.ch/– Kristian Nese- http://kristiannese.blogspot.com/
Virt
ually
Aw
are.
com