Invoke-WebRequest with currently logged in user credentials?

Joe H 96 Reputation points
2023-05-12T14:52:09.3666667+00:00

I have an Azure App Service running Linux configured with Azure AD authentication per this Microsoft document.

Everything works fine if I access the site via a browser, as it handles all the necessary bits to authenticate as the user. I'm not sure how to go about authenticating as the current user (which is an AAD account) logged into Windows via Invoke-WebRequest in PowerShell though. I'm assuming I need to retrieve some kind of token to pass along with Invoke-WebRequest.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2023-05-13T13:18:02.9066667+00:00

    Try this switch.

    -UseDefaultCredentials

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

    https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-5.1

    Here's an example that shows the use of -SessionVariable and -WebSession parameters to connect to a site and then make subsequent requests.

    http://powershellcookbook.com/recipe/vODQ/script-a-web-application-session

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.