Parameters with double quotes are not properly passed to Scriptblock by ArgumentList

让人想犯罪 __ 提交于 2019-12-04 07:09:29

You could do something like this

$remoteAddress = "some-pc"
$deploymentCommand = "D:\some path\Command.exe"
$deploymentPackages = @('package - one - external', 'package - two - external', 'package - three - internal')
$remoteScript = {
    param( $deployCmd, $deployPackage )
    & $deployCmd "-package:$deployPackage" -action:doit
}

foreach ($deploymentPackage in $deploymentPackages)
{
   invoke-command -ComputerName $remoteAddress -ScriptBlock $remoteScript -ArgumentList $deploymentCommand,$deploymentPackage
}

This bundles -package:<some string here> into a single argument when passed to your executable, which is the same as doing something like -package:"aaa bbb ccc" in cmd.exe.

I assume you don't want literal quotes passed to the exe, just for -package:<some string here> to be a single argument regardless of spaces in <some string here>

If you want literal quotes to be passed to the exe, use the above code with

& $deployCmd "-package:`"$deployPackage`"" -action:doit

You can fix this by using single quotes to wrap your strings. With single quotes, the content between the quotes will be untouched(variables won't expand and signs like quotes will be kept). E.g.

PS > '"this is a test"'
"this is a test"

So to fix your script, try replacing your deploymentpackages array with this:

$deploymentPackages = @('"package - one - external"', '"package - two - external"', '"package - three - internal"')

Try using single quotes around the string with the double quotes. I simplified the script a little bit to just write the string instead of running it.

$remoteAddress = "some-pc";
$deploymentPackages = @('"package - one - external"', '"package - two - external"', '"package - three - internal"');

foreach ($deploymentPackage in $deploymentPackages)
{
    invoke-command -ComputerName $remoteAddress -ScriptBlock {
        param ($deploymentPackage) write-host ("-package:{0} -action:doit" -f $deploymentPackage); 
    } -ArgumentList   $deploymentPackage;
}
Yonnatan Bar

Use """. For example,

$Var = "One"

$Var will display One.

While """$Var""" will display One

I have encountered the same problem when trying to send an email from a PowerShell script called from a DOS script. I have lost some time to find the following solution.

DOS Script

set MSG="US-TrackingFile has been copied to Z: drive"
call SendMail.bat %MSG%

SendMail.bat DOS script

PowerShell.exe .\SendMail.ps1 '"%1"'

SendMail.ps1 PowerShell script

$msg = $args[0]

Send-MailMessage
  -to "bernard_schleich@atos.net" 
  -subject "$msg" 
  -smtpserver emearelay.ec.company.com 
  -from "windows.system@company.com" 

The following solution without calling intermediary DOS script work also

set MSG="\"US-TrackingFile has been copied to Z: drive\""
PowerShell.exe .\SendMail.ps1 %MSG%

but is a little tricky because you must add \" at start and end of each message.

In all cases, the title of my message is correctly displayed without double quote at start and end of message :-)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!