Powershell Script to Validate on Whether Custom Feature Requires Upgrade

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