September 11

SharePoint 2010 PowerShell scripts to install Web Parts and Features

For development you can use Visual Studio 2010  features (right click deploy) to install web parts and features, but once you get into testing/staging and production enviornemnts you can use the following script

Web Parts

POWERSHELL-SharePoint-WebPart.ps1

Add-PsSnapin Microsoft.SharePoint.PowerShell

Write-Host "Initializing parameters"
$featureAction=$args[3]
$CurrentDir=$args[0]
$solutionName=$args[1] + ".wsp"
$webpartName=$args[1]
$SolutionPath=$CurrentDir + "\" + $solutionName
$logfile=$CurrentDir + "\" + $webpartName + "_" + $featureAction + ".log"
$urlPath=$args[2]
$installToURL=$args[4]

if (( $urlPath -eq $null) -or ( $installToURL -eq $null ))
{
    $installToURL="No"
}

Write-Host "Logfile: " $logfile " Starting...."
Start-Transcript $logfile

if ( $featureAction -eq "Remove")
{
    Write-Host "Rretracting and uninstalling solution:" $solutionName
    Uninstall-SPSolution -Identity $solutionName  -allwebapplications -Confirm:$false

    Write-Host "Waiting till retaction has finished " -nonewline
    do
    {
        Start-Sleep 2
        Write-Host "." -nonewline
    }
    while ((Get-SPSolution $solutionName).Deployed)

    Start-Sleep 5
    Write-Host "." -nonewline

    Write-Host "Removing solution:" $solutionName " "
    Remove-SPSolution -Identity $solutionName -Confirm:$false

    Write-Host $solutionName " Now Removed!"

}

if ( $featureAction -eq "Install")
{

    Write-Host "Adding solution:" $solutionName " "
    Add-SPSolution $SolutionPath 

    if ( $installToURL -ne "Yes" )
    {
        Write-Host "Deploying solution:" $solutionName "to GAC "
        Install-SPSolution -Identity $solutionName  -AllWebApplications -GACDeployment -CASPolicies -Confirm:$false
    }
    else
    {
        Write-Host "Deploying solution:" $solutionName "to" $urlPath "and GAC "
        Install-SPSolution -Identity $solutionName -WebApplication $urlPath -GACDeployment -Confirm:$false
    }

    Write-Host "Waiting till deployment has finished " -nonewline
    do
    {
        Start-Sleep 2
        Write-Host "." -nonewline
    }
    while ((Get-SPSolution $solutionName).Deployed)

    Start-Sleep 5
    Write-Host "." -nonewline

    Write-Host $solutionName " Now installed!"

}

if ( $featureAction -eq "Update")
{
    Write-Host "Updating solution:" $solutionName " "
    Update-SPSolution -Identity $solutionName -LiteralPath $SolutionPath -GACDeployment -Confirm:$false
    Write-Host $solutionName " Now updated!"
}

Write-Host "" 

Stop-Transcript

Remove-PsSnapin Microsoft.SharePoint.PowerShell
To use POWERSHELL-SharePoint-WebPart.ps1, create dos bat file called
mywebpart-install.bat
@echo off
REM                                                    Current               Site Collection
REM               Script to install webpart            Dir    webpart Name   to Install to       Action (Install, Update, Remove)
REM              ----------------------------------    ------ -------------- ------------------- ------------
powershell -file ".\POWERSHELL-SharePoint-WebPart.ps1" "%CD%" "mywebpart" "http://stage.company.com" "Install"

FEATURES

POWERSHELL-SharePoint-Feature.ps1

Add-PsSnapin Microsoft.SharePoint.PowerShell

Write-Host 'Will initialize parameters'
$CurrentDir=$args[0]
$solutionName=$args[1] + ".wsp"
$featureName=$args[1]
$SolutionPath=$CurrentDir + "\" + $solutionName
$urlPath=$args[2]
$action=$args[3]

$logfile=$CurrentDir + "\" + $args[1] + "_" + $action + ".log"

Write-Host "Logfile: " + $logfile + " Starting...."
Start-Transcript $logfile

$errorActionPreference = 'Inquire'
if ( $action -eq "Remove" )
{
	Write-Host $action ' ' $featureName '....'

	Write-Host 'Will now disable feature, ' $featureName ' for all site in Site Collection in ' $urlPath

	# Get Feature object
	$feature = Get-SPFeature $featureName

	#Get web application
	$webApp = Get-SPWebApplication -Identity $urlPath

	#Deactivate site scoped feature for all site collections in a Web Application (checks to see if the feature is activated before attempting to deactivate)
	Write-Host 'Will now deactivate solution:' $solutionName
	$webApp | Get-SPSite -limit all | ForEach-Object {
    	Write-Host '...Checking Site ' $_.Url ' for ' $solutionName
        if ($_.Features[$feature.ID]) {
            Write-Host '... ...deactiviatng feature ' $solutionName ' from ' $_.Url
            Disable-SPFeature $feature -Url $_.Url -Force -Confirm:$false
		}
	}

    $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}

	Write-Host 'Will now retract solution:' $solutionName
	Uninstall-SPSolution -Identity $solutionName -Confirm:$false

	#Wait for solution to be uninstalled
    while ($solution.Deployed -eq $true)
    {
        Write-Host '.'  -nonewline
		Start-Sleep -s 1
    }
    #make sure job is finished
    while ( $solution.JobExists )
    {
        write-host '.'  -nonewline
        sleep 1
    }
	write-host ''
	Write-Host 'Will now remove solution:' $solutionName
	Remove-SPSolution -Identity $solutionName -force -Confirm:$false
}

if ( $action -eq "Install")
{

	Write-Host 'Will now add solution:' $solutionName
	Add-SPSolution -LiteralPath $SolutionPath 

	Write-Host 'Will now deploy solution:' $solutionName
	Install-SPSolution -Identity $solutionName -GACDeployment  -force

    $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}

    while ( $solution.JobExists )
    {
        write-host '.'  -nonewline
        sleep 1
    }
	write-host ''
    write-host 'Solution, ' $solutionName ', ' $action '!'

}

if ( $action -eq "Update")
{
	Write-Host 'Will now update solution:' $solutionName
	Update-SPSolution -Identity $solutionName -LiteralPath $SolutionPath -GACDeployment  -force

    $solution = Get-SPSolution | where-object {$_.Name -eq $solutionName}

    while ( $solution.JobExists )
    {
        write-host '.'  -nonewline
        sleep 1
    }
	write-host ''
    write-host 'Solution, ' $solutionName ', ' $action '!'
}

#place bland line to fix log format
write-host ' '

#Stop Log
Stop-Transcript

#fix output file so it has newlines in notepad
[string]::Join("`r`n",(Get-Content $logfile)) | Out-File $logfile

Remove-PsSnapin Microsoft.SharePoint.PowerShell

To use POWERSHELL-SharePoint-Feature.ps1, create dos bat file called
myFeature-install.bat

@echo off
REM                                                    Current               Site Collection
REM               Script to install feature            Dir    feature Name   to Install to       Action (Install, Update, Remove)
REM              ----------------------------------    ------ -------------- ------------------- ------------
powershell -file ".\POWERSHELL-SharePoint-WebPart.ps1" "%CD%" "myfeature" "http://stage.company.com" "Install"


Copyright © 2012-2014. All rights reserved.

Posted September 11 by John Hutton in category "SharePoint", "SharePoint 2010", "SharePoint 2013