Deploy Multiple WSPs at Farm or Web Application Level

  1. #region OPTIONS  
  2. # CHECKS THE POWERSHELL VERSION  
  3. # ReuseThread: Creates a new thread for the first invocation and then re-uses that thread in   
  4. # subsequent invocations. This field is introduced in Windows PowerShell 2.0.  
  5.     $ver = $host | select version  
  6.     if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}   
  7.      
  8. # CHECKS FOR THE SHAREPOINT POWERSHELL SNAPIN, IF ITS NOT PRESENT IT LOADS IT  
  9.     if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
  10.         {  
  11.         Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable SnapinErr -ErrorAction SilentlyContinue  
  12.         if ($SnapinErr)  
  13.             {  
  14.             Write-Host "`r`n Error: Cannot install Microsoft.SharePoint.PowerShell Snapin. $SnapinErr" -ForegroundColor Red  
  15.             Write-Host "`r`n It is now safe to exit the console or press any button to exit..."  
  16.             [System.Console]::Read()  
  17.             exit  
  18.             }  
  19.         }  
  20.          
  21. # CHANGE THE TITLE OF THE CONSOLE  
  22. $a = (Get-Host).UI.RawUI  
  23. $a.WindowTitle = "Running: PowerShellInstallMultipleWSPs.ps1"     
  24.      
  25. #endregion  
  26.  
  27. #region VARIABLES  
  28.   
  29. $InstallDIR = "C:\WSPs" #WSPs directory  
  30. $InstallScope = "LOCAL" # "GLOBAL" - Farm or "LOCAL" - Web application Level  
  31. $WebAppName = "http://timesdash_dev.timesgroup.com/"  
  32.  
  33. #endregion  
  34.  
  35.  
  36. #region CODE  
  37. Write-Host "Working, Please wait...."  
  38.    
  39. $Dir = get-childitem $InstallDIR -Recurse  
  40. $WSPList = $Dir | where {$_.Name -like "*.wsp*"}  
  41. Foreach ($wsp in $WSPList )  
  42. {  
  43.     $WSPFullFileName = $wsp.FullName  
  44.     $WSPFileName = $wsp.Name  
  45.     Write-Host -ForegroundColor White -BackgroundColor Blue "Working on $WSPFileName"   
  46.    
  47.     try  
  48.     {  
  49.         Write-Host -ForegroundColor Green "Checking Status of Solution"  
  50.         $output = Get-SPSolution -Identity $WSPFileName -ErrorAction Stop  
  51.     }  
  52.     Catch  
  53.     {  
  54.         $DoesSolutionExists = $_  
  55.     }  
  56.     If (($DoesSolutionExists -like "*Cannot find an SPSolution*") -and ($output.Name -notlike  "*$WSPFileName*"))  
  57.     {  
  58.         Try  
  59.         {  
  60.             Write-Host -ForegroundColor Green "Adding solution to farm"  
  61.             Add-SPSolution "$WSPFullFileName" -Confirm:$false -ErrorAction Stop | Out-Null  
  62.    
  63.             Write-Host -ForegroundColor Green "Checking Status of Solution"  
  64.             $output = Get-SPSolution -Identity $WSPFileName -ErrorAction Stop  
  65.             $gobal = $null  
  66.             if ($output.Deployed -eq $false)  
  67.             {  
  68.                 try  
  69.                 {  
  70.                     Write-Host -ForegroundColor Green "Deploy solution"  
  71.                     if($InstallScope -eq "GLOBAL")  
  72.                     {  
  73.                         Install-SPSolution -Identity "$WSPFileName" -GACDeployment -AllWebApplications -Force -Confirm:$false -ErrorAction Stop | Out-Null  
  74.                     }  
  75.                     else  
  76.                     {  
  77.                         Install-SPSolution -Identity "$WSPFileName" -GACDeployment -WebApplication "$WebAppName" -Force -Confirm:$false -ErrorAction Stop | Out-Null                      
  78.                     }                     
  79.                 }  
  80.                 Catch  
  81.                 {  
  82.                     $gobal = $_  
  83.                 }  
  84.             }  
  85.    
  86.             Sleep 1  
  87.             $dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }  
  88.             If ($dpjobs -eq $null)  
  89.             {  
  90.                 Write-Host -ForegroundColor Green "No solution deployment jobs found"  
  91.             }  
  92.             Else  
  93.             {  
  94.                 If ($dpjobs -is [Array])  
  95.                 {  
  96.                     Foreach ($job in $dpjobs)  
  97.                     {  
  98.                         $jobName = $job.Name  
  99.                         While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)  
  100.                         {  
  101.                             Write-Host -ForegroundColor Yellow -NoNewLine "."  
  102.                             Start-Sleep -Seconds 5  
  103.                         }  
  104.                         Write-Host  
  105.                     }  
  106.                 }  
  107.                 Else  
  108.                 {  
  109.                     $jobName = $dpjobs.Name  
  110.                     While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)  
  111.                     {  
  112.                         Write-Host -ForegroundColor Yellow -NoNewLine "."  
  113.                         Start-Sleep -Seconds 5  
  114.                     }  
  115.                     Write-Host  
  116.                 }  
  117.             }  
  118.         }  
  119.         Catch  
  120.         {  
  121.             Write-Error $_  
  122.             Write-Host -ForegroundColor Red "Skipping $WSPFileName, Due to an error"  
  123.             Read-Host  
  124.         }  
  125.     }  
  126.     Else  
  127.     {  
  128.         $skip = $null  
  129.         $tryagain = $null  
  130.         Try  
  131.         {  
  132.             if ($output.Deployed -eq $true)  
  133.             {  
  134.             Write-Host -ForegroundColor Green "Retracting Solution"  
  135.             Uninstall-SPSolution -AllWebApplications -Identity $WSPFileName -Confirm:$false -ErrorAction Stop  
  136.             }  
  137.         }  
  138.         Catch  
  139.         {  
  140.             $tryagain = $_  
  141.         }  
  142.         Try  
  143.         {  
  144.             if ($tryagain -ne $null)  
  145.             {  
  146.                 Uninstall-SPSolution -Identity $WSPFileName -Confirm:$false -ErrorAction Stop  
  147.             }  
  148.         }  
  149.         Catch  
  150.         {  
  151.             Write-Host -ForegroundColor Red "Could not Retract Solution"  
  152.         }  
  153.    
  154.         Sleep 1  
  155.         $dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }  
  156.         If ($dpjobs -eq $null)  
  157.         {  
  158.             Write-Host -ForegroundColor Green "No solution deployment jobs found"  
  159.         }  
  160.         Else  
  161.         {  
  162.             If ($dpjobs -is [Array])  
  163.             {  
  164.                 Foreach ($job in $dpjobs)  
  165.                 {  
  166.                     $jobName = $job.Name  
  167.                     While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)  
  168.                     {  
  169.                         Write-Host -ForegroundColor Yellow -NoNewLine "."  
  170.                         Start-Sleep -Seconds 5  
  171.                     }  
  172.                     Write-Host  
  173.                 }  
  174.             }  
  175.             Else  
  176.             {  
  177.                 $jobName = $dpjobs.Name  
  178.                 While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)  
  179.                 {  
  180.                     Write-Host -ForegroundColor Yellow -NoNewLine "."  
  181.                     Start-Sleep -Seconds 5  
  182.                 }  
  183.                 Write-Host  
  184.             }  
  185.         }         
  186.    
  187.         Try  
  188.         {  
  189.             Write-Host -ForegroundColor Green "Removing Solution from farm"  
  190.             Remove-SPSolution -Identity $WSPFileName -Confirm:$false -ErrorAction Stop  
  191.         }  
  192.         Catch  
  193.         {  
  194.             $skip = $_  
  195.             Write-Host -ForegroundColor Red "Could not Remove Solution"  
  196.             Read-Host  
  197.         }  
  198.         if ($skip -eq $null)  
  199.         {  
  200.             Try  
  201.             {  
  202.                 Write-Host -ForegroundColor Green "Adding solution to farm"  
  203.                 Add-SPSolution "$WSPFullFileName" -Confirm:$false -ErrorAction Stop | Out-Null  
  204.                 $gobal = $null  
  205.                 try  
  206.                 {  
  207.                     Write-Host -ForegroundColor Green "Deploy solution"  
  208.                     if($InstallScope -eq "GLOBAL")  
  209.                     {  
  210.                         Install-SPSolution -Identity "$WSPFileName" -GACDeployment -AllWebApplications -Force -Confirm:$false -ErrorAction Stop | Out-Null  
  211.                     }  
  212.                     else  
  213.                     {  
  214.                         Install-SPSolution -Identity "$WSPFileName" -GACDeployment -WebApplication "$WebAppName" -Force -Confirm:$false -ErrorAction Stop | Out-Null  
  215.                     }  
  216.                 }  
  217.                 Catch  
  218.                 {  
  219.                     $gobal = $_  
  220.                 }  
  221.             }  
  222.             Catch  
  223.             {  
  224.                 Write-Error $_  
  225.                 Write-Host -ForegroundColor Red "Skipping $WSPFileName, Due to an error"  
  226.                 Read-Host  
  227.             }  
  228.    
  229.             Sleep 1  
  230.             $dpjobs = Get-SPTimerJob | Where { $_.Name -like "*$WSPFileName*" }  
  231.             If ($dpjobs -eq $null)  
  232.             {  
  233.                 Write-Host -ForegroundColor Green "No solution deployment jobs found"  
  234.             }  
  235.             Else  
  236.             {  
  237.                 If ($dpjobs -is [Array])  
  238.                 {  
  239.                     Foreach ($job in $dpjobs)  
  240.                     {  
  241.                         $jobName = $job.Name  
  242.                         While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)  
  243.                         {  
  244.                             Write-Host -ForegroundColor Yellow -NoNewLine "."  
  245.                             Start-Sleep -Seconds 5  
  246.                         }  
  247.                         Write-Host  
  248.                     }  
  249.                 }  
  250.                 Else  
  251.                 {  
  252.                     $jobName = $dpjobs.Name  
  253.                     While ((Get-SPTimerJob $jobName -Debug:$false) -ne $null)  
  254.                     {  
  255.                         Write-Host -ForegroundColor Yellow -NoNewLine "."  
  256.                         Start-Sleep -Seconds 5  
  257.                     }  
  258.                     Write-Host  
  259.                 }  
  260.             }  
  261.     }  
  262.     Else  
  263.     {  
  264.         Write-Host -ForegroundColor Red "Cannot Install $WSPFileName, Please try manually"  
  265.         Read-Host  
  266.     }  
  267. }  
  268. }  
  269. #endregion  
  270.  
  271. # CHANGE THE TITLE OF THE CONSOLE  
  272. $a = (Get-Host).UI.RawUI  
  273. $a.WindowTitle = "Completed: PowerShellInstallMultipleWSPs.ps1"   
  274. #Write-Host "It is now safe to exit the console or press any button to continue..."  
  275. #[System.Console]::Read()