How do I call exec in psake to an executable with a variable path? - powershell

I can't seem to call this executable correctly in my psake deploy script.
If I do this:
exec { "$ArchiverOutputDir\NServiceBus.Host.exe /install" }
It simply outputs this (and is clearly not calling the executable - just outputting the value of that expression):
c:\ReloDotNet2_ServiceEndpoints\Archiver\NServiceBus.Host.exe /install
But if I do this:
exec { c:\ReloDotNet2_ServiceEndpoints\Archiver\NServiceBus.Host.exe /install }
I get the expected output from the executable.
How do I correctly call an executable with a variable in the path to the executable in psake? If this is actually a PowerShell issue, please feel free to correct the question to reflect that insight.
I

Classic PowerShell issue. Try this instead:
exec { & "$ArchiverOutputDir\NServiceBus.Host.exe" /install }
PowerShell not only executes commands, it also evaluates expressions e.g.:
C:\PS> 2 + 2
4
C:\PS> "hello world"
hello world
What you have given to PowerShell at the beginning of a pipeline is a string expression which it faithfully evaluates and prints to the console. By using the call operator &, you're telling PowerShell that the following thing is either the name of a command (in a string) to be executed or a scriptblock to be executed. Technically you could also use . "some-command-name-or-path". The only difference is that for PowerShell commands, & creates a new scope to execute the command in and . doesn't. For external exes it makes no difference as far as I can tell which one you use but & is typically used.

Related

Why is an unwanted empty line output on running a PowerShell script from within a Windows batch file?

When I execute the following hello.ps1 in PowerShell, I get an output like I would expect, a Hello World! with a newline.
#powershell script hello.ps1
"Hello World!"
Output of run
PS C:\test>.\hello.ps1
Hello World!
PS C:\test>
But when I call the script from a bat file, I get two (2) newlines output in Windows console.
:: Windows CMD script name: mytest.bat
powershell -ExecutionPolicy Bypass hello.ps1
Now the output has two newlines, note the empty line after Hello World!.
C:\test>mytest.bat
Hello World!
C:\test
How can I avoid this second newline?
Mandragor -
Firstly, that is not two lines, it's just one.
Secondly, this is not a PowerShell specific thing.
You are calling one shell from another, and both must provide you a response and return to interactive mode. Calling an external command for one shell to another must be processed by the called shell first and results returned from the called shell to the calling shell, then exit the called process, and that calling shell must complete its process and any stdout stuff, and exit that process.
Double-clicking a bat/cmd file, starts cmd.exe, task manager shows
that process is running
In your bat/cmd you are calling another standalone exe, task manager
shows that the process is running. It must execute and return stdout
results, then close/exit.
cmd.exe the completes, and returns its stdout stuff, if any, and places the cursor at the next line for more interactive work.
Hence, the two responses, only if you ask for cmd.exe stdout stuff will you see it, vs just null.
You'll always have one more line return to get you back to the calling process.
Also, point of note:
Write-Host except in specific circumstances, and what you are doing
is not one of them.
You only need that bypass policy setting if you are in a restricted
environment. RemoteSigned is now the PowerShell default, meaning all local scripts will run, and remote scripts must be signed.
Simple strings should use single quotes in most cases. Double quotes
are for expanding variable content and other formatting scenarios.
Try the same thing calling any other shell, you'll get the same results. Heck, even calling another bat/cmd from a bat/cmd.
Example:
Running mytest.bat in cmd.exe that only contains these two lines, to show that only 1 CRLF is actually returned, per process, then you get bat the interactive shell.
powershell -File D:\Temp\hello.ps1
call d:\temp\hello.bat
# Results
Hello World!
D:\Temp>call d:\temp\hello.bat
D:\Temp>echo 'Hello World!'
'Hello World!'
D:\Temp>
You can change your powershell script like this:
#powershell script hello.ps1
if ($args[0] -eq "cmd") {
write-host "Hello World!" -nonewline
}
else {
"Hello World!"
}
Then change the caller batch file:
#echo off
powershell -executionpolicy Bypass TheNameOfPowershellScript.ps1 "cmd"
Now when run from cmd use the caller batch file and from powershell directly use the ps1 script. It will give the expected result(Tested).

Run output of command as powershell incantation

I have a bash script that generates a program incantation (docker run ... with a bunch of arguments) and prints it to standard output. I want to run the output from powershell, with the optional possibility to tack on other arguments afterwards.
I've tried
D:\> & $(wsl ./my-bash-script.sh) some more args
but I get the error
The term 'docker run --rm -ti [<redacted>]' is not recognized as the name of cmdlet, function, script file, or operable program. Check the spelling of the name, or ifa path was included, verify that the path is correct and try again.
Apparently, & interprets the entire output as a single command, when in fact it's only the first word of the output that is a command; the rest is arguments.
Is there a way to do what I want?
Yes, & only supports a command name or path as its first argument - you cannot include arguments.
What you need is Invoke-Expression:
Invoke-Expression "$(wsl ./my-bash-script.sh) some more args"
Note: You'd only need & inside the string if the .sh script output a command with a quoted executable path.
Also note that PowerShell will not always interpret a given command line the same way as a POSIX-like shell (such as Bash), given that it recognizes additional metacharacters at the start of arguments; notably, arguments prefixed with # or { will not be taken as literals by PowerShell.
As an aside: While this is a legitimate use case for Invoke-Expression - assuming you trust the output from your .sh script - Invoke-Expression should generally be avoided.

Executing powershell command directly in jenkins pipeline

Is it possible to call a PowerShell command directly in the pipelines groovy script? While using custom jobs in Jenkins I am able to call the command with the PowerShell Plugin. But there is no snippet to use this in the groovy script.
I also tried sh() but it seems that this command does not allow multiple lines and comments inside the command.
To call a PowerShell script from the Groovy-Script:
you have to use the bat command.
After that, you have to be sure that the Error Code (errorlevel) variable will be correctly returned (EXIT 1 should resulting in a FAILED job).
Last, to be compatible with the PowerShell-Plugin, you have to be sure that $LastExitCode will be considered.
I have notice that the 'powershell' is now available in pipeline, but since it have several issues I prefer this variant. Still waiting it works stabil. I actually have an issue with the 'dontKillMe' behavior.
Since Jenkins 2.207 with Powershell plugin 1.4, I have replace all my calls with the official powershell pipeline command. I do now recommend to use it.
Note that you must predent \$ErrorActionPreference='Stop'; to your Script if you want it to abort on Write-Error because of an Issue with the powershell plugin.
For that porpuse I have written a little groovy method which could be integrate in any pipeline-script:
def PowerShell(psCmd) {
psCmd=psCmd.replaceAll("%", "%%")
bat "powershell.exe -NonInteractive -ExecutionPolicy Bypass -Command \"\$ErrorActionPreference='Stop';[Console]::OutputEncoding=[System.Text.Encoding]::UTF8;$psCmd;EXIT \$global:LastExitCode\""
}
[EDIT] I have added the UTF8 OutputEncoding: works great with Server 2016 and Win10.[/EDIT]
[EDIT] I have added the '%' mask[/EDIT]
In your Pipeline-Script you could then call your Script like this:
stage ('Call Powershell Script')
{
node ('MyWindowsSlave') {
PowerShell(". '.\\disk-usage.ps1'")
}
}
The best thing with that method, is that you may call CmdLet without having to do this in the Script, which is best-praxis.
Call ps1 to define CmdLet, an then call the CmdLet
PowerShell(". '.\\disk-usage.ps1'; du -Verbose")
Do not forget to use withEnv() an then you are better than fully compatible with the Powershell plugin.
postpone your Script with . to be sure your step failed when the script return an error code (should be preferred), use & if you don't care about it.
Calling PowerShell scripts is now supported with powershell step as announced on Jenkins blog.
The documentation mentions it supports multiple lines scripts.
From version 2.28 of Pipeline Nodes and Processes Plugin, we can directly use 'powershell'.
Eg: powershell(". '.Test.ps1'")
You can use the sh command like this:
sh """
echo 'foo'
# bar
echo 'hello'
"""
Comments are supported in here.

Invoking a command with variable evaluation in Octopus Deploy Powershell script

I have a simple Powershell script that I execute during an Octopus Deploy installation. This line works fine:
& $exe install --autostart
I runs an application identified by $exe variable with command line arguments "install --autostart".
Now I need to expand command line arguments with a value evaluated from a variable:
& $exe install --autostart -servicename=$serviceName
"$serviceName" is the variable that gets its value during the script execution. Whatever I do it's passed to the line above by variable name, not the value, e.g. it's passed as "$serviceName". I tried single and double quotes, nothing helps. As long it's a command invocation (triggered by the "&" symbol in the beginnging of the line), the rest of the line is interpreted verbatim, no variable substitions.
I used last couple of hours trying to figure this out and this is driving me mad. Any tips are appreciated.
I just did some testing on my side and it looks like if you'd like the variable passed in to the command to be evaluated as a variable it needs whitespace on both sides. So you would want to define your variable as $serviceName = "-servicename=*name*" or if that is not possible then create a new variable just before running the command
$tmpServicename = "-servicename=$($serviceName)"
& $exe install --autostart $tmpServiceName

PowerShell - Outputting to a variable not working

I am writing a rather lengthy PowerShell script to perform a lot of functions. For the most part, everything is going very well.
However, another function I am running at all levels of the script is a very detailed log. I'll post the command line portion as an example (simplified):
$Batch = $RunMe[1]
$ResultCode = (Invoke-Expression $Batch -ErrorAction Stop)
$ResultCode
My expected result is:
Return Code 0
Then I would log it. Instead I am getting something like:
C:\batchfiles\batchfile.bat argument
Which is the command I am using Invoke-Expression to execute.
Some quick explanation of the variables:
$RunMe is an array that stores the commands (0 is either "Batch" or "Proc" and 1 is the command to run)
$Batch is created in the code as written (this is the first reference to it)
$CurrFileToExec is the current file being processed (the script runs on a folder at a time). I'm using it for a string replace for the command line I got from SQL.
I can't get $ResultCode to pass the correct output of the command. It just passes the command line function ($Batch) again.
As I said, the actual functionality part works great, but I can't get that return code to my logfile.
If I can presume $batch contains external executables, the return code will be in the automatic variable $LASTEXITCODE - invoke-expression's return consists of things written to STDOUT and STDERR by external applications or things written to powershell's output or error streams by script. It will not contain the executable's dos-style return code, if you called one.
The solution was to modify my $Batch variable to add a cmd /c to the beginning. I think there was an issue with just using Invoke-Expression and scoping of the output parameter. I added a line for:
$Batch = "cmd /c " + $Batch
Then ran again, and $ResultCode had the expected output.