PowerSSH.psm1

$Commands = 'ssh', 'ssh-agent', 'ssh-add', 'ssh-keygen', 'ssh-keyscan', 'ssh-copy-id', 'scp', 'sftp', 'rsync'

ForEach ($Command in $Commands) {
    Set-Alias -Name $Command -Value Invoke-SshCommand
}

Function Invoke-SshCommand {
    If ([System.Environment]::GetEnvironmentVariable('PowerSshChecked', 'Process')) {
        $AgentPid = [System.Environment]::GetEnvironmentVariable('PowerSshAgentPid', 'User')
        $AgentSocket = [System.Environment]::GetEnvironmentVariable('PowerSshAgentSocket', 'User')
    } Else {
        # Bash
        If (`
            (-not ($ProcessBashId = [System.Environment]::GetEnvironmentVariable('PowerSshPid', 'User')))`
            -or (-not ($ProcessBash = Get-Process -Id $ProcessBashId -ErrorAction Ignore))`
            -or ($ProcessBash.Name -notlike 'powershell')`
        ) {
            $ProcessBash = Start-Process PowerShell -PassThru -ArgumentList '-WindowStyle Hidden -Command Bash'
            [System.Environment]::SetEnvironmentVariable('PowerSshPid', $ProcessBash.Id, 'User')
        }
        # Agent
        If (`
            (-not ($AgentPid = [System.Environment]::GetEnvironmentVariable('PowerSshAgentPid', 'User')))`
            -or (-not ($AgentSocket = [System.Environment]::GetEnvironmentVariable('PowerSshAgentSocket', 'User')))`
            -or (-not (Bash -c "ps $AgentPid | grep ssh-agent"))
        ) {
            If ($MyInvocation.InvocationName -eq 'ssh-agent') {
                $AgentOutput = Bash -c "ssh-agent $Args" | Select-String -Pattern '(SSH_[A-Z_]+)=([^;]*)' -AllMatches
            } Else {
                $AgentOutput = Bash -c 'ssh-agent' | Select-String -Pattern '(SSH_[A-Z_]+)=([^;]*)' -AllMatches
            }
            $AgentPid = $AgentOutput.Matches[1].Groups[2].Value
            $AgentSocket = $AgentOutput.Matches[0].Groups[2].Value
            [System.Environment]::SetEnvironmentVariable('PowerSshAgentPid', $AgentPid, 'User')
            [System.Environment]::SetEnvironmentVariable('PowerSshAgentSocket', $AgentSocket, 'User')
            If (Bash -c "grep ~/.bashrc -e 'export SSH_AGENT_PID'") {
                Bash -c "sed -i 's/export SSH_AGENT_PID=.*/export SSH_AGENT_PID=$AgentPid/g' ~/.bashrc"
                Bash -c "sed -i 's/export SSH_AUTH_SOCK=.*/export SSH_AUTH_SOCK=$($AgentSocket -replace '/', '\/')/g' ~/.bashrc"
            } Else {
                Bash -c "printf 'export SSH_AGENT_PID=$AgentPid\nexport SSH_AUTH_SOCK=$AgentSocket' >> ~/.bashrc"
            }
        }
        # Key
        If ($MyInvocation.InvocationName -eq 'ssh-add') {
            Bash -c "export SSH_AGENT_PID=$AgentPid && export SSH_AUTH_SOCK=$AgentSocket && ssh-add $Args"
        } Else {
            If (-not (Bash -c "export SSH_AGENT_PID=$AgentPid && export SSH_AUTH_SOCK=$AgentSocket && ssh-add -l 2> /dev/null | grep .ssh")) {
                Bash -c "export SSH_AGENT_PID=$AgentPid && export SSH_AUTH_SOCK=$AgentSocket && ssh-add"
            }
        }
        If (Bash -c "export SSH_AGENT_PID=$AgentPid && export SSH_AUTH_SOCK=$AgentSocket && ssh-add -l 2> /dev/null | grep .ssh") {
            [System.Environment]::SetEnvironmentVariable('PowerSshChecked', $true, 'Process')
        }
    }
    Bash -c "export SSH_AGENT_PID=$AgentPid && export SSH_AUTH_SOCK=$AgentSocket && $($MyInvocation.InvocationName) $Args"
}