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. catch[Exception] {
  31. Write - Error $Error[0]
  32. $err = $_.Exception
  33. while ($err.InnerException) {
  34. $err = $err.InnerException
  35. Write - Output $err.Message
  36. }
  37. }
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. $scriptBase = split - path $SCRIPT: MyInvocation.MyCommand.Path - parent
  8. Set - Location $scriptBase
  9. #Deleting any.rtf files in the scriptbase location
  10. $FindRTFFile = Get - ChildItem $scriptBase\ * .*-include * .rtf
  11. if ($FindRTFFile) {
  12. foreach($file in $FindRTFFile) {
  13. remove - item $file
  14. }
  15. }
  16. start - transcript $logfile
  17. $featureName = ""
  18. [string] $featureName = Read - Host "Enter the Feature name [e.g. MyCustomFeature]"
  19. try {
  20. Write - Verbose "Query for features requiring upgrading ..."
  21. $fd = Get - SPFeature $featureName
  22. switch ($fd.Scope) {
  23. "Farm" {
  24. $output = [Microsoft.SharePoint.Administration.SPWebService]::AdministrationService.QueryFeatures($fd.Id, $true)
  25. break
  26. }
  27. "WebApplication" {
  28. $output = [Microsoft.SharePoint.Administration.SPWebService]::QueryFeaturesInAllWebServices($fd.Id, $true)
  29. break
  30. }
  31. "Site" {
  32. $output = foreach($webapp in Get - SPWebApplication) {
  33. $webapp.QueryFeatures($fd.Id, $true)
  34. }
  35. break
  36. }
  37. "Web" {
  38. $output = foreach($site in Get - SPSite - Limit All) {
  39. $site.QueryFeatures($fd.Id, $true)
  40. }
  41. break
  42. }
  43. }
  44. Write - Output $output
  45. }
  46. catch[Exception] {
  47. Write - Error $Error[0]
  48. $err = $_.Exception
  49. while ($err.InnerException) {
  50. $err = $err.InnerException
  51. Write - Output $err.Message
  52. }
  53. }
  54. 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.