SharpeRAD / Cake.Powershell

Powershell addin for Cake
http://cakebuild.net
MIT License
83 stars 36 forks source link

Issues of executing remote script #7

Closed bumbeishvili closed 7 years ago

bumbeishvili commented 7 years ago

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?

SharpeRAD commented 7 years ago

Please use "StartPowershellFile" over "StartPowershellScript" for script files.

agc93 commented 7 years ago

@SharpeRAD in saying that, it's currently not possible to execute any command that uses a PowerShell ScriptBlock, since the curly braces get interpreted differently. Is it worth maybe including a setting to prevent echoing the command to the log?

SharpeRAD commented 7 years ago

Like this?

agc93 commented 7 years ago

No, I was getting at preventing this from executing. As it stands, including a curly brace is fine from an execution point of view, but the call to ICakeLog (at least in the case of cake.exe and its CakeBuildLog implementation) fails because the curly braces are implemented differently.

In saying that, I've just tested and using a module to replace the default ICakeLog implementation and the only way to get this to work is something along the lines of this (hacky) solution:

public void Write(Verbosity verbosity, LogLevel level, string format, params object[] args)
        {
            if (verbosity > Verbosity) return;
            _console.WriteLine(format.Contains("ScriptBlock") ? format.Replace("{", "{{").Replace("}", "}}") : format,
                args);
        }

Is it worth maybe changing that line in PowerShellRunner to double up any curly braces it finds since even string.Format will choke on them as-is?

SharpeRAD commented 7 years ago

Good call, PR away 😄 or I'll look at it tonight.

agc93 commented 7 years ago

Done! #8 🍰

bumbeishvili commented 7 years ago

@SharpeRAD How can I start powershell script file on remote server?

SharpeRAD commented 7 years ago

I've updated the readme to include a remote file example, please be aware paths are relative to the server your running the script on, not from. Also checkout the troubleshooting section.

bumbeishvili commented 7 years ago

How can I pass parameters to that script? More specifically what my needs are:

I need to execute test.ps1 file in remote server, which is located under hr2-02.bog.ge domain on C:\ disk I have every permission & access issue resolved

remote powershell file content is following

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

So I need to pass name and password parameters

agc93 commented 7 years ago

@bumbeishvili as a workaround, note that your original solution (using Invoke-Command with ScriptBlock ) should work with Cake.Powershell version 0.2.7 since my PR is included in that version.

In saying that, I would defer to @SharpeRAD on StartPowershellFile being the "proper" way of doing it..

bumbeishvili commented 7 years ago

@agc93, @SharpeRAD it worked, thanks both :+1:

One issue left, logs are not displaying , so It's really hard to debug

update I was able to display logs using command StartPowershellScript(powershellStringCommand, new PowershellSettings().SetLogOutput());

SharpeRAD commented 7 years ago

I updated the ReadMe again to include arguments for "StartPowershellFile".