ipconfig | Select-String "IPv4 Address"
返回如下内容:
IPv4 Address. . . . . . . . . . . : 192.168.1.50
IPv4 Address. . . . . . . . . . . : 172.20.112.1
IPv4 Address. . . . . . . . . . . : 192.168.208.1
假设我想用空字符串替换“IPv4 地址。。。。。。。。。。。。。:”-代替。我该怎么做?以下是我尝试的方法:
ipconfig | Select-String "IPv4 Address" -replace "IPv4 Address. . .",""
这给了我以下错误:
Select-String : A parameter cannot be found that matches parameter name 'replace'.
At line:1 char:41
+ ipconfig | Select-String "IPv4 Address" -replace "IPv4 Address. . .", ...
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-String], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand
有任何想法吗?
答案1
你需要重写它来执行-replace
操作。这样它就不必寻找具有Select-String
功能。
正如错误所表明的:Select-String : A parameter cannot be found that matches parameter name 'replace'
这正是问题所在——Select-String
没有-Replace
参数。
有效语法
(ipconfig | Select-String "IPv4 Address") -replace "IPv4 Address. . .",""
或者
(ipconfig | Select-String "IPv4 Address").Tostring().Replace("IPv4 Address. . .","")
或者将命令的第一部分保存到变量,然后运行代替手术
$s = ipconfig | Select-String "IPv4 Address"
$s -replace "IPv4 Address. . .",""
输出
. . . . . . . . : X.X.X.X