powershell - 如何去除变量中除数字之外的所有内容?

标签 powershell

我正在尝试通过 PowerShell 自动设置 IP 地址,我需要找出我的 interfaceindex 编号。

我的想法是这样的:

$x = ( Get-NetAdapter |
       Select-Object -Property InterfceName,InterfaceIndex |
       Select-Object -First 1 |
       Select-Object -Property Interfaceindex ) | Out-String

这将输出:

InterfaceIndex
--------------
             3

Now the problem, when I try to grab only the number using:

$x.Trim.( '[^0-9]' )

它仍然留下“InterfaceIndex”和下划线。这会导致我的脚本的下一部分出错,因为我只需要数字。

有什么建议吗?

最佳答案

这将完成您的工作:

( Get-NetAdapter | Select-Object -Property InterfceName,InterfaceIndex | Select-Object -First 1 | Select-Object -Property Interfaceindex).Interfaceindex

实际上你不需要两次选择属性:这样做:

( Get-NetAdapter |Select-Object -First 1| Select-Object -Property InterfceName,InterfaceIndex).Interfaceindex

关于powershell - 如何去除变量中除数字之外的所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45146262/

相关文章:

azure - 在Azure powershell中将输出设置为表格式而不是json

powershell - 解压缩后仅复制某些文件类型

windows - 在 cmd 中将 powershell 脚本作为自定义命令运行

powershell - 在powershell中显示所有特殊字符/转义字符

windows - 如何从查看 ProgramFiles (x86) 的命令行启动 IIS Express?

powershell - 使用 Powershell 执行权限更改 ala chmod/attrib

powershell - 保存自定义 $variables 以在不同的 PS session 中访问

powershell - 使用 PowerShell Azure 资源管理器 cmdlet 将现有 vhd 添加到 Azure VM

powershell - 使用 Powershell 命令将计算机添加到事件日志读取器组

powershell - 参数模式下的未引用标记涉及变量引用和子表达式:为什么有时将它们拆分成多个参数?