Powershell query features returns wrong result
-
08-12-2019 - |
Question
I'm currently writing a powershell script which upgrades a set of solutions then a set of features from those solutions.
After the solution upgrades everything looks great until I Call QueryFeatures which proceeds to return an empty result set.
# This is just sample code
$site.QueryFeatures((Get-SPFeature | Where { $_.DisplayName -match "$FeatureName" } | Select -First 1).Id, $true)
I've tried disposing the site before use then creating a new site with new-object, and even Clearing the Object Model Cache. The only thing that works is to open a new powershell window and issuing the same command.
However this is part of a long running script so this obviously isn't the best solution. Has anyone run into this issue before?
Solution
It's a common issue when writing deployment scripts for SharePoint in PowerShell that often you need to spawn a new PowerShell in order to:
- Get rid of cahcing
- Get a clean AppDomain without old version of dlls loaded
The version of the dlls is going to be your next problem as I assume you're going to call Upgrade on the returned features, which might want to call into a FeatureReceiver which needs to be the new version.
So it's common to write the main script as a series of:
powershell -noexit -command &'Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue;substep.ps1' -sta
OTHER TIPS
Could be a timing issue. Stick a 60 second wait in your script just before this point and see if that helps