powershell - Powershell如何使用特殊字符传递参数
问题描述
我正在尝试将变量作为参数传递给 powershell 脚本。变量有一些特殊字符,调用 ps1 脚本失败。
在这里,我只是创建了一个示例来显示问题。
PS C:\>$pass1 = '?q$*9-W$wcx)O.Ra)X-&6'
PS C:\>powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command 'echo '$pass1''
错误:
At line:1 char:26
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:32
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:37
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation
marks ("&") to pass it as part of a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
解决方案
您的脚本有语法错误,参数/变量的实际值似乎没有问题。
这是有语法错误的行:
powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command 'echo '$pass1''
这是您的问题所在:
'echo '$pass1''
你实际上在这里传递了三件事......
'echo '
$pass1
''
第二个单引号正在退出字符串。
您需要根据需要使用它们的方式在单字符串和双字符串之间交替:
$pass1 = '?q$*9-W$wcx)O.Ra)X-&6'
powershell -Command "echo '$pass1'"
推荐阅读
- sql - 如何在 sql server 中交替使用 0 和 1 值?
- java - 可以从多个模块访问包 javax.xml.stream:
, java.xml - imagemagick - 使用 Imagemagick 创建随机无缝图案
- javascript - 更改 react js 中日历组件的样式
- c# - 在 selenium C# 中面临来自 html 表单的问题和选择下拉值
- angular - 在 Angular 的 Jasmine 测试用例中配置 TestBed 的推荐方法是什么?
- javascript - 如何使用来自 ReactJS 的 MaskedInput 为 CPF 或 CNPJ 设置掩码?
- javascript - 如何重新渲染具有不同值的 PayPal 智能按钮?
- wordpress - 单击项目时关闭移动菜单(elementor kit,wordpress)
- reactjs - 如何将道具传递给将参数作为不可变字符串的组件属性