Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell-密码和SFTP的安全字符串_Powershell_Sftp - Fatal编程技术网

Powershell-密码和SFTP的安全字符串

Powershell-密码和SFTP的安全字符串,powershell,sftp,Powershell,Sftp,我正在尝试实现一种使用存储的安全字符串的方法,以便我的SFTP密码在脚本中不可见。例如,我想生成一个变量$password,可以用它来代替。我在网上找到了以下示例,但不幸的是,我无法让它们发挥作用。我过去也做过类似的事情,但可以找到我的笔记或网站链接,说明如何完成任务 read-host -assecurestring | convertfrom-securestring | out-file C:\securestring.txt $pass = cat C:\securestring.tx

我正在尝试实现一种使用存储的安全字符串的方法,以便我的SFTP密码在脚本中不可见。例如,我想生成一个变量$password,可以用它来代替。我在网上找到了以下示例,但不幸的是,我无法让它们发挥作用。我过去也做过类似的事情,但可以找到我的笔记或网站链接,说明如何完成任务

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
这是我的剧本。如果有人感兴趣,这里有一个指向管理单元的链接

再次感谢大家的帮助

更新

我试过这个:

$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()
并获取以下错误:

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
有什么建议吗


谢谢

如果您的SFTP想要使用您的安全密码的解密版本,则您需要通过以下方式从$mycred中提取该密码:

$userpassword = $mycred.getnetworkcredential().password.tostring()

你有什么问题?看起来你找到了所有需要的信息。只需更改
-username$mycred.username
-userPassword$mycred.password
就可以了。有很多网站介绍了powershell凭据,可能没有打开sftpserver,但这不应该是个问题。我建议多做点研究它怎么不起作用?你有什么错误可以和我们分享吗?我用我的尝试和收到的错误更新了帖子。谢谢看起来管理单元返回异常作为结果,而不是让它冒泡,这是我使用的第一个SFTP解决方案。如果有任何关于使用Powershell的SFTP的其他建议,可以使用安全字符串,我愿意试一试。
$userpassword = $mycred.getnetworkcredential().password.tostring()