Sharepoint学习笔记—使用PowerShell添加和部署Solution

      我们通常使用Visual Studio 2010来快速开发和部署我们的Sharepoint Solution.但有时我们不得不遇到要把我们开发的Solution Packages部署到其它物理位置(生产机),而不是我们开发环境所指向的服务器(测试机)。以前我们使用Stsadm工具来完成此工作,但现在我们更推荐使用PowerShell来操作。
     下面我们分别描述此工作涉及到的基本步骤:
一、添加Solution 到Sharepoint Farm中
通过我们要先将Visual Studio中的的Solution打包,打成的包可以到Solution的Bin\Debug目录下去找。把此目录下的Solution Package拷贝到需要部署的服务器上的指定目录下eg:  D:\Sp2010\DeploySolution\MySharepointProject.wsp
   如果用以前的Stsadm完成添加Solution工作则是 

 stsadm –o addsolution –name D:\Sp2010\DeploySolution\MySharepointProject.wsp 

   如果要使用PowerShell,则只需要从你的目标服务器桌面的的Start菜单中找到

 

    系统会自动加载Microsoft.SharePoint.PowerShell,我们便可以直接在其Command窗口中执行我们将要执行的PowerShell命令.如果对某个PowerShell命令(如Add-SPSolution)有使用上的问题,可在其Command窗口中使用Get-Help  Add-SPSolution 来取得此命令的相关帮助。
   此处,我们用PowerShell命令来完成添加Solution的工作:

Add-SPSolution  D:\Sp2010\DeploySolution\MySharepointProject.wsp

   如果你是开发的Sandboxed solution,那么你需要使用Add-SPUserSolution命令来执行上面的操作。此命令需要参数–literalpath,此参数提指向Solution的全路径,
二、部署Solution到指定的Web Application上
  接下来我们要部署Solution到我们指定的Web Application(eg: http://myserver-sp1:2010/)上
  如果用以前的Stsadm完成部署Solution工作则是

stsadm –o deploysolution –name MySharepointProject.wsp –url http://myserver-sp1:2010/    –allowCasPolicies –immediate

  如果是使用PowerShell命令,则如下

Install-SPSolution –Identity MySharepointProject.wsp –WebApplication http://myserver-sp1:2010/  -GACDeployment

  如果部署的是 Sandboxed solution,则使用Install-SPUserSolution命令。
    –GACDeployment 参数也可换成–CASPolicies,二者区别是
          GACDeployment指定可以为新 SharePoint 解决方案部署全局程序集缓存 (GAC)。。
           CASPolicies指定可以为新 SharePoint 解决方案部署代码访问安全 (CAS) 策略。
     - WebApplication参数也可换成- AllWebApplications,二者区别是:
         WebApplication: 为指定 SharePoint Web 应用程序部署 SharePoint 解决方案。该类型必须是格式为 12345678-90ab-cdef-1234-567890bcdefgh 的有效 GUID;

                                SharePoint Web 应用程序的有效名称(例如,MyOfficeApp1);或有效 SPWebApplication 对象的实例。

         AllWebApplications: 指定为服务器场中的所有 SharePoint Web 应用程序部署新的 SharePoint 解决方案。

  如果你需要强制部署此Solution,你可以使用-Force参数。
  通过上面两个步骤,你就完成了把指定的Sharepoint Solution Package添加和部署到指定的Sharepoint Farm和Web Application中。为使此文更完成,我们继续讨论我们可能要做的其它相关工作。
三、升级部署Solution.
我们可能要通过升级方式来部署我们已经在前面部署好的Solution,使用此方式前,我们首先需要把新版本的Solution拷贝到我们指定的目录下eg: D:\Sp2010\DeploySolution\MySharepointProject.wsp
   如果是使用Stsadm命令:

stsadm –o upgradesolution –name MySharepointProject.wsp –filename D:\Sp2010\DeploySolution\MySharepointProject.wsp –immediate –allowCasPolicies

  如果是使用PowerShell命令:

Update-SPSolution –Identity MySharepointProject.wsp –LiteralPath D:\Sp2010\DeploySolution\MySharepointProject.wsp  –GACDeployment

四、回收已经部署的Solution
  前面,我们已经完成了Instll, Deploy, Upgrade操作,接下来我们要实现Uninstall操作,此操作实现把Solution从指定的(或全部)WebApplication中回收回来。Solution仍然存在于Farm中,只是不再分配给WebApplication使用。
   使用PowerShell命令:

Uninstall-SPSolution –Identity MySharepointProject.wsp –WebApplication http://myserver-sp1:2010/

我们也可使用–AllWebApplications参数来一次性从此Sharepoint Farm中的所有部署此Solution的Web Application中回收Solution.
执行此命令时,系统会提示: . “Are you sure?” ,你只需要确定即可执行操作。

五、移除Solution
  这是最后一步,执行此步我们将从Sharepoint Farm中移除我们安装好的Solution.
  使用PowerShell命令:

Remove-SPSolution –Identity MySharepointProject.wsp

  上面的第四、五步如果用Stsadm来完成,则通常我们可以建立一个批处理文件eg: DeleteSolution.bat
代码内容如下(里面的内容要根据自身情况进行修改):

:begin
@echo off
rem 
** declare the solution to be retracted **
set solutionName=SampleSolution
rem 
** declare the set of fetures to be de-activated **
set featureSampleFeature1=SampleFeature1
set featureSampleFeature2=SampleFeature2
set featureSampleFeature3=SampleFeature3
rem 
** Replace this value with the URL of your site **
@set url
=http://servername/sites/sitecollectioname/sitename
@set PATH=C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN;%PATH% 
echo deactivating features 
in solution %solutionName%...
echo 
----------------------------------------------------
stsadm 
-o deactivatefeature -name %featureSampleFeature1% -url %url% -force
stsadm 
-o deactivatefeature -name %featureSampleFeature2% -url %url% -force
stsadm 
-o deactivatefeature -name %featureSampleFeature3% -url %url% -force
echo Attempting to uninstallfeature and retract solution
echo 
---------------------------------------------------
echo Rectracting solution 
%solutionName% from solution store...
stsadm 
-o retractsolution   -name %solutionName%.wsp -immediate
stsadm 
-o execadmsvcjobs 
echo Deleting solution 
%solutionName% from solution store...
stsadm 
-o deletesolution -name %solutionName%.wsp -override 
echo.
if errorlevel == 0 goto :success
:success
echo Successfully deployed solution and activated feature(s)..
echo .
goto end
:end
pause


 相关链接:

Sharepoint学习笔记—使用 Stsadm Installing或 Uninstalling features

 

posted @ 2011-05-10 10:00  wsdj  阅读(6572)  评论(1编辑  收藏  举报