通过 PowerShell 在 WinSCP 中保存会话

Save a session in WinSCP through PowerShell

我想在 WinSCP 中通过 PowerShell 保存会话,但我无法使用以下代码保存任何会话。可能是保存会话的 PowerShell 代码错误。我想保存会话,每当我打开 WinSCP 时,我都可以看到该会话并能够登录到该会话。下面是代码截图:

try
{
  # Load the assembly and setup the session properties
  Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

  # Session setup options
  $sessionOptions = New-Object WinSCP.sessionOptions -Property @{
    Protocol = ([WinSCP.Protocol]::Sftp)
    HostName = "xxx.cloud.com"
    UserName = "sftpuser_WIN_NPD" 
    PortNumber = "40022"
    #Password = ""
    SshPrivateKeyPath = "C:\key\sftp_private.ppk"
    SshHostKeyFingerprint = "ssh-rsa 2048 0c:ce:5a:8a:82:f6:03:xx:xx:xx:xx:xx:xx:xx:xx:xx"
  }

  $session = New-Object WinSCP.session
}
catch 
{
  Write-Host "Error : $($_.Exception.Message)"

  exit 1
}   

请帮助我,提前致谢:)

您不能使用 WinSCP .NET 程序集为 WinSCP GUI 创建存储站点。

您必须在 Windows 注册表(或 INI 文件)中创建站点密钥。
参见 https://winscp.net/eng/docs/config

一个简单的例子:

$key = New-Item 'HKCU:\Software\Martin Prikryl\WinSCP 2\Sessions\My New Session' -Force
$key | New-ItemProperty -Name 'HostName' -Value 'example.com' -Force
$key | New-ItemProperty -Name 'UserName' -Value 'username' -Force

另见 https://winscp.net/eng/docs/custom_distribution#preconfigured_site