PowerShell stripping double quotes from command line arguments

前端 未结 6 773
不思量自难忘°
不思量自难忘° 2020-11-28 05:42

Recently I have been having some trouble using GnuWin32 from PowerShell whenever double quotes are involved.

Upon further investigation, it appears PowerShell is str

6条回答
  •  悲哀的现实
    2020-11-28 06:33

    Relying on the CMD to shell out the issue as indicated in the accepted answer didn't work for me as double quotes were still stripped out when calling the CMD executable.

    The good solution for me was to structure my command line as an array of strings instead of a single full string containing all the arguments. Then simply pass that array as the arguments for the binary invocation:

    $args = New-Object System.Collections.ArrayList
    $args.Add("-U") | Out-Null
    $args.Add($cred.UserName) | Out-Null
    $args.Add("-P") | Out-Null
    $args.Add("""$($cred.Password)""")
    $args.Add("-i") | Out-Null
    $args.Add("""$SqlScriptPath""") | Out-Null
    & SQLCMD $args
    

    In that case, double quotes surrounding arguments are properly passed to the invoked command.

    If you need, you can test and debug it with EchoArgs from the PowerShell Community Extensions.

提交回复
热议问题