Website Check (Invoke webrequest) - URL in csv file changes everytime I run the script due to PINGID SSO randomize URL design.

shaider007 101 Reputation points
2022-03-25T08:22:37.65+00:00

In my script the url for example is www.example.123456789 that I need to check, after I ran the script. from the output in csv file, the www.example.123456789 will appear differently this is due to PINGID SSO randomized URL. Is there a solution for that like, in the URL I can add the name of the URL that when I run, it will not be read but only the URL but it will show on CSV? Or do you have a better suggestion please? as if the URL changed every time it runs, it will be difficult to identify what website it is from the CSV file. Thanks

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,435 questions
{count} votes

Accepted answer
  1. MotoX80 32,531 Reputation points
    2022-03-27T15:18:56.317+00:00

    On line 11, use the $uri variable like you use on line 24. Your script is currently using the redirected address from the response. If you don't want that, then use the address that you used on the Invoke-WebRequest.

    187248-capture.png


2 additional answers

Sort by: Most helpful
  1. shaider007 101 Reputation points
    2022-03-27T13:23:07.93+00:00

    Hi @Andreas Baumgarten , Below is the code I use and please see the output at the bottom part. Thanks

     $Uris = Get-Content -Path "C:\Check\Websites.txt"  
     $PathTxt = "C:\Check\\Uri.txt"  
     $PathCsv = "C:\Check\\Uri.csv"  
     $uriList = @()  
          
       foreach ($uri in $Uris) {  
         $uriObject = New-Object PSObject  
         $Response = ""  
         try {  
         $Response = Invoke-WebRequest -UseDefaultCredentials -Uri $uri -ErrorAction SilentlyContinue -UseBasicParsing -DisableKeepAlive  
         $uriObject | Add-Member -MemberType NoteProperty -Name "URL" -Value $Response.BaseResponse.ResponseUri  
         $uriObject | Add-Member -MemberType NoteProperty -Name "Status" -Value $Response.StatusCode  
         if ($($Response.StatusDescription)) {  
         $uriObject | Add-Member -MemberType NoteProperty -Name "Description" -Value $Response.StatusDescription  
         }  
        else  {  
        $uriobject | Add-Member -MemberType NoteProperty -Name "Description" -Value "OK"  
         }  
         Write-Host $Response.StatusCode $Response.StatusDescription $Response.BaseResponse.ResponseUri -ForegroundColor Green  
         $uriList += $uriObject  
       }  
       catch {  
         Write-Host "Unable to reach $uri" -ForegroundColor Red  
         $uriObject | Add-Member -MemberType NoteProperty -Name "URL" -Value $uri  
         $uriObject | Add-Member -MemberType NoteProperty -Name "Status" -Value "Not Responding"  
         $uriObject | Add-Member -MemberType NoteProperty -Name "Description" -Value "Error"  
         $uriList += $uriObject  
       }  
     }  
          
     $uriList | Out-File -FilePath $PathTxt  
     $uriList | Export-Csv -Path $PathCsv -NoTypeInformation  
    

    Below left is the Website.txt and the one on the right (yellow) is the Uri.csv that changes randomly when I ran the website check. This is due to PINGID SSO feature. I want to have a solution wherein if I ran the script I would easily identify that the one on csv file is www.google.com.

    187247-url.jpg


  2. shaider007 101 Reputation points
    2022-03-27T18:26:06.847+00:00

    Hi @MotoX80 , thanks for you advice. It's way better now. It shows the exact link on the Description. Unlike before some random links that is hard to identify. Thanks a lot! :-)

    0 comments No comments