简体   繁体   中英

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. 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

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM