Automatic deploy wsp to SharePoint using PowerShell

1. Create a config file named “config.xml” and put it in folder “C://WspDeployment”

1:  <?xml version="1.0" encoding="utf-8"?>  
2:  <Config>  
3:   <SolutionsFolder>C:\WspDeployment\Builds\Latest</SolutionsFolder>  
4:   <WebApplication Create="FALSE">  
5:    <WebApplicationURL>http://www.yoursite.domain.base</WebApplicationURL>  
6:    <WebApplicationName>YourApplicationName</WebApplicationName>  
7:    <Port></Port>  
8:    <ApplicationPool></ApplicationPool>  
9:    <ApplicationPoolAccount></ApplicationPoolAccount>  
10:    <HostHeader></HostHeader>  
11:   </WebApplication>  
12:   <Database>  
13:    <DatabaseName></DatabaseName>  
14:    <DatabaseServer></DatabaseServer>  
15:   </Database>  
16:   <Solutions>  
17:    <Solution Scoped="FALSE">Package1.wsp</Solution>  
18:    <Solution Scoped="FALSE">Package2.wsp</Solution>  
19:    <Solution Scoped="TRUE">Package3.wsp</Solution>  
20:   </Solutions>  
21:  </Config>  

2. In the same folder of Config.xml, create new file named “WspDeployment.ps1”

1:  ## Get XML configuration file parameters  
2:  param   
3:  (  
4:    [string]$ConfigFile = $(throw '- Need parameter input file (e.g. "c:\install-config.xml")')  
5:  )  
6:  ## check to ensure Microsoft.SharePoint.PowerShell is loaded if not using the SharePoint Management Shell   
7:  $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}   
8:  if ($snapin -eq $null)   
9:  {    
10:       Write-Host "Loading SharePoint Powershell Snapin"    
11:       Add-PSSnapin "Microsoft.SharePoint.Powershell"   
12:  }  
13:  ## get the the node Config in the configuration file  
14:  $xmlinput = [xml] (get-content $ConfigFile)  
15:  $item = $xmlinput.Config  
16:  ## Read the Farm parameters  
17:  $SolutionsFolder = $item.SolutionsFolder  
18:  $WebApplicationURL = $item.WebApplication.WebApplicationURL  
19:  $WebApplicationName = $item.WebApplication.WebApplicationName  
20:  $Port = $item.WebApplication.Port  
21:  $ApplicationPool = $item.WebApplication.ApplicationPool  
22:  $ApplicationPoolAccount = $item.WebApplication.ApplicationPoolAccount  
23:  $HostHeader = $item.WebApplication.HostHeader  
24:  $DatabaseName = $item.Database.DatabaseName  
25:  $DatabaseServer = $item.Database.DatabaseServer  
26:  $Solutions = $item.Solutions  
27:  $Features = $item.Features  
28:  ## write the Farm parameters to the console application  
29:  Write-Host -f White "SolutionsFolder: $SolutionsFolder"  
30:  Write-Host -f White "WebApplicationURL: $WebApplicationURL"  
31:  Write-Host -f White "WebApplicationName: $WebApplicationName"  
32:  Write-Host -f White "ApplicationPool: $ApplicationPool"  
33:  Write-Host -f White "ApplicationPoolAccount: $ApplicationPoolAccount"  
34:  Write-Host -f White "HostHeader: $HostHeader"  
35:  Write-Host -f White "DatabaseName: $DatabaseName"  
36:  Write-Host -f White "DatabaseServer: $DatabaseServer"  
37:  ## this method will delete and retract the solution. You will need to call it  
38:  ## with the solution id and the webapplication. If it is globally deployed just  
39:  ## leave the web application empty  
40:  function DeleteSolution  
41:  {  
42:       param ([string]$solutionID, [string]$webApplication)  
43:       $farm = Get-SPFarm  
44:       $sol = $farm.Solutions[$solutionID]  
45:       if($sol)  
46:       {  
47:            Write-Host -f Yellow "Going to uninstall $solutionID"  
48:            if( $sol.Deployed -eq $TRUE )   
49:            {  
50:                 if ( $webApplication -eq "" )  
51:                 {  
52:                      Uninstall-SPSolution -Identity $solutionID -Confirm:0  
53:                 }  
54:                 else   
55:                 {  
56:                      Uninstall-SPSolution -Identity $solutionID -Confirm:0 -Webapplication $webApplication  
57:                 }  
58:                 while( $sol.JobExists )   
59:                 {  
60:                      Write-Host "waiting for retraction."  
61:                      sleep 3  
62:                 }  
63:            }  
64:            Write-Host -f Yellow "Going to Remove $solutionID"  
65:            Remove-SPSolution -Identity $solutionID -Force -Confirm:0  
66:            Write-Host -f Green $solutionID is deleted from this Farm  
67:       }  
68:       else  
69:       {  
70:            Write-Host -f Yellow "Solution $solutionID not found"  
71:       }  
72:  }  
73:  ## This method installs and deploys a solution based on its solution id, path and web application.  
74:  ## If the solution should be deployed globally just leave the web application empty  
75:  function DeploySolution  
76:  {  
77:       param ([string]$solutionID, [string]$solutionPath, [string]$webApplication)  
78:       $filename = $solutionPath + "\" + $solutionID  
79:       Write-Host -f White "Deploy solution $solutionID from $filename to webapplication $webApplication"  
80:       Add-SPSolution $filename  
81:       Write-Host -f Green "Solution $solutionID added"  
82:       Write-Host -f Yellow "Installing $solutionID into web application"  
83:       if ( $webApplication -eq "" )  
84:       {  
85:            Install-SPSolution –Identity $solutionID -GACDeployment -Force  
86:       }  
87:       else  
88:       {  
89:            Install-SPSolution –Identity $solutionID –WebApplication $webApplication -GACDeployment -Force  
90:       }  
91:       $farm = Get-SPFarm  
92:       $sol = $farm.Solutions[$solutionID]  
93:       if($sol -ne $null)  
94:       {  
95:            Write-Host -f Yellow "Going to install $solutionID"  
96:            while( $sol.Deployed -eq $FALSE )  
97:            {  
98:                 Write-Host "waiting for installation."  
99:                 sleep 3  
100:            }  
101:            Write-Host -f Green "Solution $solutionID is installed"  
102:       }  
103:       else  
104:       {  
105:            Write-Host -f Red "Installing $solutionID has failed. Solution is not found."  
106:       }  
107:  }  
108:  function ActivateFeature  
109:  {  
110:    param ([string]$featureID, [string]$url)  
111:    Write-Host -f Yellow "Activating feature $featureID on url $url"  
112:    $feature = Get-SPFeature -Identity $featureID  
113:    if ($feature -ne $Null)  
114:    {  
115:      try  
116:      {  
117:        if ($feature.Scope -eq "Site")  
118:        {  
119:          $feature = Get-SPFeature -Identity $featureID -Site $url  
120:        }  
121:        elseif ($feature.Scope -eq "Web")  
122:        {  
123:          $feature = Get-SPFeature -Identity $featureID -Web $url  
124:        }  
125:        elseif ($feature.Scope -eq "WebApplication")  
126:        {  
127:          $feature = Get-SPFeature -Identity $featureID -WebApplication $url  
128:        }  
129:        elseif ($feature.Scope -eq "Farm")  
130:        {  
131:          $feature = Get-SPFeature -Identity $featureID -Farm  
132:        }  
133:        else  
134:        {  
135:          $feature = $Null  
136:        }  
137:      }  
138:      catch  
139:      {  
140:        $feature = $Null  
141:      }  
142:      if ($feature -eq $Null)  
143:      {  
144:        if ($url -eq $Null)  
145:        {  
146:          Enable-SPFeature -Identity $featureID  
147:        }  
148:        else  
149:        {  
150:          Enable-SPFeature -Identity $featureID -Url $url  
151:        }  
152:        Write-Host -f Green "Feature $featureID is activated on url $url"  
153:      }  
154:      else  
155:      {  
156:        Write-Host -f Green "Feature $featureID is already activated on url $url"  
157:      }  
158:    }  
159:    else  
160:    {  
161:      Write-Host -f Red "Activating feature $featureID has failed. Feature is not found."  
162:    }   
163:  }  
164:  function DeactivateFeature  
165:  {  
166:    param ([string]$featureID, [string]$url)  
167:    Write-Host -f Yellow "Deactivating feature $featureID on url $url"  
168:    $feature = Get-SPFeature -Identity $featureID  
169:    if ($feature -ne $Null)  
170:    {  
171:      try  
172:      {  
173:        if ($feature.Scope -eq "Site")  
174:        {  
175:          $feature = Get-SPFeature -Identity $featureID -Site $url  
176:        }  
177:        elseif ($feature.Scope -eq "Web")  
178:        {  
179:          $feature = Get-SPFeature -Identity $featureID -Web $url  
180:        }  
181:        elseif ($feature.Scope -eq "WebApplication")  
182:        {  
183:          $feature = Get-SPFeature -Identity $featureID -WebApplication $url  
184:        }  
185:        elseif ($feature.Scope -eq "Farm")  
186:        {  
187:          $feature = Get-SPFeature -Identity $featureID -Farm  
188:        }  
189:        else  
190:        {  
191:          $feature = $Null  
192:        }  
193:      }  
194:      catch  
195:      {  
196:        $feature = $Null  
197:      }  
198:      if ($feature -ne $Null)  
199:      {    
200:        if ($url -eq $Null)  
201:        {  
202:          Disable-SPFeature -Identity $featureID -Force -Confirm:$false  
203:        }  
204:        else  
205:        {  
206:          Disable-SPFeature -Identity $featureID -Url $url -Force -Confirm:$false  
207:        }  
208:        Write-Host -f Green "Feature $featureID is deactivated on url $url"  
209:      }  
210:      else  
211:      {  
212:        Write-Host -f Green "Feature $featureID was already deactivated on url $url"  
213:      }  
214:    }  
215:    else  
216:    {  
217:      Write-Host -f Red "Deactivating feature $featureID has failed. Feature is not found."  
218:    }  
219:  }  
220:  ## get the web application and if not exist add it based on the data from the config.xml file  
221:  $webApp = Get-SPWebApplication $WebApplicationURL -ea SilentlyContinue   
222:  $doCreate = $item.WebApplication.GetAttribute("Create")  
223:  if ($webApp -eq $Null -and $doCreate -eq $TRUE)  
224:  {  
225:       Write-Host -f Yellow "No web application found. Web application is created"  
226:       New-SPWebApplication -Name $WebApplicationName -ApplicationPool $ApplicationPool -ApplicationPoolAccount $ApplicationPoolAccount -Port $Port -URL $WebApplicationURL -HostHeader $HostHeader -DatabaseName $DatabaseName -DatabaseServer $DatabaseServer  
227:       $webApp = Get-SPWebApplication $WebApplicationURL -ea SilentlyContinue  
228:  }  
229:  ## start retract, delete, install and deploy all solutions found in the config xml file. When the  
230:  ## Scoped atrribute is set to true, the solution is retracted and deployed globally  
231:  if ($webApp -ne $Null)  
232:  {  
233:    Write-Host -f Green "Web application is created/present"  
234:       foreach($solution in $Solutions.ChildNodes)  
235:       {  
236:            $scoped = $solution.GetAttribute("Scoped")  
237:            $solutionName = $solution.InnerText  
238:            if ($scoped -eq $TRUE)  
239:            {  
240:                 Write-Host -f White "Solution $solutionName is scoped"  
241:                 DeleteSolution -solutionID $solutionName -webApplication $WebApplicationURL  
242:            }  
243:            else  
244:            {  
245:                 Write-Host -f White "Solution $solutionName is not scoped"  
246:                 DeleteSolution -solutionID $solutionName -webApplication ""  
247:            }  
248:       }  
249:       foreach($solution in $Solutions.ChildNodes)  
250:       {  
251:            $scoped = $solution.GetAttribute("Scoped")  
252:            $solutionName = $solution.InnerText  
253:            if ($scoped -eq $TRUE)  
254:            {  
255:                 Write-Host -f White "Solution $solutionName is scoped"  
256:                 DeploySolution -solutionID $solutionName -solutionPath $SolutionsFolder -webApplication $WebApplicationURL  
257:            }  
258:            else  
259:            {  
260:                 Write-Host -f White "Solution $solutionName is not scoped"  
261:                 DeploySolution -solutionID $solutionName -solutionPath $SolutionsFolder -webApplication ""  
262:            }  
263:       }  
264:       if ($Features -ne $Null)  
265:       {  
266:            foreach($feature in $Features.ChildNodes)  
267:            {  
268:                 $featureUrl = $feature.GetAttribute("Url")  
269:                 DeactivateFeature -featureID $feature.InnerText -url $featureUrl  
270:            }  
271:            foreach($feature in $Features.ChildNodes)  
272:            {  
273:                 $featureUrl = $feature.GetAttribute("Url")  
274:                 ActivateFeature -featureID $feature.InnerText -url $featureUrl  
275:            }  
276:       }  
277:  }  

3. Put all wsps in to the folder “C://WspDeployment/Builds/Latest/”
4. Open the SharePoint Management shell and type

1:  cd "C:\WspDeployment\"  
2:  .\WspDeployment.ps1 -configfile config.xml  

Done!!!

Advertisements