Powershell to Deploy all WSP from Folder

  1. ##############################################  
  2. Set Variables#####################################################  
  3. $InstallDIR = "F:\Deploy"  
  4.  
  5. ##########################################  
  6. CODE, No Changes Necessary############################################  
  7. Write - Host "Working, Please wait...."  
  8. Add - PSSnapin microsoft.sharepoint.powershell - ErrorAction SilentlyContinue  
  9.   
  10. $Dir = get - childitem $InstallDIR - Recurse  
  11. $WSPList = $Dir | where {  
  12.     $_.Name - like "*.wsp*"  
  13. }  
  14. Foreach($wsp in $WSPList) {  
  15.     $WSPFullFileName = $wsp.FullName  
  16.     $WSPFileName = $wsp.Name  
  17.     clear  
  18.     Write - Host - ForegroundColor White - BackgroundColor Blue "Working on $WSPFileName"  
  19.   
  20.     try {  
  21.         Write - Host - ForegroundColor Green "Checking Status of Solution"  
  22.         $output = Get - SPSolution - Identity $WSPFileName - ErrorAction Stop  
  23.     }  
  24.     Catch {  
  25.         $DoesSolutionExists = $_  
  26.     }  
  27.     If(($DoesSolutionExists - like "*Cannot find an SPSolution*") - and($output.Name - notlike "*$WSPFileName*")) {  
  28.         Try {  
  29.             Write - Host - ForegroundColor Green "Adding solution to farm"  
  30.             Add - SPSolution "$WSPFullFileName" - Confirm: $false - ErrorAction Stop | Out - Null  
  31.   
  32.             Write - Host - ForegroundColor Green "Checking Status of Solution"  
  33.             $output = Get - SPSolution - Identity $WSPFileName - ErrorAction Stop  
  34.             $gobal = $null  
  35.             if ($output.Deployed - eq $false) {  
  36.                 try {  
  37.                     Write - Host - ForegroundColor Green "Deploy solution to all Web Apps, will skip if this solution is globally deployed"  
  38.                     Install - SPSolution - Identity "$WSPFileName" - GACDeployment - AllWebApplications - Force - Confirm: $false - ErrorAction Stop | Out - Null  
  39.                 }  
  40.                 Catch {  
  41.                     $gobal = $_  
  42.                 }  
  43.                 If($gobal - like "*This solution contains*") {  
  44.                     Write - Host - ForegroundColor Green "Solution requires global deployment, Deploying now"  
  45.                     Install - SPSolution - Identity "$WSPFileName" - GACDeployment - Force - Confirm: $false - ErrorAction Stop | Out - Null  
  46.                 }  
  47.             }  
  48.   
  49.             Sleep 1  
  50.             $dpjobs = Get - SPTimerJob | Where {  
  51.                 $_.Name - like "*$WSPFileName*"  
  52.             }  
  53.             If($dpjobs - eq $null) {  
  54.                 Write - Host - ForegroundColor Green "No solution deployment jobs found"  
  55.             }  
  56.             Else {  
  57.                 If($dpjobs - is[Array]) {  
  58.                     Foreach($job in $dpjobs) {  
  59.                         $jobName = $job.Name  
  60.                         While((Get - SPTimerJob $jobName - Debug: $false) - ne $null) {  
  61.                             Write - Host - ForegroundColor Yellow - NoNewLine "."  
  62.                             Start - Sleep - Seconds 5  
  63.                         }  
  64.                         Write - Host  
  65.                     }  
  66.                 }  
  67.                 Else {  
  68.                     $jobName = $dpjobs.Name  
  69.                     While((Get - SPTimerJob $jobName - Debug: $false) - ne $null) {  
  70.                         Write - Host - ForegroundColor Yellow - NoNewLine "."  
  71.                         Start - Sleep - Seconds 5  
  72.                     }  
  73.                     Write - Host  
  74.                 }  
  75.             }  
  76.         }  
  77.         Catch {  
  78.             Write - Error $_  
  79.             Write - Host - ForegroundColor Red "Skipping $WSPFileName, Due to an error"  
  80.             Read - Host  
  81.         }  
  82.     }  
  83.     Else {  
  84.         $skip = $null  
  85.         $tryagain = $null  
  86.         Try {  
  87.             if ($output.Deployed - eq $true) {  
  88.                 Write - Host - ForegroundColor Green "Retracting Solution"  
  89.                 Uninstall - SPSolution - AllWebApplications - Identity $WSPFileName - Confirm: $false - ErrorAction Stop  
  90.             }  
  91.         }  
  92.         Catch {  
  93.             $tryagain = $_  
  94.         }  
  95.         Try {  
  96.             if ($tryagain - ne $null) {  
  97.                 Uninstall - SPSolution - Identity $WSPFileName - Confirm: $false - ErrorAction Stop  
  98.             }  
  99.         }  
  100.         Catch {  
  101.             Write - Host - ForegroundColor Red "Could not Retract Solution"  
  102.         }  
  103.   
  104.         Sleep 1  
  105.         $dpjobs = Get - SPTimerJob | Where {  
  106.             $_.Name - like "*$WSPFileName*"  
  107.         }  
  108.         If($dpjobs - eq $null) {  
  109.             Write - Host - ForegroundColor Green "No solution deployment jobs found"  
  110.         }  
  111.         Else {  
  112.             If($dpjobs - is[Array]) {  
  113.                 Foreach($job in $dpjobs) {  
  114.                     $jobName = $job.Name  
  115.                     While((Get - SPTimerJob $jobName - Debug: $false) - ne $null) {  
  116.                         Write - Host - ForegroundColor Yellow - NoNewLine "."  
  117.                         Start - Sleep - Seconds 5  
  118.                     }  
  119.                     Write - Host  
  120.                 }  
  121.             }  
  122.             Else {  
  123.                 $jobName = $dpjobs.Name  
  124.                 While((Get - SPTimerJob $jobName - Debug: $false) - ne $null) {  
  125.                     Write - Host - ForegroundColor Yellow - NoNewLine "."  
  126.                     Start - Sleep - Seconds 5  
  127.                 }  
  128.                 Write - Host  
  129.             }  
  130.         }  
  131.   
  132.         Try {  
  133.             Write - Host - ForegroundColor Green "Removing Solution from farm"  
  134.             Remove - SPSolution - Identity $WSPFileName - Confirm: $false - ErrorAction Stop  
  135.         }  
  136.         Catch {  
  137.             $skip = $_  
  138.             Write - Host - ForegroundColor Red "Could not Remove Solution"  
  139.             Read - Host  
  140.         }  
  141.         if ($skip - eq $null) {  
  142.             Try {  
  143.                 Write - Host - ForegroundColor Green "Adding solution to farm"  
  144.                 Add - SPSolution "$WSPFullFileName" - Confirm: $false - ErrorAction Stop | Out - Null  
  145.                 $gobal = $null  
  146.                 try {  
  147.                     Write - Host - ForegroundColor Green "Deploy solution to all Web Apps, will skip if this solution is globally deployed"  
  148.                     Install - SPSolution - Identity "$WSPFileName" - GACDeployment - AllWebApplications - Force - Confirm: $false - ErrorAction Stop | Out - Null  
  149.                 }  
  150.                 Catch {  
  151.                     $gobal = $_  
  152.                 }  
  153.                 If($gobal - like "*This solution contains*") {  
  154.                     Write - Host - ForegroundColor Green "Solution requires global deployment, Deploying now"  
  155.                     Install - SPSolution - Identity "$WSPFileName" - GACDeployment - Force - Confirm: $false - ErrorAction Stop | Out - Null  
  156.                 }  
  157.             }  
  158.             Catch {  
  159.                 Write - Error $_  
  160.                 Write - Host - ForegroundColor Red "Skipping $WSPFileName, Due to an error"  
  161.                 Read - Host  
  162.             }  
  163.   
  164.             Sleep 1  
  165.             $dpjobs = Get - SPTimerJob | Where {  
  166.                 $_.Name - like "*$WSPFileName*"  
  167.             }  
  168.             If($dpjobs - eq $null) {  
  169.                 Write - Host - ForegroundColor Green "No solution deployment jobs found"  
  170.             }  
  171.             Else {  
  172.                 If($dpjobs - is[Array]) {  
  173.                     Foreach($job in $dpjobs) {  
  174.                         $jobName = $job.Name  
  175.                         While((Get - SPTimerJob $jobName - Debug: $false) - ne $null) {  
  176.                             Write - Host - ForegroundColor Yellow - NoNewLine "."  
  177.                             Start - Sleep - Seconds 5  
  178.                         }  
  179.                         Write - Host  
  180.                     }  
  181.                 }  
  182.                 Else {  
  183.                     $jobName = $dpjobs.Name  
  184.                     While((Get - SPTimerJob $jobName - Debug: $false) - ne $null) {  
  185.                         Write - Host - ForegroundColor Yellow - NoNewLine "."  
  186.                         Start - Sleep - Seconds 5  
  187.                     }  
  188.                     Write - Host  
  189.                 }  
  190.             }  
  191.         }  
  192.         Else {  
  193.             Write - Host - ForegroundColor Red "Cannot Install $WSPFileName, Please try manually"  
  194.             Read - Host  
  195.         }  
  196.     }  
  197. }