Jump to content
Software FX Community

[Guest Blogger] Username/password with invoke-sql and invoke-webservice


Recommended Posts

A lesser known cmdlet is get-credential, which allows one to create a credential object containing a username and password. 

A lot of cmdlets, that might somehow require some kind of authentication, support the credential parameter, which will use this credential object.  Some examples of cmdlets that support the credential parameter are invoke-sql and invoke-webservice.

One way of using the credential object is to do something like this:


A window will popup asking for your username and password.  Once you have the $cred variable, you would use it like this to call one of the invoke cmdlets:

PSH>invoke-webservice -credential $cred ...
PSH>invoke-sql -credential $cred ...

This allows you to use services that require authentication.  You can also use this method to store a username and password to a file for later use when invoking the cmdlet again.  In other words, you could create a credential object, store that in a file, and later retrieve it.  I'll blog about how one can do that at a later time along with the pros and cons of that method.

[Edit: October 3rd, 2007: It is possible to block the Windows popup, and enter your username and password directly into
PowerShell. Oisin Grehan posted a quick command in the PowerShell NNTP group that you can run to set this up:
new-itemproperty hklm:\software\microsoft\powershell\1\shellids -name consoleprompting -propertytype string -value true]

Link to comment
Share on other sites

  • Create New...