About Me

Having 12 years experience in Microsoft technologies.Since more than 7 years working in SharePoint technologies. Expert in providing consultation for SharePoint projects. Hands on with development and administration.

Thursday 12 December 2013

PowerShell script to deploy Multiple Farm solutions from same directory - single click - SharePoint 2010, 2013

Here is the Power Shell script to deploy multiple SharePoint farm solutions from same directory

Note: 
1.  Make sure that, your solutions should be in the same directory from where you are running the script.

2. Replace $webapp with your respective web application url @ line #3

3. Pass necessary parameters to Install-SPSolution command.

4. Save file with .ps1 as extension. Example: psDeploy.ps1

5. Create .bat file and call the .ps1 file in it. Example : Deploy.bat

cd /d %~dp0
powershell -noexit -file    ".\
psDeploy.ps1" "%CD%"
pause


6. Once done, directly run the .bat file. No need of launching PowerShell console to execute.
-------------------------------------------------------------------------------------------------------------------------
Add-PsSnapin Microsoft.SharePoint.PowerShell

$logfile=$CurrentDir + "\log.log"

$webapp = "http://sp2010"

Start-Transcript $logfile

#Do not modify anything in the script from here onwards
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

function Deploy-Solution{
param(
[string]$physicalPath,
[string]$name)


$SolutionName = $name
$SolutionPath = Join-Path ($physicalPath) $SolutionName
echo "Extracting information from $physicalPath"

#Admin service
$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
    $IsAdminServiceWasRunning = $false;
    Start-Service $AdminServiceName
   Write-Host 'SERVICE WAS STOPPED, SO IT IS NOW STARTED'
}

#Uninstall
Write-Host 'UNINSTALLING SOLUTION:' $SolutionName '...'

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}

if ($Solution -ne $null)
{
    if($Solution.ContainsWebApplicationResource)
    {
        #Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false

    Uninstall-SPSolution $SolutionName -WebApplication $webapp -Confirm:$false
    }
    else
    {
        Uninstall-SPSolution $SolutionName -Confirm:$false
    }
}

while ($Solution.JobExists)
{
    Start-Sleep 2
}

Write-Host 'THE SOLUTION:' $SolutionName 'HAS BEEN UNINSTALLED SUCCESSFULLY.'

Write-Host 'REMOVING SOLUTION ...'

if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)
{
    Remove-SPSolution $SolutionName -Confirm:$false

Write-Host 'THE SOLUTION:' $SolutionName 'HAS BEEN REMOVED SUCCESSFULLY.'
}

Write-Host 'ADDING SOLUTION: '$SolutionName '...'

Add-SPSolution $SolutionPath  | Out-Null

Write-Host 'THE SOLUTION:' $SolutionName 'HAS BEEN ADDED SUCCESSFULLY.'

Write-Host 'DEPLOYING SOLUTION:' $SolutionName '...'

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}

#use '-force' paramater to install all commands in this if statement

if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))
{
#Install-SPSolution $SolutionName –AllwebApplications -GACDeployment -Force -Confirm:$false

Install-SPSolution $SolutionName –WebApplication $webapp -GACDeployment -Force -Confirm:$false
}
else
{
Install-SPSolution $SolutionName -GACDeployment -Force -Confirm:$false
}

while ($Solution.Deployed -eq $false)
{
    Start-Sleep 2
}

Write-Host 'SOLUTION HAS BEEN DEPLOYED SUCCESSFULLY.'

if (-not $IsAdminServiceWasRunning)
{
    Stop-Service $AdminServiceName
}
}

#Get Current Physical Path
$currentPhysicalPath = Get-ScriptDirectory

#Iterate through all .wsp files in the current Physical Path to deploy solution
get-childitem $currentPhysicalPath -include *.wsp -recurse | foreach ($_) {Deploy-Solution $currentPhysicalPath $_.name}

Stop-Transcript

#Remove SharePoint Snapin
Remove-PsSnapin Microsoft.SharePoint.PowerShell

Echo Finish
----------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment