Skip to content

Instantly share code, notes, and snippets.

@jenyayel
Created February 11, 2015 13:55
Show Gist options
  • Save jenyayel/ec03bfc8bce53344e049 to your computer and use it in GitHub Desktop.
Save jenyayel/ec03bfc8bce53344e049 to your computer and use it in GitHub Desktop.
Misc powershells
# recycle all local IIS's application pools
$appPools = get-wmiobject -namespace "root\MicrosoftIISv2" -class "IIsApplicationPool"
foreach ($pool in $appPools)
{
trap [Exception] {
Write-Host "ERROR"
continue
}
Write-Host "Recycling - " $pool.name "..." -NoNewline
$pool.Recycle()
Write-Host "recycled"
}
# hits URLs for warmup
$sites =@("https://google.com", "https://yahoo.com")
function hitUrl($url)
{
Start-Job -ScriptBlock {param($url, $requestTimeout)
$request = [System.Net.WebRequest]::Create($url)
$request.UseDefaultCredentials = $true
$response = $null
try
{
$response = [System.Net.HttpWebResponse]$request.GetResponse()
$status = [System.String]::Format("{0} ({1})", $response.StatusCode, [System.Convert]::ToInt32($response.StatusCode))
Write-Host "Hitted `"$url`": $status"
}
catch [System.Net.WebException]
{
Write-Host "Hitted `"$url`": $_"
}
finally
{
if($response -ne $null)
{
$response.Close()
}
}
} -Arg $url $requestTimeout
}
Write-Host "Running..." -NoNewline
$jobs = @()
foreach($site in $sites)
{
$job = hitUrl $site
$jobs += $job
#hitUrl $site
}
While (-not (Get-Job -State "Running") -eq $null) {
Write-Host "." -NoNewline
Start-Sleep 100
}
Write-Host ""
$output = ""
foreach($job in $jobs){
Receive-Job -Job $job -OutVariable $output
Write-Host $output
}
# starts multiple browser instance in incognito mode that shares nothing
# between them - no cookies, local storage, cache and etc...
$launchUrl = "http://localhost:60170/"
$chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
function startChrome($url, $xPosition) {
Write-Host "Starting chrome at [$url]..."
# tempory folder for chrome profile data
$tempDir = "$env:temp\" + [System.Guid]::NewGuid().ToString()
New-Item -Type Directory -Path $tempDir
$args = "$url --window-position=$xPosition,0 --window-size=350,800 --user-data-dir=`"$tempDir`" --new-window"
return start-process $chromePath $args -PassThru
}
if(-Not (Test-Path $chromePath))
{
Write-Host "Chrome was not found at $chromePath"
exit 1
}
$processes = @()
# lunch clients
$processes += startChrome $launchUrl 0
$processes += startChrome $launchUrl 350
$processes += startChrome $launchUrl 700
$processes += startChrome $launchUrl 1050
Read-Host
Write-Host "Killing apps..."
foreach ($process in $processes) {
if($process.gettype().name -eq "Process") {
$process.Kill()
$process.WaitForExit()
}
}
Write-Host "Cleanup temp folders..."
foreach ($process in $processes) {
if($process.gettype().name -eq "DirectoryInfo") {
Remove-Item $process -recurse
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment