I am getting an error while executing a remote PowerShell script. From my local machine I am running a PowerShell script that uses Invoke-Command
to cd
into a directory on a remote Amazon Windows Server instance, and a subsequent Invoke-Command
to execute script that lives on that server instance. The script on the server is trying to git clone a repository from GitHub. I can successfully do things in the server script like "ls" or even "git –version". However git clone
, git pull
, etc. result in the following error:
Cloning into 'MyRepo'… + CategoryInfo : NotSpecified: (Cloning into 'MyRepo'…:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
This is my first time using PowerShell or a Windows Server. Can anyone provide some direction on this problem.
The client script:
$s = new-pssession -computername $server -credential $user
invoke-command -session $s -scriptblock { cd C:\Repos; ls }
invoke-command -session $s -scriptblock { param ($repo, $branch) & '.\clone.ps1' -repository $repo -branch $branch} -ArgumentList $repository, $branch
exit-pssession
The server script:
param([string]$repository = "repository", [string]$branch = "branch")
git --version
start-process -FilePath git -ArgumentList ("clone", "-b $branch https://github.com/MyGithub/$repository.git") -Wait
I've changed the server script to use start process and it is no longer throwing the exception. It creates the new repository directory and the .git directory but doesn't write any of the files from the github repository. This smells like a permissions issue. Once again invoking the script manually (remote desktop into the amazon box and execute it from powershell) works like a charm.
Best Answer
As far as Powershell is concerned, it is the correct behaviour.
Unfortunately (at least in my opinon), git outputs a lot of information to
stderr
even when there isn't any error. For examplegit checkout -b somenewbranch
will output things like modified file lists to stdout but theSwitched to new branch somenewbranch
message (which I think is the most relevant one) tostderr
.One of the most common ways to deal with this is to redirect
stderr
output tostdout
, e.g:This is enough for most use cases I've seen. If you want to do something with the textual output itself you'll need to go a bit further as Powershell will store the stderr-originating lines as a
RemoteException
instead of astring
. A brief example of how you might deal with something that needs to log output to the console: