# DeviceID format is like "Microsoft:38A2575F-162A-49BE-ADE4-A4CD2856A900\F080A659-FBA0-414D-BC80-D0501C39E7C1"
# Format the single slash into double \\ , to embed it into a WQL Query
if ($DeviceID -ne $null)
{
$DeviceIDArray = $DeviceID.Split('\');
# Device Id can have multiple slashes
if ($DeviceIDArray -ne $null)
{
foreach ($deviceIdPart in $DeviceIDArray)
{
$DeviceIDFixed = $DeviceIDFixed + $deviceIdPart + "\\";
}
$DeviceIDFixed = $DeviceIDFixed.TrimEnd("\\")
}
$WQLQueryFormat = "Select Connection from {0} where InstanceId='{1}'"
$WQLQuerySynthetic = [String]::Format( $WQLQueryFormat, $SyntheticEthernetPortClass, $DeviceIDFixed);
$WQLQueryEmulated = [String]::Format( $WQLQueryFormat, $EmulatedEthernetPortClass, $DeviceIDFixed);
# check out emulated if nothing found in synthetic port setting data
if ($wmiResultArray -eq $null)
{
$wmiResultArray = gwmi -Query $WQLQueryEmulated -Namespace "root\virtualization"
}
if ($wmiResultArray -ne $null)
{
$connectionArray = $wmiResultArray.Connection
if ($connectionArray -ne $null -and $connectionArray.Count -ne 0)
{
# Sample connection string is like
#\\HOSTNAME\root\virtualization:Msvm_SwitchPort.CreationClassName="Msvm_SwitchPort",Name="afa71add-a5c8-4b6a-a611-529e9
# 42bc017",SystemCreationClassName="Msvm_VirtualSwitch",SystemName="corp-SCVMM-ce9b6c56-9953-4c8b-afca-49e1f660bb12"
$connectionString = $connectionArray[0]
# split the connection string to get switch name and port name
$connectionStringPieces = $connectionString.Split('"');
if ($connectionStringPieces -ne $null)
{
# craft the perfmon instance name = VirtualSwitch.Name_SwitchPort.Name
$VPortPerfmonInstanceName = $connectionStringPieces[7] + "_" + $connectionStringPieces[3]
$wmiPerfCounter = gwmi Win32_PerfRawData_NvspPortStats_HyperVVirtualSwitchPort -Filter "Name ='$VPortPerfmonInstanceName'"
if ($wmiPerfCounter -ne $null)
{