PowerShell 脚本中调用密文密码

1. 把密码转变为加密的字符串。使用命令 ConvertFrom-SecureString

Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\pwd.txt"          #弹出输入密码的对话框,然后把加密后的密码保存到输出的文件中。

2. 把加密后的字符串转换成SecureString对象。使用命令 ConvertTo-SecureString

$f = "D:\pwd.txt"
$SecurePwd = Get-Content $f | ConvertTo-SecureString
 
3. 使用密码文件创建 Credential 信息
$Cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList UserName, (Get-Content $f | ConvertTo-SecureString)  #UserName替换成你要创建的用户名密码是引用第二步的内容
example:
Enter-PSSession -ComputerName 10.1.1.1 -Credential $Cred     #此命令可远程登录指定机器

猜你喜欢

转载自www.cnblogs.com/20e8/p/9994151.html