是否可以将 NetworkCredentials(或变体)转换为 PSCredential?
我想透明地使用当前用户的凭据,但不想提示他们。
是否可以将 NetworkCredentials(或变体)转换为 PSCredential?
我想透明地使用当前用户的凭据,但不想提示他们。
$cred = new-object PSCredential($nc.UserName, (ConvertTo-SecureString $nc.Password -AsPlainText -Force))
[System.Reflection.Assembly]::Load("System.Management.Automation")
$dc = [System.Net.CredentialCache]::DefaultCredentials
$psc = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $dc.UserName, $dc.SecurePassword
这样做可以,除了没有为运行 PowerShell 的当前应用程序域设置默认凭据。
至少这说明了如何PSCredential
从持久的用户名和密码创建一个。SecureString
总是很糟糕,但我相信它NetworkCredential
提供了一个可以使用的纯文本构造函数和一个SecureString
可以读取的属性。