-eq 函数可以与变量进行比较吗
Can the -eq function compare against a variable
看来 -eq 只能与文字进行比较。我正在尝试与这样的变量进行比较。此代码无效
> $macaddr = ((getmac /v /fo csv | findstr ASIX) -split "," )[2]
> $echo $macaddr
"00-01-02-03-04-05"
> $adapter = Get-NetAdapter | ? {$_.MacAddress -eq $macaddr}
> echo $adapter
>
如果我将比较更改为文字,它似乎工作正常。
> $adapter = Get-NetAdapter | ? {$_.MacAddress -eq "00-01-02-03-04-05"}
> echo $adapter
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
Ethernet 3 ASIX AX88179 USB 3.0 to Gigabit Ethe... 12 Up 00-01-02-03-04-05 100 Mbps
我应该如何处理比较?
/fo csv
选项使 getmac.exe
引用 mac 地址,因此文字字符串值不是 00-01-02-03-04-05
,而是 "00-01-02-03-04-05"
(包括引用标记),它们显然不同:
> '00-01-02-03-04-05' -eq '"00-01-02-03-04-05"'
False
要么使用 ConvertFrom-Json
到 re-parse 输出:
$macAddr = getmac /v /fo csv |ConvertFrom-Csv |Where-Object 'Network Adapter' -like '*ASIX*' |Select-Object -Expand 'Physical Address'
或使用Trim()
方法从原始输出中删除引号:
$macaddr = ((getmac /v /fo csv | findstr ASIX) -split "," )[2].Trim('"')
看来 -eq 只能与文字进行比较。我正在尝试与这样的变量进行比较。此代码无效
> $macaddr = ((getmac /v /fo csv | findstr ASIX) -split "," )[2]
> $echo $macaddr
"00-01-02-03-04-05"
> $adapter = Get-NetAdapter | ? {$_.MacAddress -eq $macaddr}
> echo $adapter
>
如果我将比较更改为文字,它似乎工作正常。
> $adapter = Get-NetAdapter | ? {$_.MacAddress -eq "00-01-02-03-04-05"}
> echo $adapter
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
Ethernet 3 ASIX AX88179 USB 3.0 to Gigabit Ethe... 12 Up 00-01-02-03-04-05 100 Mbps
我应该如何处理比较?
/fo csv
选项使 getmac.exe
引用 mac 地址,因此文字字符串值不是 00-01-02-03-04-05
,而是 "00-01-02-03-04-05"
(包括引用标记),它们显然不同:
> '00-01-02-03-04-05' -eq '"00-01-02-03-04-05"'
False
要么使用 ConvertFrom-Json
到 re-parse 输出:
$macAddr = getmac /v /fo csv |ConvertFrom-Csv |Where-Object 'Network Adapter' -like '*ASIX*' |Select-Object -Expand 'Physical Address'
或使用Trim()
方法从原始输出中删除引号:
$macaddr = ((getmac /v /fo csv | findstr ASIX) -split "," )[2].Trim('"')