13

INITIAL QUESTION

How Can I perform Invoke-WebRequest or similar, with Powershell so that NTLM authentication is used but also supply a body for a post.

EXAMPLE

The code sample below is my example post using invoke web request and pipes response out to a .json file. Username and Password Variable not included in example.

$myURL = https://example.blah.etc
$params = @" {""EXAMPLE1":"STUFF"} "@ 

$Headers = @{ Authorization = "Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $Username,$Password))) }


Invoke-WebRequest -Uri $myURL  -Headers $Headers -Method POST -ContentType "application/json" -Body  $params  | Select-Object -ExpandProperty Content > "C:\output.json"

UPDATE

Using -UseDefaultCredentials only works for Gets, not for posts.



ERROR RESPONSE

The remote server returned an error: (401) Unauthorized

Murchie85
  • 815
  • 2
  • 12
  • 27
  • There has been some work to improve the web request cmdlets in version 6.0. See thread here: https://github.com/PowerShell/PowerShell/issues/4274 and documentation here: https://learn.microsoft.com/en-us/powershell/module/Microsoft.PowerShell.Utility/Invoke-RestMethod?view=powershell-6 – veefu Jan 26 '18 at 16:31
  • 2
    `Using -UseDefaultCredentials only works for Gets, not for posts.` -- You can perform NTLM-auth with GET, save the session to a variable (use `-SessionVariable`) and then do POST using saved session. – n01d Jun 14 '18 at 08:17

1 Answers1

16

Just use -UseDefaultCredentials.

Trying to manipulate the headers for NTLM is hard work. It is a challenge response that is painful. Let PS do the work...

Seki
  • 11,135
  • 7
  • 46
  • 70
markgamache
  • 436
  • 2
  • 6