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.
- $featureName = ""
- [string] $featureName = Read - Host "Enter the Feature name [e.g. MyCustomFeature]"
- try {
- Write - Verbose "Query for features requiring upgrading ..."
- $fd = Get - SPFeature $featureName
- switch ($fd.Scope) {
- "Farm" {
- $output = [Microsoft.SharePoint.Administration.SPWebService]::AdministrationService.QueryFeatures($fd.Id, $true)
- break
- }
- "WebApplication" {
- $output = [Microsoft.SharePoint.Administration.SPWebService]::QueryFeaturesInAllWebServices($fd.Id, $true)
- break
- }
- "Site" {
- $output = foreach($webapp in Get - SPWebApplication) {
- $webapp.QueryFeatures($fd.Id, $true)
- }
- break
- }
- "Web" {
- $output = foreach($site in Get - SPSite - Limit All) {
- $site.QueryFeatures($fd.Id, $true)
- }
- break
- }
- }
- Write - Output $output
-
- }
- catch[Exception] {
- Write - Error $Error[0]
- $err = $_.Exception
- while ($err.InnerException) {
- $err = $err.InnerException
- Write - Output $err.Message
- }
- }
Complete Code
- $LogTime = Get - Date - Format yyyy - MM - dd_hh - mm
- $LogFile = ".\ValidateFeatureNeedsUpgradePatch-$LogTime.rtf"
- #Add SharePoint PowerShell Snapin
- if ((Get - PSSnapin - Name Microsoft.SharePoint.PowerShell - ErrorAction SilentlyContinue) - eq $null) {
- Add - PSSnapin Microsoft.SharePoint.Powershell
- }
-
- $scriptBase = split - path $SCRIPT: MyInvocation.MyCommand.Path - parent
- Set - Location $scriptBase
- #Deleting any.rtf files in the scriptbase location
- $FindRTFFile = Get - ChildItem $scriptBase\ * .*-include * .rtf
- if ($FindRTFFile) {
- foreach($file in $FindRTFFile) {
- remove - item $file
- }
- }
- start - transcript $logfile
-
- $featureName = ""
-
- [string] $featureName = Read - Host "Enter the Feature name [e.g. MyCustomFeature]"
-
-
- try {
-
- Write - Verbose "Query for features requiring upgrading ..."
-
- $fd = Get - SPFeature $featureName
-
- switch ($fd.Scope) {
- "Farm" {
- $output = [Microsoft.SharePoint.Administration.SPWebService]::AdministrationService.QueryFeatures($fd.Id, $true)
- break
- }
- "WebApplication" {
- $output = [Microsoft.SharePoint.Administration.SPWebService]::QueryFeaturesInAllWebServices($fd.Id, $true)
- break
- }
- "Site" {
- $output = foreach($webapp in Get - SPWebApplication) {
- $webapp.QueryFeatures($fd.Id, $true)
- }
- break
- }
- "Web" {
- $output = foreach($site in Get - SPSite - Limit All) {
- $site.QueryFeatures($fd.Id, $true)
- }
- break
- }
- }
- Write - Output $output
-
- }
- catch[Exception] {
- Write - Error $Error[0]
- $err = $_.Exception
- while ($err.InnerException) {
- $err = $err.InnerException
- Write - Output $err.Message
- }
- }
-
- stop - transcript
Execution Procedure
- Download and copy the script to the SharePoint server
- Launch the SharePoint management shell
- 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.