Powershell Script to Check Whether SharePoint Feature Requires Upgrade

Introduction

This article outlines how to use a PowerShell script to determine whether a custom feature requires an upgrade.

Custom Feature

The following piece of code checks whether the feature requires an upgrade.

  1. $featureName = ""  
  2. [string] $featureName = Read - Host "Enter the Feature name [e.g. MyCustomFeature]"  
  3. try {  
  4.     Write - Verbose "Query for features requiring upgrading ..."  
  5.     $fd = Get - SPFeature $featureName  
  6.     switch ($fd.Scope) {  
  7.         "Farm" {  
  8.             $output = [Microsoft.SharePoint.Administration.SPWebService]::AdministrationService.QueryFeatures($fd.Id, $true)  
  9.             break  
  10.         }  
  11.         "WebApplication" {  
  12.             $output = [Microsoft.SharePoint.Administration.SPWebService]::QueryFeaturesInAllWebServices($fd.Id, $true)  
  13.             break  
  14.         }  
  15.         "Site" {  
  16.             $output = foreach($webapp in Get - SPWebApplication) {  
  17.                 $webapp.QueryFeatures($fd.Id, $true)  
  18.             }  
  19.             break  
  20.         }  
  21.         "Web" {  
  22.             $output = foreach($site in Get - SPSite - Limit All) {  
  23.                 $site.QueryFeatures($fd.Id, $true)  
  24.             }  
  25.             break  
  26.         }  
  27.     }  
  28.     Write - Output $output  
  29.   
  30. }   
  31. catch[Exception] {  
  32.     Write - Error $Error[0]  
  33.     $err = $_.Exception  
  34.     while ($err.InnerException) {  
  35.         $err = $err.InnerException  
  36.         Write - Output $err.Message  
  37.     }  
  38. }  
Complete Code
  1. $LogTime = Get - Date - Format yyyy - MM - dd_hh - mm  
  2. $LogFile = ".\ValidateFeatureNeedsUpgradePatch-$LogTime.rtf"  
  3. #Add SharePoint PowerShell Snapin  
  4. if ((Get - PSSnapin - Name Microsoft.SharePoint.PowerShell - ErrorAction SilentlyContinue) - eq $null) {  
  5.     Add - PSSnapin Microsoft.SharePoint.Powershell  
  6. }  
  7.   
  8. $scriptBase = split - path $SCRIPT: MyInvocation.MyCommand.Path - parent  
  9. Set - Location $scriptBase  
  10. #Deleting any.rtf files in the scriptbase location  
  11. $FindRTFFile = Get - ChildItem $scriptBase\ * .*-include * .rtf  
  12. if ($FindRTFFile) {  
  13.     foreach($file in $FindRTFFile) {  
  14.         remove - item $file  
  15.     }  
  16. }  
  17. start - transcript $logfile  
  18.   
  19. $featureName = ""  
  20.   
  21. [string] $featureName = Read - Host "Enter the Feature name [e.g. MyCustomFeature]"  
  22.   
  23.   
  24. try {  
  25.   
  26.     Write - Verbose "Query for features requiring upgrading ..."  
  27.   
  28.     $fd = Get - SPFeature $featureName  
  29.   
  30.     switch ($fd.Scope) {  
  31.         "Farm" {  
  32.             $output = [Microsoft.SharePoint.Administration.SPWebService]::AdministrationService.QueryFeatures($fd.Id, $true)  
  33.             break  
  34.         }  
  35.         "WebApplication" {  
  36.             $output = [Microsoft.SharePoint.Administration.SPWebService]::QueryFeaturesInAllWebServices($fd.Id, $true)  
  37.             break  
  38.         }  
  39.         "Site" {  
  40.             $output = foreach($webapp in Get - SPWebApplication) {  
  41.                 $webapp.QueryFeatures($fd.Id, $true)  
  42.             }  
  43.             break  
  44.         }  
  45.         "Web" {  
  46.             $output = foreach($site in Get - SPSite - Limit All) {  
  47.                 $site.QueryFeatures($fd.Id, $true)  
  48.             }  
  49.             break  
  50.         }  
  51.     }  
  52.     Write - Output $output  
  53.   
  54. }   
  55. catch[Exception] {  
  56.     Write - Error $Error[0]  
  57.     $err = $_.Exception  
  58.     while ($err.InnerException) {  
  59.         $err = $err.InnerException  
  60.         Write - Output $err.Message  
  61.     }  
  62. }  
  63.   
  64. stop - transcript  
Execution Procedure

 

  1. Download and copy the script to the SharePoint server
  2. Launch the SharePoint management shell
  3. Navigate to the script path and execute as in the following:


Enter the feature name to check the status of its upgrade.

Conclusion

Thus this article outlines how to use a PowerShell script to check whether or not a SharePoint custom feature requires an upgrade.