JoshAdel
JoshAdel

Reputation: 68682

Running github for windows powershell on remote server

I'm attempting to use Ansible to automate some deployment on a remote Windows machine and am having some difficulty with executing git commands on the remote machine. If I'm physically on remote machine, I use git through the posh-git command line tool provided by the Github client for Windows. I made the git executable available from the standard powershell, by adding

.  (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")
.  $env:github_posh_git\profile.example.ps1

to my profile in %USERPROFILE%\Documents\WindowsPowerShell\Profile.ps1.

When I try to execute git commands in a powershell script through Ansible, it appears as if the profile is not being "sourced" (using the Linux parlance that I'm much more familiar with) in the remote session. According to these docs, there is a profile for "Current User, All Hosts" that is read from $Home\Documents\Profile.ps1, but locating the same file there doesn't seem to solve the issue.

Any ideas of how to get my environment set up properly so that I can run powershell scripts that reference the Github posh-git git command from within a powershell script executed by Ansible, which is using a python wrapper around WinRM?

Upvotes: 1

Views: 1057

Answers (2)

JoshAdel
JoshAdel

Reputation: 68682

Instead of trying to rely on a profile, I ended up just calling the line that sets up git directly in my powershell script:

function SetupGit () {
    Write-Host "Setting up powershell git access"

    if (Get-Command git -errorAction SilentlyContinue) {
        Write-Host "git is already accessible"
        return
    }

    .  (Resolve-Path "$env:LOCALAPPDATA\GitHub\shell.ps1")

    if (-Not (Get-Command git -errorAction SilentlyContinue)) {
        Write-Host "Cannot find git command"
        Exit 1
    }
}

Upvotes: 1

briantist
briantist

Reputation: 47792

PowerShell doesn't load the profile when you use remoting.

posh-git doesn't really add git to PowerShell, it mostly gives a custom prompt in git repos and tab completion for git commands. The actual git functionality is provided by msysgit, so there is a git.exe executable around somewhere.

If you add the path (for me it's C:\Program Files (x86)\Git\bin) to your PATH environment variable on the remote host, or include the full path to git in your commands, it should work; so instead of git push, maybe do:

& "${env:ProgramFiles(x86)}\Git\Bin\git.exe" push

Not very pretty, but it should do the job. You can also make an alias:

New-Alias -Name git -Value "${env:ProgramFiles(x86)}\Git\Bin\git.exe"
git push

But you still can't load this automatically without the profile, unless you create a new session configuration and create a new powershell endpoint, but I'm not sure if Ansible can use that. You could try to replace the configuration of the default endpoint Microsoft.PowerShell but that seems like a bad idea.

Upvotes: 1

Related Questions