使用Powershell自动化部署SharePoint解决方案
作者:杨柳@水杉网络
适用版本
- SharePoint 2010
- SharePoint 2013
使用方式
- 下载脚本solution.deployment.ps1。
- 复制
solution.deployment.ps1脚本到需要部署的解决方案包所在的目录。
- 修改脚本中第121行中的
$solutionNames变量值,多个解决方案包使用分号(;)隔开。
- 在Powershell命令行窗口执行
solution.deployment.ps1。
脚本
$Snapin = get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if($Snapin -eq $null){
Write-Output -InputObject "Loading SharePoint Powershell Snapin"
try
{
Add-PSSnapin "Microsoft.SharePoint.Powershell"
Write-Output -InputObject "Loaded SharePoint PowerShell Snapin"
}
catch
{
$(throw "There was an error loading SharePoint PowerShell Snapin")
exit
}
}
function SSUninstallSolution($name)
{
$solution = Get-SPSolution $name -ErrorAction Continue;
if ($solution -eq $null -or !$solution.Deployed)
{
$(throw "Solution is null or the solution has not deployed, it can not be uninstalled.");
exit;
}
Write-Output -InputObject "The solution: $solutionName is deployed in this farm.";
Write-Output -InputObject "Prepare to uninstall solution: $solutionName";
$lanPack = $solution.GetLanguagePack(0);
if ($lanPack -ne $null -and $lanPack.ContainsWebApplicationResource)
{
Uninstall-SPSolution -Identity $solution -AllWebApplications -Confirm:$false;
}
else {
Uninstall-SPSolution -Identity $solution -Confirm:$false;
}
while($solution.JobExists)
{
start-sleep 1
Write-Output -InputObject "Uninstalling solution: $solutionName";
}
Write-Output -InputObject "Uninstalled solution: $solutionName";
}
function SSRemoveSolution($name)
{
$solution = Get-SPSolution $name -ErrorAction Continue;
if ($solution -eq $null -or $solution.Deployed)
{
$(throw "Solution is null or the solution has deployed, it can not be removed.");
exit;
}
Write-Output -InputObject "Prepare to remove solution: $solutionName";
Remove-SPSolution -Identity $solution -Confirm:$false;
while($solution.JobExists)
{
start-sleep 1
Write-Output -InputObject "Removing solution: $solutionName";
}
Write-Output -InputObject "Removed solution: $solutionName";
}
function SSInstallSolution($name)
{
$solution = Get-SPSolution $name -ErrorAction Continue;
if ($solution -eq $null -or $solution.Deployed)
{
$(throw "Solution is null or the solution has deployed, it can not be removed.");
exit;
}
Write-Output -InputObject "Prepare to install solution: $name";
$lanPack = $solution.GetLanguagePack(0);
$containsGlobalAssembly = $solution.ContainsGlobalAssembly;
if ($lanPack -ne $null -and $lanPack.ContainsWebApplicationResource)
{
if ($containsGlobalAssembly)
{
Install-SPSolution -Identity $solution -AllWebApplications -Force -GACDeployment -Confirm:$false;
}
else {
Install-SPSolution -Identity $solution -AllWebApplications -Force -Confirm:$false;
}
}
else
{
if ($containsGlobalAssembly)
{
Install-SPSolution -Identity $solution -Force -GACDeployment -Confirm:$false;
}
else {
Install-SPSolution -Identity $solution -Force -Confirm:$false;
}
}
$solution = Get-SPSolution $name;
while($solution.JobExists)
{
start-sleep 1
Write-Output -InputObject "Installing solution: $name";
}
Write-Output -InputObject "Installed solution: $name";
}
$solutionNames = "shuishan.s2.framework.wsp;shuishan.s2.organizationchart.wsp;shuishan.s2.form.wsp;shuishan.s2.mobile.wsp;shuishan.s2.workflow.wsp";
$p = Get-Location;
foreach($solutionName in $solutionNames.split(';'))
{
$spath = $p.Path + "/" + $solutionName;
$solution = Get-SPSolution $solutionName -ErrorAction Continue;
if ($solution -ne $null)
{
Write-Output -InputObject "The solution: $solutionName exists in this farm.";
if ($solution.Deployed)
{
SSUninstallSolution($solutionName);
}
SSRemoveSolution($solutionName);
}
else
{
Write-Output -InputObject "The solution: $solutionName is not exists in this farm.";
}
Write-Output -InputObject "Add solution: $solutionName";
Add-SPSolution -LiteralPath $spath;
SSInstallSolution($solutionName);
}