Note that there are some explanatory texts on larger screens.

plurals
  1. POPowershell - Secure String for Passwords and SFTP
    text
    copied!<p>I am trying to implement a way to use a stored secure string so that my SFTP password is not visiable in the script. For example, I'd like to generate a variable $password that could be used instead. I found the following examples online but I can't get them to work unfortunately. I've done something similar in the past but can find my notes or links to the website that explained how to complete the task.</p> <pre><code>read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt $pass = cat C:\securestring.txt | convertto-securestring $mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "test",$pass </code></pre> <p>Here is my script. Here is a link to the snapin if anyone is interested. <a href="http://www.k-tools.nl/index.php/sftp-in-powershell/" rel="nofollow">http://www.k-tools.nl/index.php/sftp-in-powershell/</a></p> <pre><code>#Add the SFTP snap-in Add-PSSnapin KTools.PowerShell.SFTP #Define some variables $sftpHost = "ftp.domain.com" $userName = "user" $userPassword = "password" $localFile = "C:\bin\emp1.xlsx" #Open the SFTP connection $sftp = Open-SFTPServer -serverAddress $sftpHost -userName $userName -userPassword $userPassword #Upload the local file to the root folder on the SFTP server $sftp.Put($localFile) #Close the SFTP connection $sftp.Close() </code></pre> <p>Again, thanks for everyones help!</p> <p>UPDATE</p> <p>I tried this:</p> <pre><code>$pass = cat c:\bin\ftpcreds.txt | convertto-securestring $mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "usertest1",$pass $sftpHost = "ftp.domain.com" $userName = $mycred.username $userPassword = $mycred.password $sftp = Open-SFTPServer -serverAddress $sftpHost -userName $userName -userPassword $userPassword $sftp.Put($localFile) $sftp.Close() </code></pre> <p>And get this error:</p> <pre><code>Method invocation failed because [Tamir.SharpSsh.jsch.JSchException] doesn't contain a method named 'Put'. At C:\bin\SFTP Upload Samples.ps1:21 char:1 + $sftp.Put($localFile) + ~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound Method invocation failed because [Tamir.SharpSsh.jsch.JSchException] doesn't contain a method named 'Close'. At C:\bin\SFTP Upload Samples.ps1:36 char:1 + $sftp.Close() + ~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound </code></pre> <p>Any suggestions?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload