Struggling to encrypt the password in my WinSCP PowerShell script (example below)
Hello,
I need to encrypt my password in this copy script from Windows to Linux. This script has been working fine, I just need to add security.
Current working code
I then tried to encrypt:
I ran this to generate the hashed password:
Then added to my script:
But get this error when running:
Any idea I what I need to add/change?
Thanks
I need to encrypt my password in this copy script from Windows to Linux. This script has been working fine, I just need to add security.
Current working code
# Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Set up session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "192.168.138.113" UserName = "test" Password = "test999#" SshHostKeyFingerprint = "ssh-ed25519 255 mfVWm6kqrWMoPBy7q7tkZydM/SmvA3jVPjRXoLxcLfA=" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Transfer files $session.PutFiles("C:\dism.log", "/usr/share/csv/*").Check() } finally { $session.Dispose() }
I ran this to generate the hashed password:
Read-Host 'enter password' -AsSecureString | ConvertFrom-SecureString | Out-File 'C:\temp\password.txt'
# Load WinSCP .NET assembly Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll" # Set up session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "192.168.138.113" UserName = "test" SecurePassword = ConvertTo-SecureString "01000000d08c9ddf0115d1118c7a00c04fc297eb01000000f7e98d274da0e843a4ff48f5aa20208200000000020000000000106600000001000gkhkjhkjhkjhlkjlkj428a8b5d2fae4abd140000000ba8036948a049304e1311100b694fc03d1445d9d03ff0e25e0461383d303f9d9b5f76f477c514cf939c7637fd88897e32815af66e9ecefa8619e18e12202ac7c " SshHostKeyFingerprint = "ssh-ed25519 255 mfVWm6kqrWMoPBy7q7tkZydM/SmvA3jVPjRXoLxcLfA=" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Transfer files $session.PutFiles("C:\dism.log", "/usr/share/csv/*").Check() } finally { $session.Dispose() }
ConvertTo-SecureString : Input string was not in a correct format. At line:9 char:22 + ... ePassword = ConvertTo-SecureString "01000000d08c9ddf0115d1118c7a00c04 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [ConvertTo-SecureString], FormatException + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.ConvertToSecureStringCommand
Thanks