none
Testing a list of URL with Powershell behind Autoproxy RRS feed

  • General discussion

  • We are preparing to migrate a large user community from one proxy service to another. We are going to do this user communities at a time and as we start with our power users, I want to be able to give them a script which will import their bookmarks, find all of the URL in it, then test every URL to make sure they all work. I want them to be able to do this before we migrate them, then run it again after we migrate them to make sure everything that worked with the old proxy is still working.

    I have written the following script and Im having a number of challenges.

    First, it seems to randomly fail going through the proxy. I test a URL in the Explorer browser on my desktop and I get to the URL. I test the same URL in my script and I get:

    Exception calling "GetResponse" with "0" argument(s): "The operation has timed out"

    Second, we set the proxy.pac to be used for the user in Group Policy. If the script takes longer than ~15 minutes to run, the GP refreshes the autoproxy configuration and suddenly things change, so I really need to improve the intelligence of the script so if for some reason the value of

    (get-itemproperty'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').AutoConfigURL

    changes, the script stops.

    This is the current state of my script. Any assistance you all may be able to provide would be most appreciated.

    cd \Proxy_Test;
    
    $urls = Get-Content .\whitelist.txt
    $Today = (Get-Date -format "MMddyyyyHHmmss")
    
    Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"  >> .\$Today.response.txt;
    
    #echo "Command 1";
    $Proxy = new-object System.Net.WebProxy
    #echo "Command 2";
    $proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').AutoConfigURL
    #echo "Command 3";
    $Proxy.Address = $proxyAddr
    #echo "Command 4";
    $Proxy.useDefaultCredentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    #echo "Command 5";
    $webClient = new-object System.Net.WebClient 
    #echo "Command 6";
    $webClient.Headers.Add(“user-agent”, “Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko ”) 
    #echo "Command 7";
    $webClient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
    
    foreach($URI in $urls){
    try {
       echo " ";
       echo $URI;
       echo $URI >> .\$Today.response.txt;
      $HttpWebResponse = $null;
      $HttpWebRequest = [System.Net.HttpWebRequest]::Create("$URI");
      $HttpWebRequest.Method = "HEAD"
      $HttpWebRequest.Timeout = 30000 # = 30 seconds
      $HttpWebResponse = $HttpWebRequest.GetResponse();
      $NOW = (Get-Date -format "HH:mm:ss")
      echo $NOW >> .\$Today.response.txt;
      if ($HttpWebResponse) {
        Write-Host -Object $HttpWebResponse.StatusCode.value__;
         Write-Output -Object $HttpWebResponse.StatusCode.value__ >> .\$Today.response.txt;
        Write-Host -Object $HttpWebResponse.GetResponseHeader("X-Detailed-Error");
         Write-Output -Object $HttpWebResponse.GetResponseHeader("X-Detailed-Error") >> .\$Today.response.txt;
             (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').AutoconfigURL >> .\$Today.response.txt; 
             $NOW = (Get-Date -format "HH:mm:ss")
             echo $NOW >> .\$Today.response.txt;
         echo " " >> .\$Today.response.txt;
      }
    }
    catch {
      $ErrorMessage = $Error[0].Exception.ErrorRecord.Exception.Message;
      $Matched = ($ErrorMessage -match '[0-9]{3}')
      if ($Matched) {
       Write-Host -Object ('HTTP status code was {0} ({1})' -f $HttpStatusCode, $matches.0);
       Write-Output -Object ('HTTP status code was {0} ({1})' -f $HttpStatusCode, $matches.0) >> .\$Today.response.txt;
       (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').AutoconfigURL >> .\$Today.response.txt;
       $NOW = (Get-Date -format "HH:mm:ss")
       echo $NOW >> .\$Today.response.txt;
       echo " " >> .\$Today.response.txt;
      }
      else {
      echo "Error Message: "
        Write-Host -Object $ErrorMessage;
        Write-Output -Object $ErrorMessage >> .\$Today.response.txt;
        (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').AutoconfigURL >> .\$Today.response.txt;
        $NOW = (Get-Date -format "HH:mm:ss")
        echo $NOW >> .\$Today.response.txt;
        echo " " >> .\$Today.response.txt;
                  }
     }
    }
    #echo "END Command 1";
    echo " ";
    #echo "END Command 2";
    (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').AutoconfigURL  >> .\$Today.response.txt;
    $NOW = (Get-Date -format "HH:mm:ss")
    echo $NOW >> .\$Today.response.txt;
    echo "EXIT";

    Example whitelist.txt

    http://asq.org
    http://search.officemax.com
    http://secure.asq.org
    http://settings.data.microsoft.com
    http://sourceforge.net
    http://slashdot.org
    http://support.adp.com
    http://support.rackspace.com
    http://theregister.co.uk
    http://www.ups.com
    http://www.weather.com
    http://www.yahoo.com
    http://www.officedepot.com
    http://xsi.outlook.com

    Thank you!

    E.



    Friday, May 27, 2016 12:21 PM