jordand83
jordand83

Reputation: 31

Can you embed the private key into a PowerShell WinSCP script?

Can you embed the private key into a PowerShell WinSCP script instead of calling for the .ppk file? Maybe something like this?

# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    Protocol = [WinSCP.Protocol]::Scp
    HostName = "domain.com"
    UserName = "username"
    SshHostKeyFingerprint = "ssh-rsa 2048 38741934871934871293471293487"
    SecurePrivateKeyPassphrase = "AAABAHH1......................"
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Your code
}
finally
{
    $session.Dispose()
}

Upvotes: 2

Views: 1952

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202494

Use SessionOptions.SshPrivateKey property:

$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
    ...
    SshPrivateKey = "PuTTY-User-Key-File-3: ssh-rsa
Encryption: none
Comment: ...
Public-Lines: 6
...
Private-Lines: 14
...
Private-MAC: ...
"
}

# Set up session options

$session = New-Object WinSCP.Session

# Connect
$session.Open($sessionOptions)

Upvotes: 1

Related Questions