4

是否可以将 NetworkCredentials(或变体)转换为 PSCredential?

我想透明地使用当前用户的凭据,但不想提示他们。

4

2 回答 2

3
$cred = new-object PSCredential($nc.UserName, (ConvertTo-SecureString $nc.Password -AsPlainText -Force))
于 2015-07-14T18:41:36.947 回答
1
[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可以读取的属性。

于 2013-10-09T14:54:24.183 回答