Invoke-webrequest

Jerryzy 10,566 Reputation points
2020-08-10T07:08:27.74+00:00

Hi All,

I am executing the powershell script Invoke-webrequest for few sharepoint sites.

Among 10 SharePoint sites, 1 sharePoint site gave the output as status code "200" and status description "ok". This is a separate webapplication than the others.

But in other SharePoint sites, the output is "401 - Unauthorized"

However when I added "usedefaultcredentials" i.e the script becomes Invoke-webrequest -usedefaultcredentials, then all the 9 sharePoints are giving output as "ok".

Please let me know why it happens for these 9 sites. how should I make them run successfully giving status code "200" without using "usedefaultcredentials".

Actually the same site is giving 2 different status codes when invoked as webrequest in Powershell. However when I am accessing the site from browser, its asking for the credential.

I am running the below script for the same URL and intermittently it is giving status as "401 Unauthorized" and also sometimes returning as "200 - Ok":

Invoke-webrequest <SiteURL>

Can you please help me with the reason of this intermittent issue for the same site.

Source from TechNet

SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,862 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jerryzy 10,566 Reputation points
    2020-08-10T09:47:05.337+00:00

    Hi,

    Instead of using UseDefaultCredentials parameter, I suggest you can pass valid credential like this:

    $cred = Get-Credential
    Invoke-WebRequest -Uri 'https://sp' -Credential $cred

    And You can check if the password for the speicific site existed in Windows Credential Manager, maybe the credential is invalid when use UseDefaultCredentials parameter.

    0 comments No comments