Need advice/support with a PowerShell script

Advertisement

sleepersimulant
Joined:
Posts:
5
Location:
Switzerland

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 /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"
Created XML-File with Username and encrypted password
<Configuration>
  <UserName>USER</UserName>
  <Password>ENCRYPTED
</Password>
</Configuration>
And the PowerShell-Script
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.
Can anyone here help me? Thank you very much!

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,984
Location:
Prague, Czechia

Re: Need advice/support with a PowerShell script

When you use a key to encrypt the password, you have to use the same key when decrypting it:
$securePassword = ConvertTo-SecureString $config.Configuration.Password -Key $key

Reply with quote

sleepersimulant
Joined:
Posts:
5
Location:
Switzerland

Hello martin

Thanks for your reply and sorry for the late reply!

I am testing an automation, which should take place with secure credentials. I am testing various things for this, one of which is the script from above. Your input worked, but unfortunately I'm still not satisfied.

Now I am testing the following and wanted to ask for your input. I now use the Windows Credentials Manager, which I use to save the login.
$username = "testtest"
$password = "testtest"
cmdkey /generic:fqdn.com /user:$username /pass:$password

WinSCP-Script
# 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()
}
Error:
 : Credentials not found in Windows Credential Manager for fqdn.com
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
The credentials are saved and can also be viewed in the Credential Manager under "Generic". I have experimented with it for a long time now but can't get it to work. Perhaps my Powershell script is simply wrong. Do you see anything here?

Thanks a lot martin!

Reply with quote

martin
Site Admin
martin avatar

If I understand your post correctly, this is not not a WinSCP problem.
Please ask it at appropriate place, e.g. on Stack Overflow.

Reply with quote

Advertisement

You can post new topics in this forum