1

I'm writing a c# program, it will use current user's credential to run a script.

How can I convert CredentialCache.DefaultCredentials object using in access web service to PSCredential object?

Or other better way to create PSCredential without storing username/password?

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
bychance
  • 282
  • 4
  • 14
  • Are you trying to invoke Powershell via a webservice through the credentials of the user accessing the service?? – David W Jul 24 '12 at 12:51
  • No, it is just two functions. Web service can use current user credential, and I want powershell does it too. – bychance Jul 24 '12 at 23:49
  • This can't be done. See [this question][1] [1]: http://stackoverflow.com/questions/3917779/get-current-users-credentials-object-in-powershell-without-prompting?rq=1 – Flores May 01 '13 at 11:31

1 Answers1

0

No guarantees here, but you can at least try something like the following pseudocode

// DefaultCredentials returns an ICredentials reference
// which can then be used to call GetCredentials, 
// and *that* returns a NetworkCredential.
NetworkCredential netCredential = CredentialCache.DefaultCredentials.GetCredentials(..);

// Now, with a NetworkCredential that *might* have a SecurePassword, you 
// could see if that would work to create a PSCredential. I haven't tested this,
// and honestly I'm a bit dubious of the prospects, but its at least a thought.
// There may be some translation necessary to even make that SecurePassword work,
// assuming it is even available in your context, to something the PSCredential
// can use. 

PSCredential psCredential = new PSCredential(netCredential.UserName, 
                                             netCredential.SecurePassword);

As noted, this is untested pseudocode, but hopefully it gives you a push in the right direction. Good luck.

David W
  • 10,062
  • 34
  • 60