Created
August 3, 2011 06:03
-
-
Save toddb/1122007 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Basic tasks for SharePoint. These wrap many powershell commandlets which littles bits and pieces that actually work. They | |
are particularly designed for reuse with psake scripts. Belows is a sample psake script for deploying a sharepoint. | |
For building and package see: . .\scripts\build-tasks.ps1 # see gist: https://gist.github.com/1108813 | |
$framework = '3.5x64' | |
. .\scripts\sharepoint-tasks.ps1 | |
Properties { | |
$base_dir = resolve-path . | |
$solution_dir = "$base_dir\wsp" | |
$solution = "MyWsp.wsp" | |
$application = "http://mysomething" | |
$application_global_deployment = $true | |
$path = "$solution_dir\$solution" | |
$site_location_key = "MyUrl" | |
$environment_code_key = "EnvironmentCode" | |
$default_page = "/default.aspx" | |
} | |
Task Deploy -Depends Solution-UnInstall, Solution-Remove, Solution-Add, Solution-Install, Deployment-Wait | |
Task Install -Depends Logging-Start, Deploy, Solution-Installed, Logging-Stop | |
Task Solution-Installed { | |
Check-SolutionInstalled $solution | |
} | |
Task Solution-Remove -ContinueOnError { | |
Remove-Solution $solution | |
} | |
Task Solution-Add { | |
Add-Solution $path $solution | |
} | |
Task Solution-Install { | |
Install-Solution $solution $application $false | |
} | |
Task Solution-UnInstall -ContinueOnError { | |
Uninstall-Solution $solution $application $application_global_deployment | |
} | |
Task Deployment-Wait -Description "Waits for the site to come alive after deployment" { | |
WaitFor-Site $application $default_page | |
} | |
Task Logging-Start { | |
Start-Transcript -Path .\install.log | |
} | |
Task Logging-Stop { | |
Stop-Transcript | |
} | |
#> | |
function Set-EnvironmentAppUrl($site_location_key, $application){ | |
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" | |
Write-Host "Setting application url for environment: $application" | |
$farm = Get-SPFarm | |
$farm.Properties[$site_location_key] = $application | |
$farm.Update() | |
} | |
function Clobber-Solution($solution, $application, $application_url, $application_global_deployment, $jobName){ | |
Write-Host "Clobbering solution: $solution" | |
Uninstall-Solution $solution $application $application_url $application_global_deployment | |
Remove-Solution $solution $jobName | |
} | |
function Uninstall-Solution($solution, $application, $application_global_deployment){ | |
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" | |
if ($jobName -eq $null) { $jobName = "*solution-deployment-$solution*" } | |
WaitFor-Solution($jobName) | |
Write-Host "Uninstalling solution: $solution" | |
$soln = Get-SPSolution $solution -ErrorAction SilentlyContinue | |
if($soln -ne $null) { | |
if ($soln.Deployed){ | |
if($application -eq $null -or $application_global_deployment -eq $true) { | |
Uninstall-SPSolution -identity $solution -confirm:$false | |
} else { | |
Uninstall-SPSolution -identity $solution –webapplication $application -confirm:$false | |
} | |
} | |
} else { | |
Write-Warning "Solution is not installed: $solution" | |
} | |
} | |
function Install-Solution($solution, $application, $application_global_deployment){ | |
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" | |
if ($jobName -eq $null) { $jobName = "*solution-deployment-$solution*" } | |
WaitFor-Solution($jobName) | |
Write-Host "Installing solution: $solution" | |
if($application -eq $null -or $application_global_deployment -eq $true) { | |
Install-SPSolution -identity $solution –GACDeployment -force:$true | |
} else { | |
Install-SPSolution -identity $solution –webapplication $application –GACDeployment -force:$true | |
} | |
} | |
function Add-Solution($path_to_wsp, $solution, $jobName){ | |
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" | |
Write-Host "Adding solution: $path_to_wsp" | |
if ($jobName -eq $null) { $jobName = "*solution-deployment-$solution*" } | |
WaitFor-Solution($jobName) | |
Add-SPSolution -LiteralPath $path_to_wsp | |
} | |
function Remove-Solution($solution, $jobName){ | |
Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue" | |
if ($jobName -eq $null) { $jobName = "*solution-deployment-$solution*" } | |
WaitFor-Solution($jobName) | |
Write-Host "Removing solution: $solution" | |
Write-Host(Get-SPSolution $solution -ErrorAction SilentlyContinue) | |
Get-SPSolution $solution -ErrorAction SilentlyContinue | Remove-SPSolution -confirm:$false -ErrorAction SilentlyContinue | |
} | |
function Add-SolutionDependencies($wsps, $path_to_wsp, $application_global_deployment){ | |
if ($application_global_deployment -eq $null) { $application_global_deployment = $true } | |
Write-Host 'Adding solution dependencies' | |
Foreach ($wsp in $wsps) | |
{ | |
$soln = Get-SPSolution | where-object {$_.Name -eq $wsp} | |
if ($soln -eq $null) { | |
Add-Solution "$path_to_wsp\$wsp" $soln | |
} | |
if ($soln -eq $null -or !$soln.Deployed) { | |
Install-Solution $wsp $null $application_global_deployment | |
} | |
} | |
} | |
function Remove-SolutionDependencies($wsps, $application_global_deployment){ | |
if ($application_global_deployment -eq $null) { $application_global_deployment = $true } | |
Write-Host 'Uninstalling dependent solutions' | |
$solution_dependent | % { | |
Clobber-Solution $_ $null $null $application_global_deployment $null | |
} | |
} | |
function WaitFor-Solution($jobName){ # $jobname --> "*solution-deployment-$solution*" | |
$job = Get-SPTimerJob | ?{ $_.Name -like $jobName } | |
if ($job -ne $null) | |
{ | |
$JobFullName = $job.Name | |
Write-Host -NoNewLine "Waiting to finish job $JobFullName " | |
while ((Get-SPTimerJob $JobFullName) -ne $null) | |
{ | |
Write-Host -NoNewLine . | |
Start-Sleep -Seconds 2 | |
} | |
Write-Host " Complete" | |
} | |
} | |
function WaitFor-Deployment(){ | |
$siteUp = $false | |
write-host "Waiting for site to come alive - this may take several minutes" | |
Get-Date | write-host | |
$centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | where {$_.IsAdministrationWebApplication} | |
$count = 0 | |
while (!$siteUp -and $count -le 20) | |
{ | |
try | |
{ | |
$wc = new-object System.Net.Webclient | |
$wc.UseDefaultCredentials = $true | |
$wc.DownloadString($centralAdmin.Url) | out-null | |
$siteUp = $true | |
} | |
catch [Exception] | |
{ | |
Write-Host -NoNewLine . | |
Start-Sleep -s 5 | |
$count = $count + 1 | |
} | |
} | |
Get-Date | write-host | |
} | |
function WaitFor-Site($application, $default_page, $tries, $sleepTime){ | |
$siteUp = $false | |
Write-Host "Waiting for site to come alive before running migrations, may take several minutes" | |
Write-Host "Trying site: $application" | |
Write-Host "Trying site: $application$default_page" | |
Get-Date | Write-Host | |
$wc = new-object net.webclient | |
$wc.UseDefaultCredentials = $true | |
if ($tries -eq $null) { $tries = 180 } | |
if ($sleepTime -eq $null) { $sleepTime = 5 } | |
$count = 0 | |
while (!$siteUp -and $count -le $tries) | |
{ | |
try | |
{ | |
$wc.DownloadString($application) | out-null | |
$siteUp = $true | |
} | |
catch [Exception]{ } | |
try | |
{ | |
$wc.DownloadString($application + $default_page) | out-null | |
$siteUp = $true | |
} | |
catch [Exception] { | |
Write-Host -NoNewLine . | |
Start-Sleep -s $sleepTime | |
$count = $count + $sleepTime | |
} | |
} | |
Get-Date | Write-Host | |
} | |
function Test-Deployment($url){ | |
$client = new-object System.Net.WebClient | |
$client.UseDefaultCredentials = $true | |
Write-Host "Looking for atom feed: $url" | |
$blog = [xml]$client.DownloadString($url) | |
$count = 0 | |
$blog.feed.entry | % { $count++ } | |
Write-Host "total number of tests: $count" | |
$failedcount = 0 | |
$blog.feed.entry | % { if ($_.content."#text" -match "Failed" ){ $failedcount++; write-warning $_.title."#text"} } | |
Write-Host "Failed tests: $failedcount" | |
$found = $true | |
$blog.feed.entry | % { if ($_.title."#text" -match "Versions"){ $found = $found -and $_.content."#text" -match "$revision"; if (!$found) {$failedcount++; write-warning $_.title."#text" } }} | |
Write-Host "Looking for revision: $found" | |
if ($failedcount -gt 0 -or $found -eq $false) { | |
$Error = "Some deployment tests have failed ($failedcount out of $count)" | |
Write-Error "Some deployment tests have failed ($failedcount out of $count)" | |
exit 1 | |
} | |
} | |
function Create-WebApplication($application, $application_name, $site_post, $application_pool, $application_pool_account, $database, $database_server){ | |
Write-Host "Creating web application: $application_name" | |
New-SPWebApplication -Name $application_name -Port $site_port -HostHeader $application_name -Url $application -ApplicationPool $application_pool -ApplicationPoolAccount (Get-SPManagedAccount $application_pool_account) -DatabaseName $database -DatabaseServer $database_server | |
} | |
function Remove-WebApplication($application_name){ | |
Write-Host "Removing web application: $application_name" | |
Remove-SPWebApplication -Identity $application_name -Confirm -DeleteIISSite -RemoveContentDatabases -ErrorAction SilentlyContinue | |
} | |
function Check-SolutionInstalled($solution){ | |
$solution = Get-SPSolution | where-object {$_.Name -eq $solution} | |
if ($solution -eq $null -or !$solution.Deployed) { Throw "Solution not deployed: $solution" } | |
} | |
function Restart-Service($name){ | |
$name | % { | |
net stop $_ /Y | |
net start $_ /Y | |
} | |
iisreset | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment