SharePoint Solutions Deployment-PowerShell
之前群里有人问到了这个,项目一期开发结束后正好整理了一下,发上来分享一下,也是从谷歌搜索里面学来的,大家要用好搜索哈
$ver = $host | select version
if ($ver.Version.Major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"}
Add-PsSnapin Microsoft.SharePoint.PowerShell function global:Deploy-SPSolutions() {
<#
.Synopsis
Deploys one or more Farm Solution Packages to the Farm.
.Description
Specify either a directory containing WSP files, a single WSP file, or an XML configuration file containing the WSP files to deploy.
If using an XML configuration file, the format of the file must match the following:
<Solutions>
<Solution Path="<full path and filename to WSP>" UpgradeExisting="false">
<WebApplications>
<WebApplication>http://example.com/</WebApplication>
</WebApplications>
</Solution>
</Solutions>
Multiple <Solution> and <WebApplication> nodes can be added. The UpgradeExisting attribute is optional and should be specified if the WSP should be udpated and not retracted and redeployed.
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs -WebApplication http://demo This example loads the function into memory and then deploys all the WSP files in the specified directory to the http://demo Web Application (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs -WebApplication http://demo,http://mysites This example loads the function into memory and then deploys all the WSP files in the specified directory to the http://demo and http://mysites Web Applications (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs -AllWebApplications This example loads the function into memory and then deploys all the WSP files in the specified directory to all Web Applications (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs\MyCustomSolution.wsp -AllWebApplications This example loads the function into memory and then deploys the specified WSP to all Web Applications (if applicable).
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions -Identity C:\WSPs\MyCustomSolution.wsp -AllWebApplications -UpgradeExisting This example loads the function into memory and then deploys the specified WSP to all Web Applications (if applicable); existing deployments will be upgraded and not retracted and redeployed.
.Example
PS C:\> . .\Deploy-SPSolutions.ps1
PS C:\> Deploy-SPSolutions C:\Solutions.xml This example loads the function into memory and then deploys all the WSP files specified by the Solutions.xml configuration file.
.Parameter Config
The XML configuration file containing the WSP files to deploy.
.Parameter Identity
The directory, WSP file, or XML configuration file containing the WSP files to deploy.
.Parameter UpgradeExisting
If specified, the WSP file(s) will be updated and not retracted and redeployed (if the WSP does not exist in the Farm then this parameter has no effect).
.Parameter AllWebApplications
If specified, the WSP file(s) will be deployed to all Web Applications in the Farm (if applicable).
.Parameter WebApplication
Specifies the Web Application(s) to deploy the WSP file to.
.Link
Get-Content
Get-SPSolution
Add-SPSolution
Install-SPSolution
Update-SPSolution
Uninstall-SPSolution
Remove-SPSolution
#>
[CmdletBinding(DefaultParameterSetName="FileOrDirectory")]
param (
[Parameter(Mandatory=$true, Position=0, ParameterSetName="Xml")]
[ValidateNotNullOrEmpty()]
[xml]$Config, [Parameter(Mandatory=$true, Position=0, ParameterSetName="FileOrDirectory")]
[ValidateNotNullOrEmpty()]
[string]$Identity, [Parameter(Mandatory=$false, Position=1, ParameterSetName="FileOrDirectory")]
[switch]$UpgradeExisting, [Parameter(Mandatory=$false, Position=2, ParameterSetName="FileOrDirectory")]
[switch]$AllWebApplications, [Parameter(Mandatory=$false, Position=3, ParameterSetName="FileOrDirectory")]
[Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind[]]$WebApplication
)
function Block-SPDeployment($solution, [bool]$deploying, [string]$status, [int]$percentComplete) {
do {
Start-Sleep 2
Write-Progress -Activity "Deploying solution $($solution.Name)" -Status $status -PercentComplete $percentComplete
$solution = Get-SPSolution $solution
if ($solution.LastOperationResult -like "*Failed*") { throw "An error occurred during the solution retraction, deployment, or update." }
if (!$solution.JobExists -and (($deploying -and $solution.Deployed) -or (!$deploying -and !$solution.Deployed))) { break }
} while ($true)
sleep 5
}
switch ($PsCmdlet.ParameterSetName) {
"Xml" {
# An XML document was provided so iterate through all the defined solutions and call the other parameter set version of the function
$Config.Solutions.Solution | ForEach-Object {
[string]$path = $_.Path
[bool]$upgrade = $false
if (![string]::IsNullOrEmpty($_.UpgradeExisting)) {
$upgrade = [bool]::Parse($_.UpgradeExisting)
}
$webApps = $_.WebApplications.WebApplication
Deploy-SPSolutions -Identity $path -UpgradeExisting:$upgrade -WebApplication $webApps -AllWebApplications:$(($webApps -eq $null) -or ($webApps.Length -eq 0))
}
break
}
"FileOrDirectory" {
$item = Get-Item (Resolve-Path $Identity)
if ($item -is [System.IO.DirectoryInfo]) {
# A directory was provided so iterate through all files in the directory and deploy if the file is a WSP (based on the extension)
Get-ChildItem $item | ForEach-Object {
if ($_.Name.ToLower().EndsWith(".wsp")) {
Deploy-SPSolutions -Identity $_.FullName -UpgradeExisting:$UpgradeExisting -WebApplication $WebApplication
}
}
} elseif ($item -is [System.IO.FileInfo]) {
# A specific file was provided so assume that the file is a WSP if it does not have an XML extension.
[string]$name = $item.Name if ($name.ToLower().EndsWith(".xml")) {
Deploy-SPSolutions -Config ([xml](Get-Content $item.FullName))
return
}
$solution = Get-SPSolution $name -ErrorAction SilentlyContinue if ($solution -ne $null -and $UpgradeExisting) {
# Just update the solution, don't retract and redeploy.
Write-Progress -Activity "Deploying solution $name" -Status "Updating $name" -PercentComplete -1
$solution | Update-SPSolution -CASPolicies:$($solution.ContainsCasPolicy) `
-GACDeployment:$($solution.ContainsGlobalAssembly) `
-LiteralPath $item.FullName Block-SPDeployment $solution $true "Updating $name" -1
Write-Progress -Activity "Deploying solution $name" -Status "Updated" -Completed return
} if ($solution -ne $null) {
#Retract the solution
if ($solution.Deployed) {
Write-Progress -Activity "Deploying solution $name" -Status "Retracting $name" -PercentComplete 0
if ($solution.ContainsWebApplicationResource) {
$solution | Uninstall-SPSolution -AllWebApplications -Confirm:$false
} else {
$solution | Uninstall-SPSolution -Confirm:$false
}
#Block until we're sure the solution is no longer deployed.
Block-SPDeployment $solution $false "Retracting $name" 12
Write-Progress -Activity "Deploying solution $name" -Status "Solution retracted" -PercentComplete 25
} #Delete the solution
Write-Progress -Activity "Deploying solution $name" -Status "Removing $name" -PercentComplete 30
Get-SPSolution $name | Remove-SPSolution -Confirm:$false
Write-Progress -Activity "Deploying solution $name" -Status "Solution removed" -PercentComplete 50
} #Add the solution
Write-Progress -Activity "Deploying solution $name" -Status "Adding $name" -PercentComplete 50
$solution = Add-SPSolution $item.FullName
Write-Progress -Activity "Deploying solution $name" -Status "Solution added" -PercentComplete 75 #Deploy the solution if (!$solution.ContainsWebApplicationResource) {
Write-Progress -Activity "Deploying solution $name" -Status "Installing $name" -PercentComplete 75
$solution | Install-SPSolution -GACDeployment:$($solution.ContainsGlobalAssembly) -CASPolicies:$($solution.ContainsCasPolicy) -Confirm:$false
Block-SPDeployment $solution $true "Installing $name" 85
} else {
if ($WebApplication -eq $null -or $WebApplication.Length -eq 0) {
Write-Progress -Activity "Deploying solution $name" -Status "Installing $name to all Web Applications" -PercentComplete 75
$solution | Install-SPSolution -GACDeployment:$($solution.ContainsGlobalAssembly) -CASPolicies:$($solution.ContainsCasPolicy) -AllWebApplications -Confirm:$false
Block-SPDeployment $solution $true "Installing $name to all Web Applications" 85
} else {
$WebApplication | ForEach-Object {
$webApp = $_.Read()
Write-Progress -Activity "Deploying solution $name" -Status "Installing $name to $($webApp.Url)" -PercentComplete 75
$solution | Install-SPSolution -GACDeployment:$gac -CASPolicies:$cas -WebApplication $webApp -Confirm:$false
Block-SPDeployment $solution $true "Installing $name to $($webApp.Url)" 85
}
}
}
Write-Progress -Activity "Deploying solution $name" -Status "Deployed" -Completed
}
break
}
}
}
Deploy-SPSolutions .\config.xml -WebApplication http://localhost
SharePoint Solutions Deployment-PowerShell的更多相关文章
- SharePoint 2013 使用 PowerShell 更新用户
在SharePoint开发中,经常会遇到网站部署,然而,当我们从开发环境,部署到正式环境以后,尤其是备份还原,所有用户组的用户,还依然是开发环境的,这时,我们就需要用PowerShell更新一下: P ...
- SharePoint 2013 使用PowerShell创建State Service
今天,搞SPD配置的sp2010wf迁移到sp2013环境上去,发布解决方案都很正常,给列表添加wf的时候报错“该表单无法显示,可能是由于 Microsoft SharePoint Server St ...
- SharePoint 内容部署-PowerShell
1. 创建一个新的内容部署路径 New-SPContentDeploymentPath –Name "Marketing Internet Content" –SourceSPWe ...
- sharepoint 2013 使用powershell更改站点集配额和锁定
打开sharepoint powershell 2013,使用管理员方式打开 逐行输入下面命令: $Admin = new-object Microsoft.SharePoint.Administr ...
- SharePoint自动化系列——Upload files to SharePoint library using PowerShell.
转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 日常的SharePoint站点测试中,我们经常要做各种各样的数据,今天又写了几个脚本,发现自己写的 ...
- Load sharepoint envirement by powershell
#判断当前上下文环境中是否装在了SharePoint的Powershell环境,如果没有装载,则装载到当前运行环境.$Snapin = get-PSSnapin | Where-Object {$_. ...
- [转载]SharePoint 网站管理-PowerShell
1. 显示场中所有可用的网站集 Get-SPSite Get-SPSite 2. 显示某一Web应用程序下可用的网站集 Get-SPSite –WebApplication "SharePo ...
- SharePoint场管理-PowerShell(二)
1. 合并Log文件 Merge-SPLogFile –Path E:\Logs\MergedLog.log –StartTime "1/19/2010" –Overwrite 2 ...
- SharePoint 企业搜索-PowerShell
1. 显示企业搜索服务信息 Get-SPEnterpriseSear1chService 2. 显示企业搜索服务实例 Get-SPEnterpriseSearchServiceInstance 3. ...
随机推荐
- Sqlserver2008安装部署文档
Sqlserver2008部署文档 注意事项: 如果你要安装的是64位的服务器,并且是新机器.那么请注意,你需要首先需要给64系统安装一个.net framework,如果已经安装此功能,请略过这一步 ...
- [译]Java 垃圾回收的监控和分析
说明:这篇文章来翻译来自于Javapapers 的Java Garbage Collection Monitoring and Analysi 在这个系列的Java垃圾回收教程中,我们将看到可用于垃圾 ...
- 牛腩公布系统--HTTP 错误 403.14 - Forbidden
忘了是谁说的"至理名言",做牛腩公布系统,不怕出错误,就怕出跟牛老师不一样的错误!! 刚做就開始出现各种错误了,只是话说错误越多,收获越多.把每次困难都当做历练成长的机会.不多说, ...
- C#中float的取值范围和精度
原文:C#中float的取值范围和精度 float类型的表现形式: 默认情况下,赋值运算符右侧的实数被视为 double. 因此,应使用后缀 f 或 F 初始化浮点型变量,如以下示例中所示: floa ...
- 无线连接手机进行Android测试
当每天走到哪都要拿一根数据线进行项目测试的时候,总是有一些焦急和烦躁的,如果能够无线连接测试就在好不过了. 这样不再是什么难事了,只需要几步走: 在进行无线连接测试的过程中,你的手机必须root了,这 ...
- Oracle表介绍--簇表
簇和簇表 簇其实就是一组表,是一组共享相同数据块的多个表组成. 将经常一起使用的表组合在一起成簇可以提高处理效率. 在一个簇中的表就叫做簇表.建立顺序是:簇→簇表→数据→簇索引 ...
- Xamarin.Android
Xamarin.Android之使用百度地图起始篇 一.前言 如今跨平台开发层出不穷,而对于.NET而言时下最流行的当然还是Xamarin,不仅仅能够让我们在熟悉的Vs下利用C#开发,在对原生态类库的 ...
- javascript转换.net DateTime方法 (比如转换\/Date(1426056463000)\/)
function getDate(str_time) { var re = new RegExp('\\/Date\\(([-+])?(\\d+)(?:[+-]\\d{4})?\\)\\/'); va ...
- AngularJS中数据双向绑定(two-way data-binding)
1.切换工作目录 git checkout step-4 #切换分支,切换到第4步 npm start #启动项目 2.代码 app/index.html Search: <input ng-m ...
- C++ Builder中TOpenDialog控件的使用例子
源代码如下(opendlg_loaddata为TOpenDialog控件的name,ofAllowMultiSelect代表允许多选): opendlg_loaddata->Options &l ...