Warm-up script with Powershell
I used my previous blog to perform a SQL Stored Procedure in Powershell to get my most recent visited sites. Only these sites need to be warmed up. It is common use, so first of all credits to JonTheNerd, I just tweeked it al little bit.
Here we go.
function Get-WebPage([string]$url)
{
$bypassonlocal = $false
$proxyuri = "http://" + $env:COMPUTERNAME
$proxy = New-Object system.Net.WebProxy($proxyuri, $bypassonlocal)
$wc = new-object net.webclient
$wc.credentials = [System.Net.CredentialCache]::DefaultCredentials
$wc.proxy = $proxy
$pageContents = $wc.DownloadString($url)
$wc.Dispose()
return $pageContents
}
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=DB_Server;Database=DB_Name;Integrated Security=true"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "DB_SP_GetRecentSites"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) | Out-Null
$SqlConnection.Close()
$DataSet.Tables[0];
write-host "`nSTART Warmup Script";
write-host "-------------------";
foreach ($Row in $DataSet.Tables[0].Rows){
$wc = new-object net.webclient;
$wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;
$pageContents = $wc.DownloadString($Row.Site_URL);
$wc.Dispose();
}
write-host "FINISHED successfully!"
The code is self-explanatory.
Here we go.
function Get-WebPage([string]$url)
{
$bypassonlocal = $false
$proxyuri = "http://" + $env:COMPUTERNAME
$proxy = New-Object system.Net.WebProxy($proxyuri, $bypassonlocal)
$wc = new-object net.webclient
$wc.credentials = [System.Net.CredentialCache]::DefaultCredentials
$wc.proxy = $proxy
$pageContents = $wc.DownloadString($url)
$wc.Dispose()
return $pageContents
}
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server=DB_Server;Database=DB_Name;Integrated Security=true"
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = "DB_SP_GetRecentSites"
$SqlCmd.Connection = $SqlConnection
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet) | Out-Null
$SqlConnection.Close()
$DataSet.Tables[0];
write-host "`nSTART Warmup Script";
write-host "-------------------";
foreach ($Row in $DataSet.Tables[0].Rows){
$wc = new-object net.webclient;
$wc.credentials = [System.Net.CredentialCache]::DefaultCredentials;
$pageContents = $wc.DownloadString($Row.Site_URL);
$wc.Dispose();
}
write-host "FINISHED successfully!"
The code is self-explanatory.
- Get your recent sites
- Loop through your result
- Setup a new webclient
- Set the credentials
- Get the page content from the URL (this is pure HTML, funny to see the output of this)
Hope this was usefull.
Reacties
Een reactie posten