hyper-v scripts and snips from the field rob mcshinsky dartmouth hitchcock medical center...

32
Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

Upload: annice-randall

Post on 11-Jan-2016

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

Hyper-V Scripts and Snips From The Field

Rob McShinsky

Dartmouth Hitchcock Medical CenterVirtuallyAware.com

Page 2: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 3: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 4: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 5: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 6: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 7: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 8: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 9: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 10: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 11: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 12: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 13: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 14: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 15: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 16: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 17: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

• (Hyper-V)

• Get VMs with ISO mountedGet-VMDisk -server <SERVERNAME>| ? {$_.diskpath -like "*.iso"} | ft vmelementname,diskpath –a

Page 18: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

• (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

Page 19: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

• (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

Page 20: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 21: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

}

}

Page 22: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 23: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

}

}

Page 24: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 25: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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"

Page 26: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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"

Get-VMMServer -computername $VMMGet-VM | ? {$_.hostname -like "$SH*"} | Move-VM -VMHost "$DH*"

Page 27: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 28: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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*"

Page 29: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

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

Page 30: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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

Page 31: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.com

Questions

Virt

ually

Aw

are.

com

Page 32: Hyper-V Scripts and Snips From The Field Rob McShinsky Dartmouth Hitchcock Medical Center VirtuallyAware.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