Powershell Remoting凭证

usr*_*986 12 powershell powershell-2.0

在运行远程命令等时Enable-PsSession,Invoke-command我们需要提供这些命令的凭据.

我不希望每次执行这些命令时都提供凭据.

还可以说我在执行命令时将用户名存储在变量中并使用变量.我也想为密码这样做.我能这样做吗?

例如:

Invoke-Command -ComputerName mycomputer -ScriptBlock { Get-ChildItem C:\ } -credential  mydomain\administrator
Run Code Online (Sandbox Code Playgroud)

所以这里我每次都在执行这些命令时提供密码.

命令应该如何从变量和其他机制自动获取用户名和密码?

CB.*_*CB. 20

你可以做:

$cred = get-credential #fill you credential in the pop-up window
Run Code Online (Sandbox Code Playgroud)

然后:

Invoke-Command -ComputerName mycomputer -ScriptBlock { Get-ChildItem C:\ } -credential $cred
Run Code Online (Sandbox Code Playgroud)

请记住,密码$cred很容易以明文形式恢复!

  • 如果您想将用户名/密码作为 CI 流程的一部分传递,则不能选择弹出窗口,因为它将作为自动化流程运行。还有其他方法吗? (4认同)
  • 我刚刚了解到您可以执行`$cred.GetNetworkCredential()|fl *` 来检索用户名和密码 (2认同)

小智 7

加密一切.创建一个对象,将解密的信息作为密码.像这样使用它:

read-host -assecurestring | convertfrom-securestring | out-file C:\localdrivespace\username-password-encrypted.txt
$username = "domain\username"
$password = cat C:\localdrivespace\username-password-encrypted.txt | convertto-securestring
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
Run Code Online (Sandbox Code Playgroud)