Invoke-WebReqeust Caching Website

Welcome Forums General PowerShell Q&A Invoke-WebReqeust Caching Website

This topic contains 3 replies, has 2 voices, and was last updated by

 
Participant
3 months, 1 week ago.

  • Author
    Posts
  • #110974

    Participant
    Points: 1
    Rank: Member

    Hi all,

    i am writing  a script to test a specific ADFS URL.  (Federated Metadata). The way the script works is it firsts adds an entry to the Local computers hosts file with IP Address and Name and then uses Invoke-Webrequest to access the site and see if it responds.

    this works fine, However if i provide multiple IP Addresses to test where the first IP is correct and the second IP is incorrect, Invoke web request still passes with a response of 200. although the website cannot be reached on the second IP address.

    something is caching. how do i clear the Invoke-Webrequest cache

    I have already tried the

    -Headers @{"Cache-Control"="no-cache"}
    
    
    
    $FederatedMetadata = Invoke-WebRequest "https://$($ADFSNameSpace)/FederationMetadata/2007-06/FederationMetadata.xml" -Headers @{"Cache-Control"="no-cache"}
    if($FederatedMetadata.StatusCode -eq "200"){
    Write-Host "Connection successful to https://$($ADFSNameSpace)/FederationMetadata/2007-06/FederationMetadata.xml" -ForegroundColor Green
    $EXTFederatedMetadataTest = "PASS"
    }#END_IF
    else{
    Write-Host "Connection Failed to https://$($ADFSNameSpace)/FederationMetadata/2007-06/FederationMetadata.xml" -ForegroundColor RED
    $EXTFederatedMetadataTest = "FAIL"
    }#END_ELSE

    any suggestions would be greatly appreciated

    Thanks

  • #110998

    Participant
    Points: 1
    Rank: Member

    Hi Guys,
    i have found a work around for the above issue. for anyone who faces this problem in the future.
    you can use invoke-command

     $FederatedMetadata  = invoke-command -ComputerName localhost -ScriptBlock {Invoke-WebRequest https://$($args[0])/FederationMetadata/2007-06/FederationMetadata.xml  -TimeoutSec 5} -ArgumentList $($ADFSNameSpace) -ErrorAction SilentlyContinue
     if($FederatedMetadata.StatusCode -eq "200"){
                        Write-Host "Connection successful to https://$($ADFSNameSpace)/FederationMetadata/2007-06/FederationMetadata.xml" -ForegroundColor Green 
                        $EXTFederatedMetadataTest = "PASS"
                    }#END_IF    
                    else{
                        Write-Host "Connection Failed to https://$($ADFSNameSpace)/FederationMetadata/2007-06/FederationMetadata.xml" -ForegroundColor RED
                        $EXTFederatedMetadataTest = "FAIL"
    

    The above code is able to runs fine when the hosts file is updated

    Thanks

  • #110999

    Moderator
    Points: 102
    Team MemberHelping Hand
    Rank: Participant

    Can you provide more detail about how you're setting and removing the IPs from the hosts file?

  • #111005

    Participant
    Points: 1
    Rank: Member

    Hi James,
    within the script i have two functions Update-HostFileObject and Remove-HostFileObject

    Function Update-HostFileObject {
        [cmdletbinding()]
        param(
            [string]$IPAddress,
            [String]$DNSName
        )
    
        $HostsFile = "$($ENV:systemroot)\System32\drivers\etc\hosts"
    
        Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Updating: $($IPAddress) $($DNSName) on $($HostFile)"
        "`r$($IPAddress) $DNSName" | Add-Content -PassThru $HostsFile -ErrorAction stop
    
    }
    
    Function Remove-HostFileObject {
        [cmdletbinding()]
        param(
            [string]$IPAddress,
            [String]$DNSName
        )
    
        $HostsFile = "$($ENV:systemroot)\System32\drivers\etc\hosts"
        $HostFileContent = Get-Content $HostsFile -Raw 
    
        #Remove HostFile Entry
        if($HostFileContent -match "$($IPAddress) $($NSName)"){
    
         Write-Verbose "[$((get-date).TimeOfDay.ToString()) PROCESS ] Removing: $($IPAddress) $($DNSNAme) from $($HostFile)"
        $UpdatedHostFile = $HostFileContent -replace "$IPAddress $DNSName"
        Clear-Content $HostsFile
        $UpdatedHostFile | Add-Content $HostsFile -ErrorAction stop
    
        } #ENDIF
        else{
            Write-Host "Host entry $($IPAddress) $($DNSName) not found"
        }
        
    }
    
    

The topic ‘Invoke-WebReqeust Caching Website’ is closed to new replies.