我想使用WinSCP .NET程序集與安全證書,當密碼保護在外部文件中。PowerShell使用WinSCP .NET程序集與安全憑據
# Load WinSCP .NET assembly
Add-Type -Path "D:\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
# Env
$sessionOptions.HostName = "blabla.com"
$sessionOptions.UserName = "UUUU"
#$sessionOptions.Password = "PPPP"
$sessionOptions.SshHostKeyFingerprint = "XXXXXXXXX"
$remotePath = "/home/UUUU/"
用硬編碼密碼,它的工作。如果我想使用securestring作爲密碼,我該怎麼做?
我想:
read-host -assecurestring | convertfrom-securestring | out-file D:\securestring.txt
要保持安全的密碼文件。然後,在我的劇本,我把它找回來:
$sessionOptions.Password = get-Content D:\securestring.txt | convertto-securestring
$Cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $sessionOptions.UserName, $sessionOptions.Password}
但它不工作...
WinSCP .NET程序集已經支持'SecureString',請參見[我的答案](http://stackoverflow.com/a/26279430/850848)。 – 2015-06-02 20:21:09