PSCredential错误

时间:2017-03-06 22:53:08

标签: powershell credentials

我使用内联脚本创建了PSCredential,并且能够访问凭据,直到创建凭据的PowerShell会话打开。当我关闭并重新打开PowerShell以再次运行脚本时,我无法访问$TFSAdminCred

$UserID = "xyz"
$PswdFile = "\\Server1\TFSencrypt$\Pswd.txt"
$KeyFile = "\\Server1\TFSencrypt$\AES.txt"

$Key = New-Object byte[] 16
[System.Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
$Key | Out-File $KeyFile
$Key = Get-Content $KeyFile

$Pswd = "password" | ConvertTo-SecureString -AsPlainText -Force |
        ConvertFrom-SecureString -Key $Key | Out-File $PswdFile

$TFSAdminCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserId, (Get-Content $PswdFile | ConvertTo-SecureString -Key $Key)

1 个答案:

答案 0 :(得分:0)

要保留变量以便重复使用,您可以将它们导出到磁盘并在Powershell start上导入,或者您可以使用powershell配置文件为您执行此操作。

导出变量do

$var | export-clixml 'path'

导入

$var = import-clixml 'path'

使用个人资料导入只需将导入添加到您的PowerShell个人资料中,从PS提示符访问您的个人资料notepad $profile