PoshCode Logo PowerShell Code Repository

Clear-TMGCachedUrl.ps1 by canoas 3 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/4896"></script>download | new post

Powershell Script to clear an Url from TMG/ISA Cache

  1. param(
  2.     $url = (Read-Host "Url? e.g http://www.maxfinance.pt/img/frame5.jpg")
  3. )
  4. begin {
  5. $tmgroot = new-object -comobject FPC.Root
  6. $tmgarray = $tmgroot.GetContainingArray()
  7. $myCache = $tmgArray.Cache.CacheContents
  8. $regex = ([a-zA-Z]{3,})://([\w-\.]+)(/[\w- ./?%&=]*)*?
  9. }
  10. process {
  11.     if($url -match $regex) {
  12.         $hostname = $Matches[2]
  13.         $ip =[System.Net.Dns]::GetHostAddresses($hostname) | select -ExpandProperty IPAddressToString
  14.         $cachedUrl = $url.Replace($Matches[0], "zttp://$ip/$hostname")
  15.         try {
  16.             $myCache.FetchUrl("", $cachedUrl ,0,0) #  "","zttp://83.240.174.194/www.maxfinance.pt/img/frame7.jpg",customTTL,fpcTtlIfNon
  17.             Write-host -ForegroundColor green "Done clearing $cachedUrl"
  18.         }
  19.         catch {
  20.             if($_.Exception.Message.Contains("The system cannot find the file specified")) {
  21.                 Write-Warning "Cache Hit not found!"
  22.             }
  23.             else
  24.             {
  25.                 throw
  26.             }
  27.         }
  28.     }
  29.     else
  30.     {
  31.         write-warning "Error parsing url"
  32.         $Matches
  33.     }
  34. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me