PowerSSH.psm1

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

ForEach ($Command in $CommandsSsh) {
    Set-Alias -Name $Command -Value Invoke-SshCommand
}
ForEach ($Command in $CommandsBash) {
    Set-Alias -Name $Command -Value Invoke-BashCommand
}

Function Set-PowerSsh {
    Import-Module PowerSSH
    $Content = 'Import-Module PowerSSH'
    If (-Not (Test-Path -PathType Leaf -Path $Profile)) {
        Set-Content -Path $Profile -Value $Content
    } Else {
        If (-Not (Get-Content -Path $Profile | Select-String $Content)) {
            Add-Content -Path $Profile -Value $Content
        }
    }
}

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"
}

Function Set-SshKeys {
    Param(
        [String] $Comment = (Get-Culture).TextInfo.ToTitleCase($env:COMPUTERNAME.ToLower())
    )
    Bash -c "ssh-keygen -t rsa -b 4096 -C '$Comment'"
}

Function Get-SshPublicKey {
    Bash -c "cat ~/.ssh/id_rsa.pub"
}