我有一个脚本,它接受两个字符串,每个字符串都由'|-|'
分隔。第二个字符串可以包含'
以外的任何字符。
要调用脚本,我需要:
PowerShell .\script.ps1 -arg1 'Hello|-|World' -arg2 'random|-|dwua0928]43d}'
它会在字符}
上引发错误。我怎么解决这个问题?
编辑:它还与分隔符中的-
有问题,并引发相同的错误:
At line:1 char:54
+ .\Script.ps1 -arg2 <redacted>|-|<redacted> ...
+ ~
Missing expression after unary operator '-'.
At line:1 char:53
+ .\Script.ps1 -arg1 <redacted>|-|<redacted> ...
+ ~
Expressions are only allowed as the first element of a pipeline.
它在|-|
的每一个实例中都这样做。如果我将分隔符更改为/-/
,则使用括号执行此操作。
At line:1 char:168
+ ... <redacted>/-/<redacted>!OW}:Lj<redacted> ...
+ ~
Unexpected token '}' in expression or statement.
At line:1 char:181
+ ... /-/<redacted>cO0}e]k<redacted> ...
+ ~
Unexpected token '}' in expression or statement.
脚本的开头是:
Param
(
[switch] $enable,
[string] $arg1,
[string] $arg2
)
发布于 2022-09-21 23:58:06
内部调用PowerShell CLI (powershell.exe
for Windows PowerShell,pwsh
for PowerShell (Core) 7+)
- Assuming you're running _Windows PowerShell_ (or, to generalize, the _same edition_ of PowerShell), you can invoke your script _directly_:
.\script.ps1 -arg1‘Hello’-arg2‘随机数--dwua0928]43d}’
如果您仍然想调用CLI --这很昂贵,因为它涉及创建子进程,并且导致类型保真度丢失--请使用
{ ... }
),但请注意,这只在PowerShell内部起作用(尽管这两个PowerShell版本都能工作,即您可以从Windows调用pwsh.exe
,从PowerShell (Core) 7+调用powershell.exe
)powershell { .\script.ps1 -arg1‘Hello\x World’-arg2‘随机\x{##**$} }
如果要从
-File
CLI参数,而不是(在Windows PowerShell中隐含的) -Command
参数:powershell -File .\script.ps1 -arg1“哈啰--世界”-arg2 -arg2
- While this works from inside PowerShell too, the disadvantage compared to the `{ ... }` approach is that the output is only ever _text_, whereas the `{ ... }` approach is - within limits - capable of preserving the original data types - see [this answer](https://stackoverflow.com/a/59180367/45375) for more information.
- In order to make a `-Command`-based command line work, enclose the PowerShell code in `"..."` _as a whole_, in which case the embedded `'...'` strings _are_ passed as such:
powershell -Command ".\script.ps1 -arg1‘Hello\’-arg2‘随机区-dwua0928 0928]43d}’“
有关PowerShell CLI的全面概述,请参阅this post。
至于,你尝试过
使用-File
). ( -File
(-f
)或-Command
(-c
) )调用powershell.exe
,默认为-Command
(请注意,与之相反,pwsh
,PowerShell (核心) 7+ CLI )默认为-Command
当使用
"
字符的所有后续参数,即从"..."
-enclosed参数中,将产生的令牌与空格连接,然后将得到的字符串解释为PowerShell代码。'...'
引用参数的事实是不相关的:- PowerShell invariably translates the original quoting into `"..."` quoting when calling _external programs_ (as they can only be expected to `"..."` quoting, not also `'...'` quoting)
- However, it only uses `"..."` quoting _if needed_, which is solely based on whether the argument at hand _contains spaces_.
您的论点,即逐字Hello|-|World
和random|-|dwua0928]43d}
,都不包含空格,因此它们被放置在powershell.exe
的命令行中(但请注意,即使它们包含空格并因此被"..."
括起来,PowerShell的命令行处理也会删除"
字符)。
最终的结果是powershell.exe
子进程试图执行以下PowerShell代码,这些代码与您问题中显示的错误不出所料地中断了:
# !! Note the absence of quoting around the -arg1 and -arg2 values.
.\script.ps1 -arg1 Hello|-|World -arg2 random|-|dwua0928]43d}
您可以很容易地引发您从PowerShell会话中看到的以下错误:
# -> Error "Missing expression after unary operator '-'."
Write-Output Hello|-|World
https://stackoverflow.com/questions/73805784
复制相似问题