Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use PowerShell invoke-webrequest with Windows Authentication and Username\Password

The following PowerShell invoke-webrequest works for me when the Windows Service I'm running it from has permission to call the webservice. However, this isn't always the case.

I need to the ability to use Windows Authentication but also set the account username\password for the call. Does anyone have some sample code for doing this?

Invoke-WebRequest -UseBasicParsing "url" -UseDefaultCredentials -Method GET
like image 255
PatrickNolan Avatar asked Sep 15 '18 20:09

PatrickNolan


People also ask

How does Invoke-WebRequest work?

The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page or web service. It parses the response and returns collections of links, images, and other significant HTML elements.

How do I pass credentials in PowerShell without prompt?

$cred = Get-Credential without asking for prompts in powershell.

How do I pass default credentials in PowerShell?

Just set the UseDefaultCredentials property of the WebClient to $true and that will use the credentials of the current user for authentication.


1 Answers

You may set the UseDefaultCredentials property of Invoke-WebRequest module to true. Link to the official doc: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-6

$url = "http://yourURL"
$wc = New-Object System.Net.WebClient
$wc.UseDefaultCredentials = $true
$response = $wc.DownloadString($url)
like image 86
ImGroot Avatar answered Sep 18 '22 10:09

ImGroot