简体   繁体   English

无法将值从命令行传递给对象-Powershell参数

[英]Unable to pass value from command line to Object - Powershell Paramter

I am trying to figure out how to pass a paramter that contains special characters in Powershell to an Object inside my function. 我试图弄清楚如何将Powershell中包含特殊字符的参数传递给函数内的对象。 Here is an example of my code. 这是我的代码示例。

 function a{
 param(
 [string]$string    
 )
 #convert to URL encoding here
 #Query API
 #Return JSON values
 }

Now I type this in Powershell 现在我在Powershell中输入

 PS> a  foo(foo; bar) foo/bar ver1.0

And it fires on an error for ";" 并为“;”错误触发 and then ")" being part of the string 然后“)”成为字符串的一部分

Here is the error: 这是错误:

 At line:1 char:32
 + a  foo(foo; bar) foo/bar ver1.0
 +                                ~
 Missing closing ')' in expression.
 At line:1 char:41
 + a  foo(foo; bar) foo/bar ver1.0
 +                                         ~
 Unexpected token ')' in expression or statement.
 + CategoryInfo          : ParserError: (:) [],        ParentContainsErrorRecordException
 + FullyQualifiedErrorId : MissingEndParenthesisInExpression

You have two options the way I see it. 我有两种选择。

Use single quotes: 使用单引号:

a 'foo(foo; bar) foo/bar ver1.0'

Or escape all the special characters: 或转义所有特殊字符:

a foo`(foo`;` bar`)` foo/bar` ver1.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell:如何将字符串作为字符串从命令行传递到PowerShell脚本 - PowerShell: how to pass , as a string from command line to a powershell sciript 如何将当前的powershell管道对象“$ _”作为命令行arg传递? - How to pass the current powershell pipe-object “$_” as a command line arg? 从PowerShell运行时,如何在MSBuild命令行上传递包含分号的属性值? - How do I pass a property value containing a semicolon on the MSBuild command line when running it from PowerShell? powershell传递命令行参数 - powershell pass command line arguments 从命令行将多个参数传递给Powershell函数? - Pass multiple parameter to Powershell function from command line? Powershell-将数组作为参数传递给从命令行调用的可执行文件 - Powershell - Pass an array as argument to an executable called from command line 如何从命令行运行 Powershell 脚本并将目录作为参数传递 - How to run a Powershell script from the command line and pass a directory as a parameter 如何从命令行将带括号的参数传递给 Powershell? - How to pass a parameter with parentheses to Powershell from command line? 如何从 PowerShell 命令行转义管道字符以传递到非 PowerShell 命令 - How to escape pipe character from PowerShell command line to pass into non-PowerShell command 无法将命令正确传递到Powershell中的远程计算机 - Unable to pass command properly to remote computers in Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM