Powershell代码删除匹配的文本,给出错误

问题描述 投票:0回答:1

我有这个代码:

powershell -Command "(Get-Content C:\users\Public\ITCMD\Toggler\settings.ini) | Where-Object {$_ -notmatch "\b!Friend!\b"} | Set-Content C:\users\Public\ITCMD\Toggler\settings.ini"

我订了!朋友!到文本值。该脚本正在吐出这些错误:

At line:1 char:86
+ ... blic\ITCMD\Toggler\settings.ini) | Where-Object {$_ -notmatch \buhgft ...
+                                                                  ~
You must provide a value expression following the '-notmatch' operator.
At line:1 char:87
+ ... \Toggler\settings.ini) | Where-Object {$_ -notmatch \buhgft\b} | Set- ...
+                                                         ~~~~~~~~~
Unexpected token '\buhgft\b' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedValueExpression

Errors

想知道它为什么这样做?谢谢!

powershell batch-file replace
1个回答
2
投票

您的双引号被cmd解释为命令的结尾并导致PowerShell的解析问题。如果用单引号替换它们,它将修复解析问题:

powershell -Command "(Get-Content C:\users\Public\ITCMD\Toggler\settings.ini) |^
    Where-Object {$_ -notmatch '\b!Friend!\b'} |^
    Set-Content C:\users\Public\ITCMD\Toggler\settings.ini"
© www.soinside.com 2019 - 2024. All rights reserved.