makerofthings7
makerofthings7

Reputation: 61483

Convert NetworkCredentials to a PSCredential?

Is it possible to convert NetworkCredentials (or a variant) to a PSCredential?

I want to transparently use the credentials of the current user, but don't want to prompt them.

Upvotes: 5

Views: 6393

Answers (2)

Cameron Taggart
Cameron Taggart

Reputation: 6091

$cred = new-object PSCredential($nc.UserName, (ConvertTo-SecureString $nc.Password -AsPlainText -Force))

Upvotes: 3

Luke Puplett
Luke Puplett

Reputation: 45273

[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

That would do it, except that the default credentials are not set for the current application domain in which PowerShell is running.

At least this illustrates how you can create a PSCredential from a persisted username and password. The SecureString is always a bummer but I believe NetworkCredential offers a plaintext constructor you can use and a SecureString property to read from.

Upvotes: 1

Related Questions