I am working on the following code.
$HTTP_Request =[System.Net.WebRequest]::Create('http://google.com')
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode
echo $HTTP_Status
But I want to run it using my default credentials, because there are few URLs which returns 401 ,that is client not authorized and for that I need it to run using my default credentials.
Can anyone help me regarding same, as I want to store the status of some URLs and this code is working fine except for those which are protected.
So the question was to run web request while using default credentials, here are the solutions I found:
for powershell 2.0:-
$req = [system.Net.WebRequest]::Create($uri)
$req.UseDefaultCredentials = $true
try
{
$res = $req.GetResponse()
}
catch [System.Net.WebException]
{
$res = $_.Exception.Response
}
$int = [int]$res.StatusCode
echo $int
For Powershell 3.0:-
try{
$res = Invoke-WebRequest $uri -UseDefaultCredentials
}
catch [System.Net.WebException]
{
$res = $_.Exception.Response
}
$int = [int]$res.StatusCode
echo $int
Both scripts are extremely doing well but if you want to find code status of many URLs, then you should go for powershell 3.0 , it handles web-requests in a much better way.