使用PowerShell进行网络订单查询

时间:2017-11-05 13:28:12

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0 powershell-remoting

我已经编写了一个powershell脚本来查询网卡绑定顺序,脚本将以Vethernet格式查看网卡绑定顺序,如果此顺序正确则存储,那么它将以绿色打印输出,否则将以黄色打印。 此外,我在这里又增加了一个条件,即如果在绑定顺序中没有找到存储NIC,它应该看到Vethernet是第一顺序,如果顺序正确,它应该打印为绿色,否则它应该是黄色。但是我的剧本似乎有些问题。

第一条if条件线工作正常,但第二条条件线条工作正常。我尝试更改订单,即使在更改订单后,它也会将颜色打印为绿色。

任何人都可以帮我纠正这个脚本吗?

$results = Invoke-Command -ComputerName $ComputerName {(Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").Bind | ForEach-Object {
  $guid = ($_ -split '\\')[2]
  Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE GUID='$guid'" |
    Select-Object -ExpandProperty NetConnectionID
}}

$adapteresult = $results -join","

if (($adapteresult.Contains('Storage') -and ($adapteresult -match 'vEthernet\s+\([^)]+\),storage')))
{
Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
}
elseif ($adapteresult -NotContains "Storage" -and ($adapteresult -match 'vEthernet\s+\([^)]+\),'))
{
Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
}
else
{
Add-Content $report "<td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td>"
}

1 个答案:

答案 0 :(得分:0)

更准确地说,您可以使用 $ Null

进行检查

只需添加OR条件,如:

if (($adapteresult -match 'vEthernet\s+\([^)]+\),storage') -or ($adapteresult -eq $null)) 

根据更新,为什么要使用多个if else:

if( ($adapteresult -match 'vEthernet\s+\([^)]+\),storage') -or ($adapteresult -match 'vEthernet*\s+\([^)]+\),') )
{
Add-Content $report "<td bgcolor= 'Aquamarine'  height='25' align=center><B>$adapteresult</B></td>"
}
else
{
Add-Content $report "<td bgcolor= 'Yellow'  height='25' align=center><B>$adapteresult</B></td>"
}