我正在尝试使用 PowerShell 脚本从 ipconfig 的输出中提取我的本地网络 IP 地址。我已经使用临时输出文件让它工作,但想消除该步骤,只在脚本中使用一个变量。我无法弄清楚为什么不使用临时输出文件的脚本不起作用。任何帮助将非常感激。
这是两个脚本。首先,工作版本:
$input_path = 'C:\Users\Will\Code\Scripts\tmp.txt'
$regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56." > tmp.txt
$ipAddress = select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
# Pop up an alert window with the IP address
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup($ipAddress,0,"Done",0x1)
# Copy the IP address to the clipboard
$ipAddress | CLIP
现在几乎相同但仅使用变量的非工作版本:
$regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
$input = ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56."
#$input > inputVar.txt
$ipAddress = select-string -inputObject $input -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
# Pop up an alert window with the IP address
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup($ipAddress,0,"Done",0x1)
# Copy the IP address to the clipboard
$ipAddress | CLIP
我正在使用 select-string 来查找包含“IPv4”的行,这些行通过管道传输到另一个对 select-string 的调用中,以消除包含我的虚拟机 IP 地址的行。如果我在非工作脚本的第 6 行启用测试输出并将其与工作脚本中的“tmp.txt”文件进行比较,我可以看到到目前为止,两个脚本的工作方式相同。
此外,两个脚本中的第 10-15 行是相同的。所以我相信唯一有问题的行是#8。
我已经尝试了所有我能想到的方法,但总是得到相同的结果(“IPv4”而不是 IP 地址) 第一个脚本按预期工作并给了我 IP。
这两个选项似乎都应该起作用(并且可能是我尝试过的所有选项中最合乎逻辑的),但都没有给我 IP:
$ipAddress = select-string -inputObject $input -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
和
$ipAddress = $input | select-string -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
我什至尝试消除所有额外的步骤,只制作一个像这样的长管道:
$ipAddress = ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56." | select-string -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value }
并像这样复制到剪贴板:
ipconfig | select-string "IPv4" | select-string -notmatch "192.168.56." | select-string -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } | CLIP
但似乎没有任何效果。
我在哪里做错了?
谢谢!
更新 2015 年 3 月 13 日下午 6:30
我正在运行 Windows 7 64 位
这是 $PSVersionTable 的输出:
名称 值
---- -----
CLRVersion 2.0.50727.5485
BuildVersion 6.1.7601.17514
PSVersion 2.0
WSManStackVersion 2.0
PSCompatibleVersions {1.0, 2.0}
SerializationVersion 1.1.0.1
PSRemotingProtocolVersion 2.1
是的,我的最终目标只是获得 IP。真的,我只是想找到一种快速的方法来做到这一点。我想我在技术上并不需要 IP 本身来用于我使用它的目的,但是一旦我进入它,我就非常着迷于试图解决剩下的问题,只是出于好奇。
感谢所有的帮助!
在休息了大约 10 年之后,我才重新开始编码,而且我已经超级生疏了。
我不确定为什么我的非工作版本对我有用,如果它对你有用。当我运行它时,我在弹出窗口和剪贴板上得到“IPv4”,而不是IP(我在使用临时文件的工作版本中得到。
我将把这些正则表达式更改集成到我当前的脚本中,并使用你的 gwmi 解决方案,Matt。初始运行对我没有任何回报,但我会玩弄它并让你知道。
让我知道您是否有更多的想法,为什么根据这些更新的信息,带有变量的版本在我的端不能正常工作。这对我来说完全莫名其妙。
再次感谢!
更新 2015 年 3 月 13 日晚上 10:27
这是更新后 $PSVersionTable 的输出:
名称 值
---- -----
PSVersion 4.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion
4.0.30319.18444 BuildVersion 6.3.9600.16406
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0}
PSRemotingProtocolVersion 2.2
如果我在 PowerShell 中逐段运行管道命令,会发生以下情况。如您所见,它似乎在第 14 行中断:$ipAddress = $ipAddress | % { $_.Matches }
无论如何,认为这可能会有所帮助:
PS C:\Users\Will\Code> $regex = '\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'
PS C:\Users\Will\Code> $inputVar = ipconfig | select-string "IPv4" | select-string -notmatch "192\.168\.56\."
PS C:\Users\Will\Code> $inputVar
IPv4 Address. . . . . . . . . . . : 192.168.0.105
PS C:\Users\Will\Code> $ipAddress = select-string -inputObject $inputVar -Pattern $regex -AllMatches
PS C:\Users\Will\Code> $ipAddress
IPv4 Address. . . . . . . . . . . : 192.168.0.105
PS C:\Users\Will\Code> $ipAddress = $ipAddress | % { $_.Matches }
PS C:\Users\Will\Code> $ipAddress
Groups : {IPv4}
Success : True
Captures : {IPv4}
Index : 3
Length : 4
Value : IPv4
PS C:\Users\Will\Code> $ipAddress = $ipAddress | % { $_.Value }
PS C:\Users\Will\Code> $ipAddress
IPv4
PS C:\Users\Will\Code>