Need advice/support with a PowerShell script
Hello together
First of all I would like to thank you for your time and possible ideas.
The following situation
I have a SFTP server running on Debian and I have written a script that creates users, a password and gives them access to
Now I want to write a PowerShell script that has the password encrypted in the script, establishes the connection, checks whether data that exists locally also exists in the remote folder (on the SFTP server) and copies it if not. I have read the documentation on the WinSCP page (thanks for that!). Unfortunately I fail.
What was done:
Encryption
Created XML-File with Username and encrypted password
And the PowerShell-Script
But when I run the script in Powershell ISE I get the following output
Can anyone here help me? Thank you very much!
First of all I would like to thank you for your time and possible ideas.
The following situation
I have a SFTP server running on Debian and I have written a script that creates users, a password and gives them access to
/share
only. This all works and I can successfully log in via WinSCP and see everything from /share
.
Now I want to write a PowerShell script that has the password encrypted in the script, establishes the connection, checks whether data that exists locally also exists in the remote folder (on the SFTP server) and copies it if not. I have read the documentation on the WinSCP page (thanks for that!). Unfortunately I fail.
What was done:
Encryption
$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."
But when I run the script in Powershell ISE I get the following output
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.