硬编码密码到powershells“新PSSession”

问题描述:

我有一个脚本来获取和设置给定用户的其他计算机上的用户的Windows环境变量。是否可以硬编码该用户的密码,以便我不必每次运行该脚本时都键入它?硬编码密码到powershells“新PSSession”

我的剧本看起来是这样的:

$s5 = New-PSSession -computername testauto2, testauto3 -Credential 
Domain\testautouser 

invoke-command -session $s5[0] -scriptblock {[Environment]::GetEnvironmentVariable("TestBrowser", "user")} 
+0

一些提示这里:http://powershell.com/cs/blogs/tobias/archive/2010/10/22/encrypting-passwords.aspx – 2012-04-04 13:28:43

是的 - 你完全可以做到这一点,只要你是舒适与安全的影响(一PW在某处的文件)...

这里有一个例子:

$pw = convertto-securestring -AsPlainText -Force -String <insert pw here> 
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Domain\User",$pw 
$session = new-pssession -computername <computer> -credential $cred 

我在类似的情况下使用了这种方法。这当然不是完美的,但它使我不像在文件中硬编码密码那样紧张。我在第一次运行时读取和存储密码,然后从DPAPI加密的文件中读取。我通常从内部网络上的共享位置运行脚本,并将加密的密码文件存储在本地计算机上的私人文件夹中。

$user = "Domain\testautouser" 
$passwdFile = "$env:USERPROFILE\myscript-$user" 
if ((Test-Path $passwdFile) -eq $false) { 
    $cred = new-object system.management.automation.pscredential $user, 
     (read-host -assecurestring -prompt "Enter a password:") 
    $cred.Password | ConvertFrom-SecureString | Set-Content $passwdFile 
} 
else { 
    $cred = new-object system.management.automation.pscredential $user, 
     (Get-Content $passwdFile | ConvertTo-SecureString) 
} 
+0

我觉得这是一个很好的补充,以减轻安全考虑小。 – 2017-05-11 13:26:53