不知道为什么,但我的if语句没有按照正确的方向进行,在下面的示例中,一个接口被禁用,但它仍然显示True值,但它应该显示False值。
我有以下接口:
get-netadapter
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
Embedded FlexibleLOM ...2 HPE FlexFabric 10Gb 4-port 536FLR-...#2 10 Disabled 08-xxx-62 10 Gbps
Embedded FlexibleLOM ...1 HPE FlexFabric 10Gb 4-port 536FLR-...#4 11 Up 08-xxx-60 10 Gbps
您可以看到其中一个已禁用。这是脚本:
$vmswitch1stinterface = (Get-VMSwitchTeam -Name "Teaming").NetAdapterInterfaceDescription|select -first 1
$vmswitch2ndinterface = (Get-VMSwitchTeam -Name "Teaming").NetAdapterInterfaceDescription|select -last 1
$1stinterfacestatus = Get-NetAdapter -InterfaceDescription $vmswitch1stinterface|select Status
$2ndinterfacestatus = Get-NetAdapter -InterfaceDescription $vmswitch2ndinterface|select Status
if (($1stinterfacestatus -and $2ndinterfacestatus) -eq 'up')
{
Write-Host "OK: NIC ports are up (VMSwitch)"
Exit 0
} else
{
Write-Host "WARNING: An interface is down! Please check in the list which one is not UP:"
Get-NetAdapter -InterfaceDescription $vmswitch1stinterface|Select-Object InterfaceDescription, Status
Get-NetAdapter -InterfaceDescription $vmswitch2ndinterface|Select-Object InterfaceDescription, Status
Exit 1
}
为了便于确定,数值如下:
$vmswitch1stinterface: HPE FlexFabric 10Gb 4-port 536FLR-T Adapter #4
$vmswitch2ndinterface: HPE FlexFabric 10Gb 4-port 536FLR-T Adapter #2
$1stinterfacestatus
Status
------
Up
$2ndinterfacestatus
Status
------
Disabled
这个脚本的结果是以下不正确的:
OK: NIC ports are up (VMSwitch)
我在逻辑中遗漏了什么?
$1stinterfacestatus
和$2ndinterfacestatus
是PSCustomObjects,并且可以具有属性。在您的情况下,两个对象都具有Status
属性,您可以使用.
表示法访问该属性。(即$yourPSCustomObject.Status
(。
我想你打算测试两个接口是否都启动了,你可以通过来完成
if (($1stinterfacestatus.status -eq 'up' -and $2ndinterfacestatus.status -eq 'up')) {
Write-Output "Both interfaces are up"
}