我想通过PowerShell自动设置一个IP地址,我需要找出我的接口索引号是什么。如何去除变量中除数字之外的所有内容?
什么我已经做了是这样的:
$x = (Get-NetAdapter |
Select-Object -Property InterfceName,InterfaceIndex |
Select-Object -First 1 |
Select-Object -Property Interfaceindex) | Out-String
这将输出:
InterfaceIndex -------------- 3
现在的问题,当我尝试用抢仅数:
$x.Trim.('[^0-9]')
它仍然保留“InterfaceIndex”和下划线。这导致我的脚本的下一部分出错,因为我只需要这个数字。
有什么建议吗?
或者,将$ x保留为PowerShell对象'$ x = Get-NetAdapter | Select-Object -Property InterfaceName,InterfaceIndex | Select-Object -First 1'并将其引用为'$ x.InterfaceIndex'。 –
你应该总是在左边的 – 4c74356b41