Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues of executing remote powershell script using cake-build

I am trying to execute following test.ps1 script

param([string]$name,[string]$password);
write-Output "Hello $($name) my password is  $($password)";
dir c:\
New-Item c:\HRTemp\test.txt -ItemType file

on remote server using following command

StartPowershellScript("Invoke-Command", args =>
{
    args.Append("ScriptBlock", "{{c:\\test.ps1 -name dato -password test}}" );
});

I was able to successfully invoke this command from command line and now I want the same using cake script.

I am using Cake.Powershell addin.

When I try to execute it with one curly brace {c:\\test.ps1 -name dato -password test} , I am getting error:

Error: Input string was not in a correct format.

When I try it with two curly brace

{{c:\\test.ps1 -name dato -password test}}

output is the following

Executing: Invoke-Command -ScriptBlock {{c:\test.ps1 -name dato -password test}}

but, when I check on remote server test.txt file is not created.

Do you have any ideas why this is happening?

like image 903
bumbeishvili Avatar asked Oct 19 '22 00:10

bumbeishvili


1 Answers

This is caused by the different handling of curly braces by the ProcessArgumentBuilder used internally by the Cake.Powershell addin, and the format parser used internally in Cake's internal logger.

I submitted a PR to Cake.Powershell which has now been merged and a new release published, so upgrading to version 0.2.7 will resolve this issue for you.

You should then be able to use something like the following:

StartPowershellScript("Invoke-Command", args =>
{
    args.Append("hostname").Append("-ScriptBlock {c:\\test.ps1 -name dato - password test}");
});

And while the log will include double braces, the actual command will only use single braces and should run correctly.

like image 125
agc93 Avatar answered Oct 20 '22 23:10

agc93