HopelessN00b HopelessN00b - 3 months ago 50
PowerShell Question

Is it possible for Invoke-WebRequest to transparently authenticate with the user credentials of the user running the script?

I've got a script that basically creates a user account on a number of systems, including Active Directory, and an IIS webserver that authenticates against that same Active Directory Domain.

Right now, if I just do my

Invoke-WebRequest
without specifying credentials, I get a 401 error, and if I use the credential switch, I have to specify or prompt for credentials. However, when it creates the Active Directory account, PowerShell is smart enough to uses the credentials of the user running the script to create the account, without having to specify or prompt for credentials.

Is it possible to do this same thing with
Invoke-WebRequest
?

Answer

As per TechNet for Invoke-WebRequest the -UseDefaultCredentials switch should do exactly what you are asking for:

Indicates that the cmdet uses the credentials of the current user to send the web request.