首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将字符串传递给PowerShell脚本时意外的令牌'}‘

将字符串传递给PowerShell脚本时意外的令牌'}‘
EN

Stack Overflow用户
提问于 2022-09-21 19:07:53
回答 1查看 61关注 0票数 -1

我有一个脚本,它接受两个字符串,每个字符串都由'|-|'分隔。第二个字符串可以包含'以外的任何字符。

要调用脚本,我需要:

代码语言:javascript
运行
复制
PowerShell .\script.ps1 -arg1 'Hello|-|World' -arg2 'random|-|dwua0928]43d}'

它会在字符}上引发错误。我怎么解决这个问题?

编辑:它还与分隔符中的-有问题,并引发相同的错误:

代码语言:javascript
运行
复制
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.

它在|-|的每一个实例中都这样做。如果我将分隔符更改为/-/,则使用括号执行此操作。

代码语言:javascript
运行
复制
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.

脚本的开头是:

代码语言:javascript
运行
复制
Param
(
    [switch] $enable,
    [string] $arg1,
    [string] $arg2
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-21 23:58:06

  • 一般不需要从PowerShell.

内部调用PowerShell CLI (powershell.exe for Windows PowerShell,pwsh for PowerShell (Core) 7+)

代码语言:javascript
运行
复制
- 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

代码语言:javascript
运行
复制
- 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.
代码语言:javascript
运行
复制
- 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

当使用

  • 时,PowerShell的命令行处理首先删除任何语法(即未转义) "字符的所有后续参数,即从"..."-enclosed参数中,将产生的令牌与空格连接,然后将得到的字符串解释为PowerShell代码。

  • 在您的例子中,使用'...'引用参数的事实是不相关的:

代码语言:javascript
运行
复制
- 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|-|Worldrandom|-|dwua0928]43d},都不包含空格,因此它们被放置在powershell.exe的命令行中(但请注意,即使它们包含空格并因此被"..."括起来,PowerShell的命令行处理也会删除"字符)。

最终的结果是powershell.exe子进程试图执行以下PowerShell代码,这些代码与您问题中显示的错误不出所料地中断了:

代码语言:javascript
运行
复制
# !! Note the absence of quoting around the -arg1 and -arg2 values.
.\script.ps1 -arg1 Hello|-|World -arg2 random|-|dwua0928]43d}

您可以很容易地引发您从PowerShell会话中看到的以下错误:

代码语言:javascript
运行
复制
# -> Error "Missing expression after unary operator '-'."
Write-Output Hello|-|World
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73805784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档