Powershell - passing parameters to script executed via piping [duplicate] - powershell

I am running powershell script over ssh as ssh user#host "powershell -Comand - < script.ps1. It works as expected as long as I start passing arguments.
When I put it as powershell -Command - my args it fails (as documented)
'-' was specified with the -Command parameter; no other arguments to -Command are permitted.
While the other way around powershell my args -Command - it fails with:
The term 'my' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included
, verify that the path is correct and try again.
At line:1 char:3
+ my <<<< args -Command -
+ CategoryInfo : ObjectNotFound: (my:String) [], CommandNotFoundE
xception
+ FullyQualifiedErrorId : CommandNotFoundException
I intend to put in arbitrary list of parameter without any parsing.
Edit:
As I investigate further, it seems I am doing something wrong even when the command is specified explicitly:
(local bash) $ echo '\n' | ssh -i master-key Admin#10.8.55.78 '$SYSTEMROOT/System32/WindowsPowerShell/v1.0/powershell' -Command 'Write-Host \$\(\$args.Count\)' "my" "args"
0 my args
It seems that passes no arguments but they are printed on console for some reason. Avoiding the ssh does not seems to change anything:
(cygwin) $ $SYSTEMROOT/System32/WindowsPowerShell/v1.0/powershell -Command 'Write-Host $($args.Count)' "my" "args"
0 my args

You can't do that directly, but I think this can be done, if you wrap your script in scriptblock and pass arguments to it:
echo "& { $(cat script.ps1) } 'my' 'args'" | ssh user#host "powershell -Command"
Since -Command parameter can't handle multiline strings, there is a way to pass it in (though not via standard input) using Base64 encoded value of -EncodedCommand parameter, but it's ugly:
ssh user#host "powershell -encodedcommand $((echo "& {"; cat script.ps1 ; echo "} 'my' 'args'") | iconv -f ascii -t utf-16le | base64 -w0 ; echo -e "\n")

This one works as expected:
script=$(cat <<-'SCRIPT'
{
$a=$Args[0];
$b=$Args[1];
# Do not enclose $script into "" to avoid this comment spread till the EOL
Write-Host "This is 'a': $a";
Write-Host "This is 'b': $b";
} # <- call as [[[ -c "& { $script } ... " ]]] if you ommit braces '{}' here
SCRIPT
)
a="THE VALUE OF THE \"a\""
b="B B B B"
powershell -nologo -executionpolicy bypass -c "& $script '$a' '$b'"
output:
> This is 'a': THE VALUE OF THE "a"
> This is 'b': B B B B

Related

String comparison not working in powershell

I am trying an if else condition in powershell using string comparison. I tried as per documentation using -eq operator. But getting below error. Here "Build.Reason" is a predefined variable. Not sure why its looking for cmdlet name for variable.
Write-Host "$(Build.Reason)"
if ($(Build.Reason) -eq "Manual" ) {
$temp = "https://url/api/qualitygates/project_status?&pullRequest=$(Build.Reason)"
Write-Host "Manual"
} else {
Write-Host "CI"
}
Error
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a\_temp\d7af16d6-ce3e-4dec-a636-9447962fdac4.ps1'"
Manual
Manual : The term 'Manual' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\a\_temp\d7af16d6-ce3e-4dec-a636-9447962fdac4.ps1:7 char:5
+ if (Manual -eq "Manual" ) {
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Manual:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : CommandNotFoundException
It looks like $(Build.Reason) is a macro-style value provide by a CI system (it is not a PowerShell construct), which is expanded to become a literal part of the code before PowerShell sees it.
Therefore, if this value is to be treated as a string in the resulting PowerShell code, you need to quote it; e.g.:
if ("$(Build.Reason)" -eq "Manual") { # ...
Note that if there's a chance that $(Build.Reason) expands to a value with embedded " characters, they would have to be escaped as `". Similarly, if the value contains embedded $ chars., single-quoting should be used, which may then require escaping embedded single quotes as ''.
If this escaping cannot be performed at the source, you can use a verbatim here-string:
if (#'
$(Build.Reason)
'# -eq 'Manual') { # ...
Important: The closing '# must always be at the very beginning of the line.

Powershell how to pass parameter with special characters

I'm trying to pass a variable to powershell script as parameter. Variable has some special characters and the call to ps1 script fails.
Here I just created a sample to show the problem.
PS C:\>$pass1 = '?q$*9-W$wcx)O.Ra)X-&6'
PS C:\>powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command 'echo '$pass1''
ERROR:
At line:1 char:26
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:32
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
Unexpected token ')' in expression or statement.
At line:1 char:37
+ echo ?q$*9s8ubhD8c-W$wcx)O.Ra9)DX-D&6
+ ~
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation
marks ("&") to pass it as part of a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
enter image description here
Your script has a syntax error, it doesn't appear to be a problem with the actual value of the parameter/variable.
This is the line that has a syntax error:
powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Command 'echo '$pass1''
This is where your problem is:
'echo '$pass1''
You're actually passing 3 things here...
'echo '
$pass1
''
The second single quote is exiting the string.
You need to alternate between single and double strings depending on how you need to use them:
$pass1 = '?q$*9-W$wcx)O.Ra)X-&6'
powershell -Command "echo '$pass1'"

How to correctly escape spaces and backslashes in command line arguments?

I had some issues passing an array of strings to a command in PowerShell, so I'm debugging my script. I'm using the EchoArgs.exe program found in the PowerShell Community Extension Project (PSCX).
If I execute this script:
Import-Module Pscx
cls
$thisOne = 'this_one\';
$secondOne = 'second one\';
$lastOne = 'last_one'
$args = $thisOne `
, "the $secondOne" `
, "the_$lastOne"
EchoArgs $args
I get this result:
Arg 0 is <this_one\>
Arg 1 is <the second one" the_last_one>
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" this_one\ "the second one\" the_last_one
It seems that if a string contains spaces, the last backslash escapes the double quote. In fact all seems working if I escape only that backslash:
Import-Module Pscx
cls
$thisOne = 'this_one\';
$secondOne = 'second one\\';
$lastOne = 'last_one'
$args = $thisOne `
, "the $secondOne" `
, "the_$lastOne"
EchoArgs $args
with this result:
Arg 0 is <this_one\>
Arg 1 is <the second one\>
Arg 2 is <the_last_one>
Command line:
"C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\Pscx\Apps\EchoArgs.exe" this_one\ "the second one\\" the_last_one
Is there a "smart" way in PowerShell (i.e. a cmdlet) to escape any string in order to avoid such issues?
Try using Start-Process instead. It has an $Arguments parameter that would suit this better.
See here: PowerShell - Start-Process and Cmdline Switches

Passing empty strings using double quotes via PowerShell.exe -command

Seems like a bug... Only thing I can think of is double quotes are being lost through external invocation but it works when the string has a character, so I don't think so. Zero length string fails.
My version:
>$host
Name : ConsoleHost
Version : 3.0
Empty string fails:
> powershell.exe -command '&{write-host ""}'
The string is missing the terminator: ".
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
Works if the string has a character:
> powershell.exe -command '&{write-host "a"}'
a
Works by itself:
> &{write-host ""}
<blank line>
Works with reversed quotes:
>powershell.exe -command "&{write-host ''}"
<blank line>
And as #Ansgar pointed out escaping works (\ isn't documented in powershell.exe /?)
> powershell.exe -command '&{write-host \"\"}'
<blank line>
It appears to be using the at-string parser. The command ends up being:
#"'&{write-host ""}'"
which reduces to
'&{write-host "}'
The alternatives are:
# Like you said, escape the string
powershell.exe -command '&{write-host \"\"}'
# Use a script block instead of a string
powershell.exe -command {write-host ""}
You might also look into using a Base64 encoded command, but I've never gotten that to work.

Powershell variable for cmd argument

I want to use a powershell variable for a cmd argument but I don't know how to make it.
function iptc($file)
{
$newcredit = correspondance($credit)
$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit=$newcredit $file.FullName'
Invoke-Expression $cmd
}
For example newcredit can be "James" but in my case when I run the command -Credit will only be "$newcredit".
Regards
Single quotes (' ') do not expand variable values in the string. You can address this by either using double quotes (" "):
$cmd = "& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit=$newcredit $file.FullName"
Or, by the method I most often use, by using string formatting:
$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit={0} {1}' -f $newcredit, $file.FullName
If either of the parameters has a space in it then the parameter will need to be surrounded by double quotes in the output. In that case I would definitely use string formatting:
$cmd = '& C:\exiftool\exiftool.exe -S -t -overwrite_original -Credit="{0}" "{1}"' -f $newcredit, $file.FullName