Category Archives: PowerShell Script

SharePoint Automation – Power Shell Scripts

Hi,

How often I think about PowerShell Scripts while working on SharePoint 2007 and deploying solutions using Stsadm. Though, it worked well for me, but never got control as promissed by the powershell scripts.

Recently, I got chance to start working and ext exploring my experiences with PowerShell Scripts for deployment of SharePoint 2010 solution and doing a lot with Microsoft .Net object model siting outside Visual Studio and working in colorful Power Shell Snap In 🙂

Today, I’m gonna share a piece of work that we (SharePoint developers) needs to deal with almost in every SharePoint project, and that’s the deployment of SharePoint Solutions. You may find the contents of this post on google, but I tried to keep things simple and planned.

First of all, for those who are new to Power Shall, you may be wondering about the Commands that you can use with power shell. I use a better way to keep them with me, try the following command in Power Shell to dump all available SharePoint commands in a text file.

Get-Command -module Microsoft.SharePoint.PowerShell > textfilename.txt

Once You have all the commands, its easy for you to go and explore commands based on your requirements, I would say the Get-help is really helpful here 🙂

Now let’s play with Power shell and deploy our Solution files for the SharePoint Farm.

Lets take the case where we have some solution files, and we want to write a script that will Add and deploy solution files to specified Farm or Web.
To make it clean and dynamic I use a xml Configuration file for deployment.

<ProjectName>
<Solutions>
<Solution Name=”Usman.SP.Projects.Utilities.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.Utilities.wsp” CASPolicies=”false” GACDeployment=”true”>    </Solution>
<Solution Name=”Usman.SP.Projects.Branding.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.Branding.wsp” CASPolicies=”false” GACDeployment=”true”>    </Solution>
<Solution Name=”Usman.SP.Projects.Core.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.Core.wsp” CASPolicies=”false” GACDeployment=”true”>    </Solution>
<Solution Name=”Usman.SP.Projects.Pages.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.Pages.wsp” CASPolicies=”false” GACDeployment=”true”>    </Solution>
<Solution Name=”Usman.SP.Projects.RequestHandling.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.RequestHandling.wsp” CASPolicies=”false” GACDeployment=”true”>    </Solution>
<Solution Name=”Usman.SP.Projects.WebStructure.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.WebStructure.wsp” CASPolicies=”false” GACDeployment=”true”>    </Solution>
<Solution Name=”Usman.SP.Projects.Presentation.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.Presentation.wsp” CASPolicies=”false” GACDeployment=”true”>
<WebApplications>
</WebApplications>
</Solution>
<Solution Name=”Usman.SP.Projects.Sites.wsp” Path=”C:\Users\afzalu\PS_Scripts\Installer\Usman.SP.Projects.Sites.wsp” CASPolicies=”false” GACDeployment=”true”>
<WebApplications>
</WebApplications>
<Solution>
</Solutions>

</ProjectName>

For Now, I just take an example where I need to deploy the solution, but ofcourse with this design you can also do feature activation and other webapplication chnages (we will do that in some other post).

Now let’s see how easily we can add a Power Shell Script to add these solution to specific WebApplication or Farm

# add SharePoint Powershell snapin
add-PSSnapin Microsoft.Sharepoint.powershell
write-host “———————————————————-” -foregroundcolor Green
write-host “Deploying Soultions” -foregroundcolor Green

if($args){

# Getting web.Config File

$configFile = $args[0];

if ([string]::IsNullOrEmpty($configFile)) { return }

[xml]$solutionsConfig = Get-Content $configFile

if ($solutionsConfig -eq $null) { return }

$solutionsConfig.DLW.Solutions.Solution | ForEach-Object{
[string]$path = $_.Path
[bool]$gac = [bool]::Parse($_.GACDeployment)
[bool]$cas = [bool]::Parse($_.CASPolicies)
$webApps = $_.WebApplications.WebApplication

#Add the solution

$spAdminServiceName = “SPAdminV4”
[string]$name = Split-Path -Path $path -Leaf
$solution = Get-SPSolution $name -ErrorAction SilentlyContinue
Write-Host “Adding solution $name…”
$solution = Add-SPSolution $path

#Deploy the solution

if (!$solution.ContainsWebApplicationResource) {
Write-Host “Deploying solution $name to the Farm…”
$solution | Install-SPSolution -GACDeployment:$gac -CASPolicies:$cas -Confirm:$false
}
else {
if ($webApps -eq $null -or $webApps.Length -eq 0) {
Write-Warning “The solution $name contains web application resources but no web applications were specified to deploy to.”
return
}
$webApps | ForEach-Object {
Write-Host “Deploying solution $name to $_…”
$solution | Install-SPSolution -GACDeployment:$gac -CASPolicies:$cas -WebApplication $_ -Confirm:$false
}
}
# Restarting the Service
Stop-Service -Name $spAdminServiceName
Start-SPAdminJob -Verbose
Start-Service -Name $spAdminServiceName

#Block until we’re sure the solution is deployed.

do { Start-Sleep 2 } while (!((Get-SPSolution $name).Deployed))
} #End of $solutionsConfig.Solutions.Solution | ForEach-Object
}
write-host “Solutions Deployed Successfully” -foregroundcolor Green

write-host “———————————————————–” -foregroundcolor Green

Sorry for Formating, but it takes too much time so I prefer sharing things rather than spending lots of time on formating.
Once you run the above code you will see a very informative Power Shell Window, that will show you all the deployment setps in a friendly way so your deployement team can really enjoy the deployment.
You can use the below code to uninstall and delete the solution files using the same XML file

# add SharePoint Powershell snapin

add-PSSnapin Microsoft.Sharepoint.powershell

write-host “———————————————————-” -foregroundcolor Green

write-host “Deleting Soultions” -foregroundcolor Green

if($args){

# Getting web.Config File

$configFile = $args[0];

if ([string]::IsNullOrEmpty($configFile)) { return }

[xml]$solutionsConfig = Get-Content $configFile

if ($solutionsConfig -eq $null) { return }

$solutionsConfig.DLW.Solutions.Solution | ForEach-Object{

[string]$name = $_.Name

$spAdminServiceName = “SPAdminV4”

$solution = Get-SPSolution $name

# Un-Install Solutions

if($solution.Deployed){

if (!$solution.ContainsWebApplicationResource) {

Write-Host “Un-Installing solution $name from the Farm…”

$solution | UnInstall-SPSolution -Confirm:$false

}

else{

Write-Host “Un-Installing solution $name …”

$solution | UnInstall-SPSolution -AllWebApplications:$true -Confirm:$false

}

Stop-Service -Name $spAdminServiceName

Start-SPAdminJob -Verbose

Start-Service -Name $spAdminServiceName

#Block until we’re sure the solution is deployed.

do { Start-Sleep 2 } while (((Get-SPSolution $name).Deployed))

}

} #End of $solutionsConfig.Solutions.Solution | ForEach-Object

# Remove Solutions

$solutionsConfig.DLW.Solutions.Solution | ForEach-Object{

if(!$solution.Deployed){

[string]$name = $_.Name

Write-Host “Getting uninstall solution $name…” -foregroundcolor YELLOW

$unInstallSolution = Get-SPSolution $name

$unInstallSolution | Remove-SPSolution -Confirm:$false

Write-Host “Solutoin $name… Removed” -foregroundcolor YELLOW

}

}

}

write-host “Solutions Deleted Successfully” -foregroundcolor Green

write-host “———————————————————–” -foregroundcolor Green

Advertisements