Skip to content

Instantly share code, notes, and snippets.

@kensykora
Last active August 29, 2015 14:00
Show Gist options
  • Save kensykora/ad00ac5dc05a3a6ca6f9 to your computer and use it in GitHub Desktop.
Save kensykora/ad00ac5dc05a3a6ca6f9 to your computer and use it in GitHub Desktop.
Goes through the motions to log into umbraco, right click the root node, click "Republish All Content", and then click the button to confirm
# Clear Umbraco Cache
#
# Simulates a set of HTTP requests that a normal use would do to log into umbraco and clear the global site cache (republish the root node)
#
# == Usage ==
# .\clear-umbraco-cache.ps1 -HostName Hostname -User Username -Pass Password [-UseSsl $false]
#
# == Params ==
# Hostname: Hostname of the server running umbraco
# User / Pass: Valid credentials for an Umbraco User
# UseSSL: (Optional) Enable or Disable SSL (defaults to True)
#
# == Example Usage ==
# .\clear-umbraco-cache.ps1 -HostName dev.americanclubresort.com -User ksykora -Pass #######
#
param ([Parameter(Mandatory=$True)][string]$hostname,
[Parameter(Mandatory=$True)][string]$user,
[Parameter(Mandatory=$True)][string]$pass,
[bool]$UseSsl = $true)
$MAX_ATTEMPTS = 10
$TIME_BETWEEN_ATTEMPTS_SECONDS = 5
$ErrorActionPreference = "Stop"
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
function Clear-UmbracoCache([string]$hostname,
[string]$username,
[string]$password,
[bool]$ssl = $true) {
$address = "http{0}://{1}" -f @{$true="s";$false=""}[$ssl], $hostname
if(-not $ssl) {
$sslChar = ""
}
#Get login form
$req = Invoke-WebRequest -Uri "$address/umbraco/login.aspx" -SessionVariable session -TimeoutSec 300
$attempts = 1
while(($req.Forms.Length -eq 0 -or (-not $req.Forms[0].Action.EndsWith("login.aspx"))) -and $attempts -le $MAX_ATTEMPTS) {
#If there was a page returned that didn't include a form, attempt to wait and retry
Write-Host "attempt $attempts failed. Waiting $TIME_BETWEEN_ATTEMPTS_SECONDS seconds"
Start-Sleep -Seconds $TIME_BETWEEN_ATTEMPTS_SECONDS
$req = Invoke-WebRequest -Uri "$address/umbraco/login.aspx" -SessionVariable session -TimeoutSec 300
$attempts = $attempts + 1
}
if($req.Forms.Length -eq 0 -or (-not $req.Forms[0].Action.EndsWith("login.aspx"))) {
throw "Unable to find a form to log into"
}
$form = $req.Forms[0]
$form.Fields["lname"] = $username
$form.Fields["passw"] = $password
#Submit login form
$submitLoginRequest = Invoke-WebRequest -Uri "$address/umbraco/login.aspx" -Method "Post" -Body $form.Fields -WebSession $session -TimeoutSec 300
$form = $submitLoginRequest.Forms[0]
if(-not $form.Action.EndsWith("umbraco.aspx")) {
throw "Unable to log in"
}
$repubRequest = Invoke-WebRequest -Uri "$address/umbraco/dialogs/republish.aspx" -WebSession $session -TimeoutSec 300
$form = $repubRequest.Forms[0]
$form.Fields["ctl00`$body`$bt_go"] = "Republish entire site"
$response = Invoke-WebRequest -Uri "$address/umbraco/dialogs/republish.aspx" -Method "Post" -WebSession $session -Body $form.Fields -TimeoutSec 300
if($response.Content.Contains("The website cache has been refreshed.")) {
Write-Host "Cache Cleared"
} else {
Write-Host $response
throw "Did not successfully clear cache"
}
}
Clear-UmbracoCache -hostname $Hostname -username $user -password $pass -ssl $UseSsl
[System.Net.ServicePointManager]::CertificatePolicy = $null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment