10

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
PatrickNolan
  • 1,671
  • 2
  • 20
  • 40
  • Check out [Creating a PS Credential from a Clear Text Password in Powershell](https://blogs.technet.microsoft.com/gary/2009/07/23/creating-a-ps-credential-from-a-clear-text-password-in-powershell/) on the MSDN. – Tomalak Sep 15 '18 at 20:15

1 Answers1

12

You may set the UseDefaultCredentials property of Invoke-WebRequest module to true. Link to the official doc: https://learn.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)
lalibi
  • 3,057
  • 3
  • 33
  • 41
ImGroot
  • 796
  • 1
  • 6
  • 17
  • Thanks for your response I am using UseDefaultCredentials in my original post. I need to use WindowsAuthentication but specify the Username\Password for the account I want to use. I do not want to use the default credentials associated with the process executing the powershell script. – PatrickNolan Sep 15 '18 at 21:54
  • Then, you might need to use `-Authentication` along with `-Credential` props – ImGroot Sep 15 '18 at 21:58