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. ...
随机推荐
- Notepad++ 配置 支持jquery、html、css、javascript、php代码提示
原文:Notepad++ 配置 支持jquery.html.css.javascript.php代码提示 官网下载:http://notepad-plus-plus.org/ 获取插件的方法:打开软件 ...
- Windows服务之启动、停止、暂停、继续
原文:Windows服务之启动.停止.暂停.继续 Windows服务之启动.停止.暂停.继续 2011-11-09 15:07:37 我来说两句 收藏 我要投稿 [字体:小 大] ...
- 是男人就下100层【第四层】——Crazy贪吃蛇(1)
贪吃蛇是一款非常经典的游戏,这些经典游戏给我们的童年添加了不少乐趣,今天開始我们来一步一步的在Android设备上实现一款贪吃蛇游戏,我也是第一次写这个游戏,有可能会写错,或者走弯路,可是终于希望能有 ...
- UUShutdown关机工具 - 给 Windows8.1Metro 开始屏幕添加 关机重启按钮
UUShutdown,给开始屏幕(开始菜单)添加重启关机等按钮 如图: 安装完成之后找到开始菜单程序文件夹中的快捷方式,附加到开始屏幕即可. 2.0加入主程序界面,支持换肤和定时: 看见的需要那就拿去 ...
- Spring Resources之介绍和资源接口
1.介绍 不幸的是Java的标准的java.net.URL类和针对不同的URL前缀的标准处理器都不够充分去访问所有的低级资源.例如,美誉标准化的URL实现可能用于去范围需要从classpath中或者相 ...
- 编写Javascript类库(jQuery版
编写Javascript类库(jQuery版) - 进阶者系列 - 学习者系列文章 Posted on 2014-11-13 09:29 lzhdim 阅读(653) 评论(1) 编辑 收藏 本系列文 ...
- Smarty include
注:由于水平有限,欢迎指正.转载请务必注明出处. 1 include Attribute Name Type Required Default 描述 file string Yes n/a T ...
- ASP.NET MVC IOC之Unity攻略
ASP.NET MVC IOC之Unity攻略 一.你知道IOC与DI吗? 1.IOC(Inversion of Control )——控制反转 即依赖对象不在被依赖模块的类中直接通过new来获取 先 ...
- Idea安装GO语言插件
https://github.com/go-lang-plugin-org/go-lang-idea-plugin 安装方法写的很清楚,网上也很多我就不细写了,只是有一个问题,下载插件下不下来,懒得找 ...
- 关于如何惟一地标识一台Android设备的综合性讨论
想必大家在开发Android项目的时候,多多少少会遇到“如何惟一地标识一台Android设备”等类似的问题.不只是以前,即使是现在乃至可以预见的将来,这个问题都将一直存在. 如果大家使用搜索工具搜索的 ...