Skip to content

Hyper-V PowerShell

Port mirror VM card Hyper-V to virtual NIC ManagementOS on the same vSwitch on that host

Set-VMSwitch -Name "External" -AllowManagementOS $True Set-VMNetworkAdapter -VMName TEST-MDT -Name "Network Adapter" -PortMirroring Source Set-VMNetworkAdapter -ManagementOS -Name "External" -PortMirroring Destination Set-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName External -Access -VlanId "11"

(Get-VMSwitch -Name "External").AllowManagementOS (Get-VMNetworkAdapter -VMName TEST-MDT -Name "Network Adapter").PortMirroringMode (Get-VMNetworkAdapter -ManagementOS -Name "External").PortMirroringMode Get-VMNetworkAdapterVlan -ManagementOS -VMNetworkAdapterName External

Get virtual machine on Hyper-V cluster

$objs=@();Get-ClusterResource | where {$.ResourceType -eq "Virtual Machine" -and $.State -eq "Online"} | foreach {$Nodename = $.OwnerNode; $VMName = $.Name.Substring(16); $VM = Get-VM $VMName -ComputerName $Nodename -ErrorAction SilentlyContinue; $Processors = $VM.ProcessorCount; if ($VM.DynamicMemoryEnabled) {$Memory = $([math]::round($VM.MemoryAssigned/1MB))} else {$Memory = $([math]::round($VM.MemoryStartup/1MB))}; $DynamicMemoty = $VM.DynamicMemoryEnabled; $NICs = $VM.NetworkAdapters; $Switch = @(); $VLAN = @(); $IP = @(); foreach ($NIC in $NICs) {$Switch += $NIC.SwitchName; $VLAN += $NIC.VlanSetting.AccessVlanId; $IP += $($NIC.IPAddresses | where {$_.Length -le 15 })}; $Drives = $VM.HardDrives; $Controller = @(); $Volume = @(); $Capacity = @(); $UsedSpace = @(); foreach ($Drive in $Drives){$DriveObj = Get-VHD $Drive.Path -ComputerName $Nodename; $Controller += $Drive.ControllerType; $Volume += $Drive.Path.Split("\")[2]; $Capacity += $([math]::round($DriveObj.Size/1GB)); $UsedSpace += $([math]::round($DriveObj.FileSize/1GB))}; $Generation = $VM.Generation; $IntegrationServices = $VM.IntegrationServicesVersion.Revision; $obj = [PSCustomObject]@{Host = $Nodename; VM = $VMName; CPUs = $Processors; MemoryMB = $Memory; DynMem = $DynamicMemoty; vSwitches = $([string]::join(“, ”, ($Switch))); VLAN = $([string]::join(“, ”, ($VLAN))) ; IPs = $([string]::join(“, ”, ($IP))); DiskTypes = $([string]::join(“, ”, ($Controller))); VolumeStores = $([string]::join(“, ”, ($Volume))); DiskCapacityGB = $([string]::join(“, ”, ($Capacity))); DiskUsedGB = $([string]::join(“, ”, ($UsedSpace))); VMGen = $Generation; IntegrSvcVer = $IntegrationServices}; $objs += $obj};$objs | Sort-Object Host, VM | ConvertTo-HTML | Out-File .\clustervm.html

Get Hyper-V SCSI controllers

$objs=@();Get-ClusterResource | where {$.ResourceType -eq "Virtual Machine" -and $.State -eq "Online"} | foreach {$Nodename = $.OwnerNode; $VMName = $.Name.Substring(16); $SCSIControllers = @(Invoke-Command -ScriptBlock {Get-VMScsiController $args[0]} -ComputerName $Nodename -ArgumentList $VMName -ErrorAction silentlycontinue).count;$obj = [PSCustomObject]@{Host = $Nodename; VM = $VMName; SCSIControllers = $SCSIControllers};$objs += $obj};write-output $objs|sort SCSIControllers,VM

Get Physical-to-Virtual CPU Ratio on Hyper-V Cluster

foreach ($Node in Get-ClusterNode){$Processors = (Get-VMHost $Node.Name).LogicalProcessorCount;$SumVMCPU=0;foreach ($VM in Get-VM -ComputerName $Node.Name){$VMCPU=(Get-VMProcessor $VM).Count;$SumVMCPU = $SumVMCPU + $VMCPU};Write-Host "$Node CPU number: $Processors";Write-Host "Summary VM CPU on the host: $SumVMCPU";Write-Host "Physical-to-Virtual CPU ratio is 1 to $([math]::round($($SumVMCPU/$Processors)))"}