【发布时间】:2015-06-29 02:51:24
【问题描述】:
我目前有一个脚本,成功使用这种方法远程连接到服务器,并使用 Invoke-Command 运行一些任务:
$c = Get-Credential
$computername = "server.fqdn"
$session = New-PSSession -ComputerName $computername -Authentication CredSSP -Credential $c
执行此操作时,系统会提示我输入要运行的帐户的用户名和密码。我输入它,它就可以工作。
我希望能够做的不是在脚本执行时提示输入凭据,而是希望它自动使用我存储在某个文本文件中的凭据(或至少使用密码这种方式)。这是因为我需要安排脚本从服务器外部运行。
还有另一个thread 似乎与我想要做的非常接近。使用文本文件和转换安全字符串。我不太明白如何使用 New-PSSession 使用此方法。也许还有另一种方式。
到目前为止,我有以下代码。
$username = "domain\username"
$password = cat C:\scripts\password.txt | convertto-securestring
$c = new-object -typename System.Management.Automation.PSCredential `
-argumentlist $username, $password
$computername = "server.fqdn"
$session = New-PSSession -ComputerName $computername -Authentication CredSSP -Credential $c
我仍然提示输入用户名和密码并收到错误:
convertto-securestring : Input string was not in a correct format
我的密码在文本文件中。它就像密码123。在我尝试使用文本文件之前,密码有效。
【问题讨论】:
标签: powershell scripting credentials windows-server