Thursday, August 30, 2012

.ps1 Script for Packaging of SharePoint 2010 Application.


How to Deploy application on Client Machine using Power-cell Scripting ?

Steps : 

1) Take a backup of application from Site Settings ->Save Site as Template. Download .wsp at local machine.
2) Now put .wsp ,.ps1 and installer in same folder.

in installer.Bat file we have to write
============================================

cd /d %~dp0

%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe -file .\Install.ps1 %1 %2 %3 %4 "%~dp0

pause

============================================
3) Now here is .ps1 script for application.
 ( We can direct run the .Ps1 by right click on .Ps1 ->Run with PowerCell )


$sp_webapp_hostheader = "EDMS"
$sp_webapp_port = "2000"
$sp_webapp_apppoolaccount = "spserver3\administrator"
$sp_webapp_databaseserver = "spserver3\administrator"
$sp_wsp_path = "spserver3:2000"
$CurrentDir = "spserver3:2000"


$file = $CurrentDir + "Install.log"
start-transcript $file
trap {stop-transcript; break}

$ErrorActionPreference = "Stop"
#=======================================================================
#Include the SharePoint cmdlets

sleep 2
Add-PsSnapin Microsoft.SharePoint.PowerShell
sleep 2

# Set Execution Policy
Set-ExecutionPolicy unrestricted
sleep 3

Write-Host ‘———————————————————--------------------------------------------------------’
Write-Host ‘ EDMS SHAREPOINT Deployment.....’
Write-Host ‘———————————————————--------------------------------------------------------’
Write-Host ‘  ’

Write-Host $sp_webapp_hostheader
Write-Host ‘  ’
Write-Host $sp_webapp_port
Write-Host ‘  ’
Write-Host $sp_webapp_apppoolaccount
Write-Host ‘  ’
Write-Host $sp_webapp_databaseserver
Write-Host ‘  ’
Write-Host $sp_wsp_path
Write-Host ‘  ’
#====================================================================
#Create Web Application
#====================================================================

$sp_webapp_name = "EDMS1_" + $sp_webapp_port
$sp_wsp_name = "EDMS1.wsp"
$sp_wsp_path =  "spserver3:2000" + $sp_wsp_name
$sp_webapp_url = "http://" + $sp_webapp_hostheader
$sp_webapp_apppool = "EDMS1" + $sp_webapp_port
$sp_webapp_databasename = $sp_webapp_name

Write-Host $sp_webapp_name
Write-Host ‘  ’
Write-Host $sp_webapp_port
Write-Host ‘  ’
Write-Host $sp_webapp_hostheader
Write-Host ‘  ’
Write-Host $sp_webapp_url
Write-Host ‘  ’
Write-Host $sp_webapp_apppool
Write-Host ‘  ’
Write-Host $sp_webapp_apppoolaccount
Write-Host ‘  ’
Write-Host $sp_webapp_databasename
Write-Host ‘  ’
Write-Host $sp_webapp_databaseserver
Write-Host ‘  ’

$sp = Get-SPWebApplication | Where {$_.DisplayName -eq $sp_webapp_name}
Write-Host ‘  ’
Write-Host $sp
Write-Host ‘’

sleep 2
if($sp -eq $null)
{

#Create a new Web Application
New-SPWebApplication -name $sp_webapp_name -Port $sp_webapp_port -HostHeader $sp_webapp_hostheader -URL $sp_webapp_url -ApplicationPool $sp_webapp_apppool -ApplicationPoolAccount $sp_webapp_apppoolaccount -DatabaseName $sp_webapp_databasename -DatabaseServer $sp_webapp_databaseserver

# display site info
Write-Host
Write-Host "Web App created" -foregroundcolor Green
Write-Host "-------------------------------------" -foregroundcolor Green
}
else
{
      Write-Host
      Write-Host "Web Application already exists, skipping this step" -foregroundcolor yellow
}

sleep 2

#====================================================================
#Create Site Collection
#====================================================================

$sp_sc_webappurl = $sp_webapp_url + ":" + $sp_webapp_port
$sp_sc_name = "EDMS1"
Write-Host ‘  ’

$targetUrl = Get-SPSite | Where-Object {$_.Url -eq $sp_sc_webappurl }
if ($targetUrl -ne $null)
{
  Write-Host "Site already exist, Deleting existing site : " $sp_sc_webappurl -foregroundcolor yellow

  Remove-SPSite -Identity $sp_sc_webappurl -Confirm:$false

  Write-Host
  Write-Host "Existing site collection deleted..." -foregroundcolor yellow
}

$err = @()
#new-SPSite -url $sp_sc_webappurl -OwnerAlias Administrator -Name $sp_sc_name -ErrorVariable err  -ErrorAction SilentlyContinue
new-SPSite -url $sp_sc_webappurl -OwnerAlias Administrator -Name $sp_sc_name -ErrorVariable err  -ErrorAction SilentlyContinue

if ($err.count > 0 )
{
[System.Console]::Clear();
Write-Host $err
}

Write-Host
Write-Host "Site collection Created..." $sp_sc_webappurl  -foregroundcolor yellow

#****************  Edms  ************

sleep 3

$sp_sc_url = $sp_webapp_url + ":" + $sp_webapp_port
$sp_wsp_name = "EDMS1.wsp"
write-host " attached"

# If solution already exists, Retract, Remove old solution and Add, Deploy new solution

$solution = Get-SPSolution $sp_wsp_name -ErrorAction SilentlyContinue
if ($solution -ne $null)
{
  Write-Host
  Write-Host "Solution already exists. " $sp_wsp_name -foregroundcolor Yellow

  $sp_wsp_name | Uninstall-SPSolution -AllWebApplications  -confirm:$false  

  Write-Host "Retracting solution..." -foregroundcolor Yellow

  # Retracting job is assigned to timer job, so we need to wait for it to complete before removing solution.

   while ( $sp_wsp_name.JobExists )
  {
   sleep 3
   write-host "."
  }

  write-host "solution retracted" -foregroundcolor yellow
  Write-Host
  Write-Host "Removing solution " $sp_wsp_name -foregroundcolor Yellow
 
    #$sp_wsp_name | Remove-SPSolution -confirm:$false
  Remove-SPSolution–Identity $sp_wsp_name -confirm:$false
  Write-Host "Removed"

}

write-host "Adding solution " -foregroundcolor yellow

$sp_wsp_name_fullPath =  $CurrentDir + $sp_wsp_name

Add-SPUserSolution -LiteralPath $sp_wsp_name_fullPath -Site $sp_sc_webappurl

write-host "Installing solution " -foregroundcolor yellow

Install-SPUserSolution –Identity $sp_wsp_name -Site $sp_sc_webappurl

Write-Host "Site Created"

sleep 30

$x = Get-SPSite $sp_sc_webappurl
write-host "1" $x
$y = $x.GetWebTemplates(1033) | Where { $_.Title -eq "EDMS1" }
write-host "  2" $y
[system.reflection.assembly]::LoadWithPartialName("Microsoft.Sharepoint")
write-host "  3"
$site= new-Object Microsoft.SharePoint.SPSite($sp_sc_webappurl)
write-host "  4" $site
$site.OpenWeb().ApplyWebTemplate($y)
write-host "  5"
$site.Dispose()

Write-Host "Done "
#====================================================================
stop-transcript

4) Now run the .bat file. Installation will be finished.

No comments:

Post a Comment