How to validate login credentials in PowerShell script

Prabha 241 Reputation points
2021-10-19T18:59:40.517+00:00

I have provided the root url, it is validating the timeout, then it is checking for suburl (login page) and after that validating login credentials using basic auth. How to check whether the login page( rooturl+relativeurl) is logged in successfully or else should throw an error. Do we need to change anything else in the code, please shed some suggestions.

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

1 answer

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-10-19T21:36:49.327+00:00

    Let me ask you a question: does your initial Invoke-WebRequest return a 401 status? If not, if you provide a -Credential value on the initial Invoke-WebRequest does it accept the credential or does it respond with a 401 status?

    Line 5 and 8: $authenticationDetails is undefined.
    Line 12: missing -ErrorAction STOP
    Line 15: if the Invoke-WebRequest is successful you still send "Page is down" to the screen.
    Line 15: if the Invoke-WebRequest throws an error, the script aborts.
    Line 23 and 25: The seem to be a duplicate (with the exception that line 25 doesn't store the result and uses a timeout)
    Line 32: You note an error and say there's a status code but you never provide one.

    Consider adding "Set-StrictMode -Version Latest" at the beginning of your script. Correct any problems it complains about.


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.