Discussion:
windows credentials in a cmdlet
(too old to reply)
Simon
2008-04-04 14:44:37 UTC
Permalink
Hi,

I'm creating a custom cmdlet that accesses a remtoe system, and
requires a username and password to do so. I would like the cmdlet
(which I am writing in C#) to pick up the username and password of the
currently logged in user, and try these, and if they fail provide a
dialog like the get-credentials cmdlet does.

I have been hunting around though, and can't seem to find a way to get
the password of the current user. I have found a couple ways to get
the username (and domain), but that is it. Has anybody else done
something like this? Any help would be appreciated.

Thanks,

Simon
RichS [MVP]
2008-04-04 16:25:01 UTC
Permalink
I'm not aware of the password being available independently.

You can use

$cred = [System.Net.CredentialCache]::DefaultNetworkCredentials

to get the credentials of the logged on user
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog: http://richardsiddaway.spaces.live.com/
PowerShell User Group: http://www.get-psuguk.org.uk
Post by Simon
Hi,
I'm creating a custom cmdlet that accesses a remtoe system, and
requires a username and password to do so. I would like the cmdlet
(which I am writing in C#) to pick up the username and password of the
currently logged in user, and try these, and if they fail provide a
dialog like the get-credentials cmdlet does.
I have been hunting around though, and can't seem to find a way to get
the password of the current user. I have found a couple ways to get
the username (and domain), but that is it. Has anybody else done
something like this? Any help would be appreciated.
Thanks,
Simon
Simon
2008-04-04 17:22:23 UTC
Permalink
Is it possible to create a PSCredential object only having a username?

When I try using the DefaultNetworkCredentials like this:

System.Net.NetworkCredential cred =
System.Net.CredentialCache.DefaultNetworkCredentials;
WriteObject(cred, true);

and then call the cmdlet in powershell, this is the result I get:


UserName
Password Domain
--------
-------- ------


I get the same empty strings if I just try to print out the Username
(for example) independently.

Thanks again,

Simon
Post by RichS [MVP]
I'm not aware of the password being available independently.
You can use
$cred = [System.Net.CredentialCache]::DefaultNetworkCredentials
to get the credentials of the logged on user
--
Richard Siddaway
Please note that all scripts are supplied "as is" and with no warranty
Blog:http://richardsiddaway.spaces.live.com/
PowerShell User Group:http://www.get-psuguk.org.uk
Simon
2008-04-04 19:17:16 UTC
Permalink
I'm also trying to track down a C# equivalent to get-credentials, so
that I can create a cmdlet that, when run, prompts the user for a
username and password. Does anybody know if something like this
exists?

Thanks,

Simon
Marco Shaw [MVP]
2008-04-05 00:14:34 UTC
Permalink
Post by Simon
I'm also trying to track down a C# equivalent to get-credentials, so
that I can create a cmdlet that, when run, prompts the user for a
username and password. Does anybody know if something like this
exists?
Thanks,
Simon
Lookup "PSHostUserInterface.PromptForCredential" either in the SDK or
your favorite search engine.
--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
Oisin (x0n) Grehan [MVP]
2008-04-04 20:57:35 UTC
Permalink
Post by Simon
Hi,
I'm creating a custom cmdlet that accesses a remtoe system, and
requires a username and password to do so.  I would like the cmdlet
(which I am writing in C#) to pick up the username and password of the
currently logged in user, and try these, and if they fail provide a
dialog like the get-credentials cmdlet does.
I have been hunting around though, and can't seem to find a way to get
the password of the current user.  I have found a couple ways to get
the username (and domain), but that is it.  Has anybody else done
something like this?  Any help would be appreciated.
Thanks,
Simon
Hi,

You won't be able to get the password of the currently logged in user.
Strictly speaking, even Windows doesn't know the [plaintext] password
of the currently logged in user, and neither does the domain
controller. Authentification systems go to great lengths using all
manner of cryptographic trickery to make this so.

If the remote machine requires a plaintext username and password, I'm
afraid you're out of luck. This is the kind of thing SSO (single-sign-
on) platforms are designed to help you with, but without knowing your
particular circumstances I cannot offer any advice. The only thing I
would suggest is to investigate if the remote system accepts Windows
Authentication, via either Kerberos or NTLM.

- Oisin

PowerShell MVP
http://www.nivot.org/
Loading...