|
#Parameters |
|
$logFolder = "C:\Temp\logs" |
|
$logFile = "$logFolder\Warmup_$(Get-Date –Format "MM-dd-yyyy_hh-mm-ss").log" |
|
$timeoutSec = 10 |
|
$headers = @{ |
|
SOAPAction= ""; |
|
"Accept-Encoding" = "gzip,deflate"; |
|
"User-Agent" = "PowerShell/Warmup-WebApp" |
|
} |
|
$webRequests = @( |
|
@{ |
|
SOAPAction = "http://tempuri.org/IService1/Operation1"; |
|
Body = @" |
|
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> |
|
<soapenv:Body> |
|
<tem:Operation1> |
|
<tem:Parameter1>1</tem:Parameter1> |
|
<tem:Parameter2>2</tem:Parameter2> |
|
</tem:Operation1> |
|
</soapenv:Body> |
|
</soapenv:Envelope> |
|
"@ |
|
}, |
|
@{ |
|
SOAPAction = "http://tempuri.org/IService1/Operation2"; |
|
Body = @" |
|
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/"> |
|
<soapenv:Body> |
|
<tem:Operation1> |
|
<tem:Parameter1>1</tem:Parameter1> |
|
<tem:Parameter2>2</tem:Parameter2> |
|
</tem:Operation1> |
|
</soapenv:Body> |
|
</soapenv:Envelope> |
|
"@ |
|
} |
|
) |
|
|
|
# Functions |
|
function Write-Log([string]$level, [string]$text) { |
|
$logTime = Get-Date –Format "yyyy-MM-dd HH:mm:ss.fff" |
|
Write-Host "[$level] $text" |
|
if ($logFile -ne $null) { |
|
"$logTime [$level] $text" | Out-File –FilePath $Logfile –Append |
|
} |
|
} |
|
function Cleanup-LogFiles() { |
|
$expiryDate = (Get-Date).AddMinutes(-7) |
|
[bool]$logFileDeleted = $false |
|
$filesToDelete = Get-Childitem $logFolder –Include "*.log" –Recurse | ? {$_.LastWriteTime -le $expiryDate} | % { |
|
Remove-Item $_.FullName | out-null |
|
Write-Log "Information" "File deleted successfully – $_" |
|
$logFileDeleted = $true |
|
} |
|
if (-not $logFileDeleted) { |
|
Write-Log "Information" "No old log file was found in $logFolder" |
|
} |
|
} |
|
function Warmup-WcfService($serviceUrl) { |
|
foreach($webRequest in $webRequests) { |
|
$headers["SOAPAction"] = $webRequest.SOAPAction |
|
Invoke-WcfService $serviceUrl $headers $webRequest.Body |
|
} |
|
} |
|
function Invoke-WcfService($uri, $headers, $body) { |
|
$invocationStartTime = Get-Date |
|
try { |
|
$result = Invoke-WebRequest –Uri $uri –Method Post –UseDefaultCredentials –ContentType "text/xml; charset=utf-8" –Headers $headers –body $body –TimeoutSec $timeoutSec |
|
$invocationDuration = (Get-Date).Subtract($invocationStartTime) |
|
if ($result.StatusCode -eq 200) { |
|
if ($result.Headers.ContainsKey("Content-Length")) { |
|
$contentLength = $result.Headers["Content-Length"] |
|
} else { |
|
$contentLength = "$($result.RawContentLength) (raw)" |
|
} |
|
Write-Log "Information" "WCF operation invoked successfully – Operation: $($webRequest.SOAPAction)($variation), Duration: $($invocationDuration), Length: $contentLength" |
|
} else { |
|
Write-Log "Warning" "WCF operation failed – Operation: $($webRequest.SOAPAction)($variation), Status: $($result.StatusCode) $($result.StatusDescription), Length: $contentLength" |
|
} |
|
} catch { |
|
Write-Log "Critical" "WCF operation failed – Operation: $($webRequest.SOAPAction)($variation), Exception: $_" |
|
} |
|
} |
|
function Invoke-WebPage([string]$uri, [bool]$getImages, [bool]$getScripts) { |
|
try { |
|
$response = Invoke-WebRequest –Uri $uri –UseDefaultCredentials –TimeoutSec $timeoutSec –Proxy "http://localhost" –ProxyUseDefaultCredentials |
|
if ($response.StatusCode -eq 200) { |
|
Write-Log "Information" "Web page invoked successfully – URL: $uri, Images: $($response.Images.Count), Scripts: $($response.Scripts.Count), Links: $($response.Links.Count)" |
|
} else { |
|
Write-Log "Warning" "Web page invocation failed – URL: $uri, Status: $($response.StatusCode) $($response.StatusDescription)" |
|
} |
|
} catch { |
|
Write-Log "Critical" "Web page invocation failed – URL: $uri, Exception: $_" |
|
} |
|
|
|
if ($getImages) { |
|
Fetch–Resources $uri ($response.Images | ? { $_.src } | % { $_.src }) |
|
} |
|
|
|
if ($getScripts) { |
|
Fetch–Resources $uri ($response.Scripts | ? { $_.src } | % { $_.src }) |
|
} |
|
} |
|
|
|
Function Fetch-Resources($baseUrl, $resources) { |
|
[uri]$uri = $baseUrl |
|
$rootUrl = $uri.Scheme + "://" + $uri.Authority |
|
|
|
$counter = 0 |
|
foreach ($resUrl in $resources) { |
|
if ($resUrl.StartsWith("http", [System.StringComparison]::OrdinalIgnoreCase)) { |
|
$fetchUrl = $resUrl |
|
} elseif (!$resUrl.StartsWith("/")) { |
|
$fetchUrl = $rootUrl + "/" + $resUrl |
|
} else { |
|
$fetchUrl = $rootUrl + $resUrl |
|
} |
|
|
|
# Progress |
|
Write-Progress –Activity "Opening " –Status $fetchUrl –PercentComplete (($counter/$resources.Length)*100) |
|
$counter++ |
|
|
|
# Execute |
|
try { |
|
$resp = Invoke-WebRequest –UseDefaultCredentials –UseBasicParsing –Uri $fetchUrl –TimeoutSec 120 |
|
Write-Log "Information" "Resource has been downloaded successfully – URL: $fetchUrl" |
|
} catch { |
|
Write-Log "Warning" "Resource failed to download – URL: $fetchUrl, Exception: $_" |
|
} |
|
} |
|
Write-Progress –Activity "Completed" –Completed |
|
} |
|
|
|
# Execution |
|
Write-Log "Information" "My Web Application Warmup Script – $(Get-Date)" |
|
Write-Log "Information" "Cleaning up old log files…" |
|
Cleanup–LogFiles |
|
Write-Log "Information" "Warming up WCF Services…" |
|
Warmup–WcfServices |
|
Write-Log "Information" "Warming up Front-End…" |
|
Invoke-WebPage "http://domain.com/area1/Page1.aspx" $true $true |
|
Invoke-WebPage "http://domain.com/area1/Page2.aspx" $true $true |
|
Write-Log "Information" "My Web Application Warmup Script Completed." |