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
很容易以明文形式恢复!
小智 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)
归档时间: |
|
查看次数: |
64248 次 |
最近记录: |