Re: PowerShell SecurePassword
Thank you very much. That was the solution.
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
SecurePassword = ConvertTo-SecureString $config.Configuration.Password
...
}
Read-Host -AsSecureString | ConvertFrom-SecureString
<Configuration>
<UserName>username</UserName>
<Password>01000000d08c9.....</Password>
</Configuration>
try
{
# Read XML configuration file
[xml]$config = Get-Content "C:\temp\config.xml"
# Load WinSCP .NET assembly
Add-Type -Path "path-to-winscp\WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
$sessionOptions.SecurePassword = ConvertTo-SecureString $config.Configuration.Password
Protocol = [WinSCP.Protocol]::Scp
HostName = "my-ip-address"
UserName = $config.Configuration.UserName
Password = $sessionOptions.SecurePassword
SshHostKeyFingerprint = "ssh-ed25519 255 my-finger-print"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.PutFiles("my-source-path\myfile-*", "my-destination-path", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
Error: The System.Security.SecureString element was not found for the specified .NET object.