[英]Powershell Remoting with credential
在运行诸如Enable-PsSession
、 Invoke-command
等远程命令时,我们需要为这些命令提供凭据。
我不想在执行这些命令时每次都提供凭据。
也可以说我将用户名存储在变量中并在执行命令时使用该变量。 我也想为密码这样做。 我可以这样做吗?
例如:
Invoke-Command -ComputerName mycomputer -ScriptBlock { Get-ChildItem C:\ } -credential mydomain\administrator
所以在这里我每次在执行这些命令时都提供密码。
命令应该如何从变量和其他一些机制中自动获取用户名和密码?
你可以做:
$cred = get-credential #fill you credential in the pop-up window
接着:
Invoke-Command -ComputerName mycomputer -ScriptBlock { Get-ChildItem C:\ } -credential $cred
请记住, $cred
中的密码很容易以明文形式恢复!
加密一切。 创建一个对象,调用解密信息作为密码。 像这样使用它:
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
如果没有明确指定,Powershell 将默认为运行 powershell 会话的用户的凭据。
因此,如果您以在远程计算机上具有管理权限的用户身份运行 Powershell,则在运行命令时不必输入凭据。
您可以做的是,您可以使用服务帐户的存储凭据创建计划任务,并允许用户(或仅您自己)访问以运行该任务。
或者您可以将凭据存储在 Windows 凭据管理器中,这意味着它们是使用您的 Windows 用户加密的。
https://gist.github.com/toburger/2947424
但是,使用凭证管理器解决方案,任何能够在您的上下文中运行脚本的用户都能够以明文形式提取密码。
不过,这不是问题,如果您只为自己使用它,或者如果每个运行脚本的管理员都从他自己的用户上下文中这样做。
任何需要密码的自动化脚本都是有缺陷的。 其他答案中的评论表明反转安全字符串是多么容易。
$cred = get-credential mydomain\\me
$cred | export-clixml credfile.xml
$cred | export-clixml credfile.xml
$cred=import-xmlcli \\hidden\\credfile.xml
enter-pssession -computername server -credential $cred
这就是我所做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.