之前群里有人问到了这个,项目一期开发结束后正好整理了一下,发上来分享一下,也是从谷歌搜索里面学来的,大家要用好搜索哈

$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的更多相关文章

  1. SharePoint 2013 使用 PowerShell 更新用户

    在SharePoint开发中,经常会遇到网站部署,然而,当我们从开发环境,部署到正式环境以后,尤其是备份还原,所有用户组的用户,还依然是开发环境的,这时,我们就需要用PowerShell更新一下: P ...

  2. SharePoint 2013 使用PowerShell创建State Service

    今天,搞SPD配置的sp2010wf迁移到sp2013环境上去,发布解决方案都很正常,给列表添加wf的时候报错“该表单无法显示,可能是由于 Microsoft SharePoint Server St ...

  3. SharePoint 内容部署-PowerShell

    1. 创建一个新的内容部署路径 New-SPContentDeploymentPath –Name "Marketing Internet Content" –SourceSPWe ...

  4. sharepoint 2013 使用powershell更改站点集配额和锁定

    打开sharepoint powershell 2013,使用管理员方式打开 逐行输入下面命令: $Admin =  new-object Microsoft.SharePoint.Administr ...

  5. SharePoint自动化系列——Upload files to SharePoint library using PowerShell.

    转载请注明出自天外归云的博客园:http://www.cnblogs.com/LanTianYou/ 日常的SharePoint站点测试中,我们经常要做各种各样的数据,今天又写了几个脚本,发现自己写的 ...

  6. Load sharepoint envirement by powershell

    #判断当前上下文环境中是否装在了SharePoint的Powershell环境,如果没有装载,则装载到当前运行环境.$Snapin = get-PSSnapin | Where-Object {$_. ...

  7. [转载]SharePoint 网站管理-PowerShell

    1. 显示场中所有可用的网站集 Get-SPSite Get-SPSite 2. 显示某一Web应用程序下可用的网站集 Get-SPSite –WebApplication "SharePo ...

  8. SharePoint场管理-PowerShell(二)

    1. 合并Log文件 Merge-SPLogFile –Path E:\Logs\MergedLog.log –StartTime "1/19/2010" –Overwrite 2 ...

  9. SharePoint 企业搜索-PowerShell

    1. 显示企业搜索服务信息 Get-SPEnterpriseSear1chService 2. 显示企业搜索服务实例 Get-SPEnterpriseSearchServiceInstance 3. ...

随机推荐

  1. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  2. linux监控命令全覆盖(图文说明)

    1.1 top 1.1.1 命令说明 Top 命令能够实时监控系统的运行状态,并且可以按照cpu.内存和执行时间进行排序 1.1.2 参数说明 命令行启动参数: 用法: top -hv | -bcis ...

  3. Windows版Cacti全新安装与升级

    全新安装 Cacti运行依赖的软件很多,mysql.PHP.Apache.Spine.rrd等,一个一个的安装非常繁琐,而且还容易出错,全新安装的话可以采用论坛提供的windows安装包安装,非常简单 ...

  4. Java开发工具IntelliJ IDEA使用教程:创建新的Andriod项目

    IntelliJ IDEA社区版作为一个轻量级的Java开发IDE,本身是一个开箱即用的Android开发工具. 注意:在本次的教程中我们将以Android平台2.2为例进行IntelliJ IDEA ...

  5. 安装Windows2008操作系统 - 初学者系列 - 学习者系列文章

    Windows2008这款服务器操作系统不知道有多少服务器在使用,毕竟前面有经典的2003系统,后续有2012操作系统.具体就不讨论这些了.下面就对Windows2008服务器操作系统的安装进行介绍. ...

  6. sql 事务日志传输

    原文:sql 事务日志传输 概述 可以使用日志传送将事务日志不间断地从一个数据库(主数据库)发送到另一个数据库(辅助数据库).不间断地备份主数据库中的事务日志,然后将它们复制并还原到辅助数据库,这将使 ...

  7. 使用Oracle Wrap工具加密你的代码

    Oracle提供Wrap工具,可以用于加密你的Package等.不过需要注意的是,加密后的代码无法解密,你需要保管好你的源代码. 以下是个例子: 1.源代码 create or replace fun ...

  8. 什么是PHP

    PHP(PHP: Hypertext Preprocessor的缩写,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛, ...

  9. ASP.NET MVC 5项目

    图文详解远程部署ASP.NET MVC 5项目   话外篇: 由于感觉自己的机器比较慢,配置不好,所以最近想把之前的项目部署到实验室的服务器上,但是由于常不在实验室,所以在想能不能远程部署.因此今天专 ...

  10. 封装两个简单的Jquery组件

    Jquery给我们提供了很大的方便,我们把他看成是一个公共库,以致在这个公共库上延伸出了很多Jquery插件:在项目过程中,有些插件总是不那么令人满意: 主要说两个项目用途: 1.  遮罩层,跟一般的 ...