Saturday, August 27, 2016

Automation of sharepoint deployment

# Automated Deployment Script for SP Packages

Add-PsSnapin Microsoft.SharePoint.PowerShell

# Initiate backup of Site collection

#$DatetimeStamp = Get-date -format g

#$DatetimeStamp = $DatetimeStamp.replace("/" , "_")

#[string]$DatetimeStamp = $DatetimeStamp.replace(":" , "_")

#$bakuppath =  "\\BackupLocationPath\" + $DatetimeStamp + ".bak"

#Backup-SPSite "BackUpSiteURL/" -path $bakuppath -Force

# Backup Completed

# Script Directory
function Get-ScriptDirectory
{
 $Invocation = (Get-Variable MyInvocation -Scope 1).Value
 Split-Path $Invocation.MyCommand.Path
}

# Deploy Solution
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 $AdminServiceNames).Status -eq "Stopped")
{
    $IsAdminServiceWasRunning = $false;
    Start-Service $AdminServiceName
   Write-Host 'SERVICE WAS STOPPED, SO IT IS NOW STARTED'
}

#Uninstall
Write-Host 'UNINSTALLING SOLUTION ...'

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

if ($Solution -ne $null)
{
    if($Solution.ContainsWebApplicationResource)
    {
        Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false
    }
    else
    {
        Uninstall-SPSolution $SolutionName -Confirm:$false
    }
   
    while ($Solution.JobExists)
   {
    Start-Sleep 2
   }

   Write-Host 'SOLUTION HAS BEEN UNINSTALLED SUCCESSFULLY.'
  
}
 else
 {
   Write-Host 'SOLUTION HAS NOT BEEN UNINSTALLED EITHER IT DOES NOT EXIST OR WAS NOT DEPLOYED PRIOR.'
 }



Write-Host 'REMOVING SOLUTION ...'

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

Write-Host 'SOLUTION HAS BEEN REMOVED SUCCESSFULLY.'
}
 else
 {
   Write-Host 'SOLUTION HAS NOT BEEN REMOVED IT DOES NOT EXIST .'
 }

Write-Host 'ADDING SOLUTION ...'

Add-SPSolution $SolutionPath  | Out-Null

Write-Host 'SOLUTION HAS BEEN ADDED SUCCESSFULLY.'

Write-Host 'DEPLOYING SOLUTION ...'

$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 –WebApplication "WebApplicationName/" -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.'

Remove-Item $SolutionPath -force

Write-Host 'PACKAGE HAS BEEN Removed FROM THE FOLDER'

#if (-not $IsAdminServiceWasRunning)
#{
 #   Start-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}

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

Echo Finish

No comments:

Post a Comment