passing parameters to script from powershell.exe -


i have script this:

param( [string]$root, [string]$bin, [string]$out, [string]$zdir )   echo "args..." echo "root: $root", "zdir: $zdir", "out: $out", "bin: $bin" 

i invoke follows:

powershell.exe -nologo -noprofile -file "c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompass\outlookcompass\zip.ps1" -root "c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompass\outlookcompass\" -zdir "zip" -out "output.zip" -bin "c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompass\outlookcompass\bin\debug\"  

but output quite contrary:

c:\code\misc>powershell.exe -nologo -noprofile -file "c:\users\arun_jayapal\docu ments\visual studio 2013\projects\outlookcompass\outlookcompass\zip.ps1" -root " c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompass\outlo okcompass\" -zdir "zip" -out "output.zip" -bin "c:\users\arun_jayapal\documents\ visual studio 2013\projects\outlookcompass\outlookcompass\bin\debug\" args... root: c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompas s\outlookcompass" -zdir zip -out output.zip -bin c:\users\arun_jayapal\document s\visual zdir: out: 2013\projects\outlookcompass\outlookcompass\bin\debug" bin: studio 

duncan's helpful answer contains crucial pointer (and helpful background information well): \" @ end of parameter values interpreted escaped ", causes parameter boundaries misinterpreted.

your options are:

  • use \\" instead of \" @ end of each parameter value, duncan suggests (e.g., "c:\foo bar\\")
  • omit trailing \ paths altogether (e.g., "c:\foo bar" instead of "c:\foo bar\")
  • if want avoid having modify parameter values, use following, single-quoted solution (verified on v3).

single-quoted solution:

this assumes you're calling cmd.exe, whether batch file or @ command prompt (inside powershell, use & <script> ...)

  • use single quotes instead of double quotes
  • use -command parameter instead of -file
  • prefix script path & (escaped ^ have cmd.exe treat literal)
powershell.exe -noprofile -command ^& 'c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompass\outlookcompass\zip.ps1' -root 'c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompass\outlookcompass\' -zdir 'zip' -out 'output.zip' -bin 'c:\users\arun_jayapal\documents\visual studio 2013\projects\outlookcompass\outlookcompass\bin\debug\' 
  • this works, because using -command (and powershell's & (call) operator invoke script path has embedded spaces), rest of command line can quoted powershell-style single-quoted strings aren't subject interpretation (not c-style argument parsing underlies powershell's own startup command-line parsing, exception of embedded " chars. - see below).

  • the characters need escaping approach are:

    • use '' embed literal ' inside parameter.
    • use \" embed literal " inside parameter.
    • if need escape % chars. protect them interpretation cmd.exe part of environment-variable references (which you'd have irrespective of whether use single- or double-quoted strings), see this answer of mine.
  • note in order execute script in current directory must .\-prefix script filename, inside powershell.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -