Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PSHostUserInterface.PromptForCredential

1,362 views
Skip to first unread message

paulmsmi...@gmail.com

unread,
Aug 19, 2008, 8:21:14 AM8/19/08
to
I'm implementing the PSHostUserInterface interface and am having some
problems with the PromptForCredential method.

1) What is the "targetName" parameter for? MSDN says "The name of the
target for which the credential is collected" which quite frankly is a
useless description.

2) Does anybody know of any cmdlets that will trigger a call to this
method. The get-credentials cmdlet seems to call the Prompt method
instead.

Thanks.

Marco Shaw [MVP]

unread,
Aug 19, 2008, 11:08:40 AM8/19/08
to

I'd have to fire up a couple of code examples to double-check, but it
looks almost like "targetName" may remove the requirement that a
username must also be entered.

read-host likely uses the Prompt() method. It only returns a string
object, and that definitely can't be used as a credential object with
other cmdlets that support it.

PS > $test=read-host "Ask"
Ask: Testing
PS > $test.gettype().fullname
System.String
PS > $test1=get-credential

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
User: foo
Password for user foo: ***

PS > $test1.gettype().fullname
System.Management.Automation.PSCredential
PS >

Note: There are 2 overloads for the method:
http://msdn.microsoft.com/en-us/library/system.management.automation.host.pshostuserinterface.promptforcredential%28VS.85%29.aspx

Marco

--
*Microsoft MVP - Windows Server - Admin Frameworks
https://mvp.support.microsoft.com/profile/Marco.Shaw
*PowerShell Co-Community Director - http://www.powershellcommunity.org
*Blog - http://marcoshaw.blogspot.com

jeremyl...@gmail.com

unread,
Sep 2, 2013, 10:16:55 PM9/2/13
to
I know this is old but I needed to post a slightly better explanation since this thread came up in my Googling for an answer to the same question.

1 - Think of TargetName as the "domain" portion of the credentials. If you use "contoso.com" as the TargetName and the user enters their username as "john.doe" then the resulting PSCredential object will have a UserName property with a value of "contoso.com\john.doe". However they can enter their own domain...if the user enters "seconddomain\john.doe" then that will be the complete UserName property value. This applies to the 4-parameter version of this method. There is a 6 parameter version of the method which may behave differently depending on the allowedCredentialTypes parameter you specify...sorry I am not sure.

I don't like to use this parameter because it prevents the user from entering a blank domain. Instead I set TargetName to "" and I enter the default/suggested domain as part of the UserName parameter. This way I allow the user to delete it if needed.

2 - I'm not aware of a Cmdlet. I just run it like this sample code:


PS > $Creds = $Host.UI.PromptForCredential("Caption","Message",$Env:USERDOMAIN + '\' + $Env:USERNAME,"")

You can see what I meant about entering the suggested domain (I use the current user's domain by default)

smprf...@gmail.com

unread,
Oct 5, 2015, 3:05:07 PM10/5/15
to
Sorry for the Necro post, but This was awesome, exactly what I was looking for. is there any way to take the credentials entered and create a new user with them?

Thanks,
Matt
0 new messages