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

$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. Hibernat之关系的处理一对多/多对一

    第一步:编写两个pojo,比如一个学生表一个班级表  这里使用注解. 需要 班级表: package com.qcf.pox; import java.util.HashSet; import jav ...

  2. 文字超出DIV后,隐藏文字并显示...

    <html> <head> <style type="text/css"> #cs{width:100px;height:50px;line-h ...

  3. 且看三星刚发布的Smart TV如何窃听你的枕边细语

    三星最新的SmartTV有一个很酷的新的声控功能,网络连接设备可以通过它来录下你说过的所有内容并把它上传到一个第三方的地方进行存储. 该公司的语音识别软件允许用户跟他们的电视通过声音来进行沟通.一旦电 ...

  4. 部分PC端安卓管理器使用强行断开重连的方法来连接手机,容易丢书数据,损坏数据

    最近发现部分PC端的安卓管理器,貌似是百度影音以及PPTV的安卓客户端,使用强行断开手机连接,然后重新连接手机的方法,来实现客户端程序连接手机. 此时,如果刚好正在复制文件,则复制的文件会损坏,并且基 ...

  5. Spring源深和六系列 CreateBean过程

    blog宗旨:用图说话. 这一章的图讲述了createBean的过程.到这里spring容器就能够完毕IOC的整个过程,拿到我们须要的对象. 下一章我们接着来看一看AOP完毕的过程. 附:文件夹 Sp ...

  6. SpecFlow - Cucumber for .NET

    SpecFlow使用入门 SpecFlow是一个BDD工具,在这里对BDD不多赘述,你可以阅读一下微软2010年十二月的一篇文章,此外如果你想要更多了解SpecFlow,可以参考我的另一篇翻译(当然, ...

  7. 图文详解ReSharper 8.1功能变化

    ReSharper 8.1版本已经发布有段时间了,被广大开发者购买和试用,今天小编就ReSharper 8.1功能变化就行详细的解说. 支持TypeScript 突出了重构(重命名,引入变量).导航. ...

  8. 创业路(VC Pipeline),创业需要融资的阅读

    企业家们经常问我,您的投资渠道(投资流程)到底是怎么样的? 看看有多少项目,有多少人遇到,频度,终于选择哪些公司进行了投资. 这让我认为有必要提高VC投资通道的可见度.同一时候也有助于介绍到底哪些方面 ...

  9. LinQ—扩展方法

    概述 本节主要解说扩展方法,涉及LinQ的详细知识不多. 扩展方法的描写叙述 .net framework为编程人员提供了非常多的类,非常多的方法,可是,不论.net framework在类中为我们提 ...

  10. C++ Builder中splitter控件的使用方法简介

    C++ Builder提供了一个Splitter控件来实现对用户窗口的分割,只需拖动该控件到窗体上,就可以实现窗口的任意分割.把面板控件(Panel)拖动到窗体上,设置其对齐方式,然后把Splitte ...