Re: Need advice/support with a PowerShell script
The "SHA256:" part definitely shouldn't be there.
See also https://winscp.net/eng/docs/faq_hostkey#automation
See also https://winscp.net/eng/docs/faq_hostkey#automation
# Importieren des CredentialManager-Moduls
Import-Module CredentialManager
# Definieren Sie die Zieladresse der Anmeldeinformationen
$server = "server.server.com"
$port = 2222
$credentialTarget = "${server}:${port}"
$credential = Get-StoredCredential -Target $credentialTarget
if ($credential -eq $null) {
Write-Host "Anmeldeinformationen für $credentialTarget wurden nicht gefunden."
exit 1
} else {
Write-Host "Anmeldeinformationen erfolgreich abgerufen."
}
function ConvertFrom-SecureStringToPlainText {
param (
[System.Security.SecureString]$secureString
)
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
try {
return [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)
}
finally {
[System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr)
}
}
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
try {
$passwordPlainText = ConvertFrom-SecureStringToPlainText -secureString $credential.Password
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = $server
$sessionOptions.PortNumber = $port
$sessionOptions.UserName = $credential.UserName
$sessionOptions.Password = $passwordPlainText
$sessionOptions.SshHostKeyFingerprint = "ssh-rsa SHA256:xxx"
Write-Host "Sitzungseinstellungen erfolgreich konfiguriert."
}
catch {
Write-Host "Fehler beim Konfigurieren der Sitzungseinstellungen: $($_.Exception.Message)"
exit 1
}
$session = New-Object WinSCP.Session
try {
# Sitzung öffnen
$session.Open($sessionOptions)
Write-Host "Erfolgreich mit dem SFTP-Server verbunden."
}
catch {
Write-Host "Verbindung zum SFTP-Server fehlgeschlagen: $($_.Exception.Message)"
}
finally {
# Sitzung schließen, falls geöffnet
if ($session.Opened) {
$session.Dispose()
}
}
Anmeldeinformationen erfolgreich abgerufen.
Fehler beim Konfigurieren der Sitzungseinstellungen: Exception setting "SshHostKeyFingerprint": "SSH host key fingerprint "ssh-rsa SHA256:xxxx" does not match pattern /((ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-sha2-nistp(256|384|521))( |-))?(\d+ )?(([0-9a-fA-F]{2}(:|-)){15}[0-9a-fA-F]{2}|[0-9a-zA-Z+/\-_]{43}=?)(;((ssh-rsa|ssh-dss|ssh-ed25519|ecdsa-sha2-nistp(256|384|521))( |-))?(\d+ )?(([0-9a-fA-F]{2}(:|-)){15}[0-9a-fA-F]{2}|[0-9a-zA-Z+/\-_]{43}=?))*/"
$username = "testtest"
$password = "testtest"
cmdkey /generic:fqdn.com /user:$username /pass:$password
# Define the local and remote paths
$localPath = "C:\testfolder\"
$remotePath = "/testfolder/test"
# Define the SFTP server details
$ftpServer = "fqdn.com"
$port = 2222
# Function to get credentials from Windows Credential Manager
function Get-CredentialFromManager {
param (
[string]$target
)
$cred = cmdkey /list:$target 2>&1 | Select-String -Pattern "User:"
if ($cred) {
$username = $cred -replace ".*User: (.*)", '$1'
$password = ConvertFrom-SecureString -SecureString (New-Object System.Management.Automation.PSCredential($username, (Read-Host -AsSecureString "Enter password for $username")).Password) -AsPlainText
return New-Object -TypeName PSCredential -ArgumentList $username, (ConvertTo-SecureString $password -AsPlainText -Force)
} else {
throw "Credentials not found in Windows Credential Manager for $target"
}
}
# Load the credentials from the Windows Credential Manager
try {
$credential = Get-CredentialFromManager -target $ftpServer
$username = $credential.UserName
$password = $credential.GetNetworkCredential().Password
} catch {
Write-Error $_.Exception.Message
exit 1
}
# Secure the password
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
# Define the WinSCP assembly location
$WinSCPPath = "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
# Load the WinSCP .NET assembly
Add-Type -Path $WinSCPPath
# Create a new WinSCP session
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $ftpServer
PortNumber = $port
UserName = $username
Password = $securePassword
SshHostKeyFingerprint = "ssh-ed25519 256 SHA256:xxx"
}
$session = New-Object WinSCP.Session
try {
# Connect to the server
$session.Open($sessionOptions)
# Synchronize the files
$synchronizationResult = $session.SynchronizeDirectories(
[WinSCP.SynchronizationMode]::Remote,
$localPath,
$remotePath,
$False,
[WinSCP.SynchronizationCriteria]::Checksum
)
# Check for errors
if ($synchronizationResult.IsSuccess) {
Write-Host "Synchronization succeeded!"
} else {
Write-Host "Synchronization failed with errors:"
$synchronizationResult.Failures | ForEach-Object { Write-Host $_.Message }
}
} finally {
# Disconnect from the server
$session.Dispose()
}
: Credentials not found in Windows Credential Manager for fqdn.com
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
$securePassword = ConvertTo-SecureString $config.Configuration.Password -Key $key
/share
only. This all works and I can successfully log in via WinSCP and see everything from /share
.
$key = (1..16)
$plainPassword = 'password'
$secureString = ConvertTo-SecureString -String $plainPassword -AsPlainText -Force
$encryptedPassword = ConvertFrom-SecureString -SecureString $secureString -Key $key
$encryptedPassword | Out-File "C:\path\encryptedpassword.txt"
<Configuration>
<UserName>USER</UserName>
<Password>ENCRYPTED
</Password>
</Configuration>
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
[xml]$config = Get-Content "C:\path\config.xml"
$securePassword = ConvertTo-SecureString $config.Configuration.Password
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "FQDN"
PortNumber = PORT
UserName = $config.Configuration.UserName
SecurePassword = $securePassword
GiveUpSecurityAndAcceptAnySshHostKey = $true
}
$session = New-Object WinSCP.Session
try {
$session.Open($sessionOptions)
# Define paths
$localPath = "C:\Users\test\Downloads\test\*"
$remotePath = "/share/remotetest/"
# Synchronize files
$synchronizationResult = $session.SynchronizeDirectories([WinSCP.SynchronizationMode]::Local, $localPath, $remotePath, $true)
# Throw on any error
$synchronizationResult.Check()
}
finally {
$session.Dispose()
}
Write-Host "Synchronization complete."
ConvertTo-SecureString : The parameter value "NOT THE ACTUAL ENCRYPTED STRING
" is not a valid encrypted string.
At line:8 char:19
+ ... ecurePassword = ConvertTo-SecureString $config.Configuration.Password
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [ConvertTo-SecureString], PSArgumentException
+ FullyQualifiedErrorId : ImportSecureString_InvalidArgument,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
New-Object : The value supplied is not valid, or the property is read-only. Change the value, and then try again.
At line:11 char:19
+ $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [New-Object], Exception
+ FullyQualifiedErrorId : SetValueException,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "Open" with "1" argument(s): "Value cannot be null.
Parameter name: sessionOptions"
At line:23 char:5
+ $session.Open($sessionOptions)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Synchronization complete.