1. 程式人生 > >PowerShell 指令碼中呼叫密文密碼

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     #此命令可遠端登入指定機器