The requirement is finding and removing the faulty site collection as well as site features using PowerShell scripts below.
FaultyFeature.ps1
Param($WebAppUrl,$IsRemove);
Add-PSSnapin Microsoft.SharePoint.PowerShell
function get-spmissingfeatures($WebAppUrl,$IsRemove)
{
Write-Host -Fore Green "Starting..."
write-host "Web Application URL: " $WebAppUrl
#Log in Excel file
$xmlPath = "$((pwd).path)/FaultyFeatures.xml";
$resultInXml = new-object xml
$decl = $resultInXml.CreateXmlDeclaration("1.0", $null, $null)
$rootNode = $resultInXml.CreateElement("FaultyFeatures");
$resultInXml.InsertBefore($decl, $resultInXml.DocumentElement)
$resultInXml.AppendChild($rootNode);
#get the web application
$SPWebApp = Get-SPWebApplication $WebAppUrl
#Iterate all site collections
foreach ($site in $SPWebApp.Sites)
{
if ($site -ne $null)
{
#iterate all site features
foreach ($feature in $site.features) {
#check site feature which doesn't have the definition
if ($feature.definition -eq $null) {
Write-Host -Fore Red "--------------------------------------"
Write-Host -Fore Green "Missing site feature:"
$SiteUrl = $resultInXml.CreateElement("SiteUrl")
$SiteUrl.SetAttribute("SiteURL",$site.url)
$SiteUrl.SetAttribute("FeatureID",$feature.DefinitionId)
$SiteUrl.SetAttribute("FeatureScope","Site")
$rootNode.AppendChild($SiteUrl);
#Remove faulty feature
if([bool]::Parse($IsRemove))
{
$site.features.remove($feature.DefinitionId,$true)
}
}
}
#iterate all sub sites under the site collection
$webs = $site | get-spweb -limit all
foreach ($web in $webs) {
#check the web feature which doesn't have the definition
foreach ($feature in $web.features) {
if ($feature.definition -eq $null) {
Write-Host -Fore Red "--------------------------------------"
Write-Host -Fore Green "Missing web feature:"
#$site.features.remove($featureid)
$WebUrl = $resultInXml.CreateElement("WebUrl")
$WebUrl.SetAttribute("WebUrl",$web.url)
$WebUrl.SetAttribute("FeatureID",$feature.DefinitionId)
$WebUrl.SetAttribute("FeatureScope","Web")
$rootNode.AppendChild($WebUrl);
#Remove faulty feature
if([bool]::Parse($IsRemove))
{
$web.features.remove($feature.DefinitionId,$true)
}
}
}
}
}
}
$resultInXml.Save($xmlPath)
}
Start-Transcript
get-spmissingfeatures $WebAppUrl $IsRemove
Stop-Transcript
FaultyFeature.bat
echo off
cd %SystemRoot%\system32\WindowsPowerShell\v1.0
powershell {Set-ExecutionPolicy Unrestricted}
cd /d %~dp0
SET WebAppUrl="http://servername:7576"
SET IsRemove=false
powershell.exe -command .\FaultyFeature.ps1 '%WebAppUrl%' '%IsRemove%'
powershell {Set-ExecutionPolicy Restricted}
pause
No comments:
Post a Comment