Create and Remove Search Scope of Web Address Type

09/01/2012 13:01

#input parameters
$scopeUrl= $server + "/sites" + $site+"/CategoryCards/Pages"
#write-host $scopeUrl
$scopeName="category cards"
$scopeDescription="Category Card library"
$searchAppName="Enterprise Search Service Application"
 # check to ensure Microsoft.SharePoint.PowerShell is loaded
    $snapin=Get-PSSnapin | Where-Object {
       $_.Name -eq 'Microsoft.SharePoint.Powershell'}
       if ($snapin -eq $null) {
       Write-Host "Loading SharePoint Powershell Snapin"
       Add-PSSnapin "Microsoft.SharePoint.Powershell"
$searchapp = Get-SPEnterpriseSearchServiceApplication $searchAppName
$category = Get-SPEnterpriseSearchMetadataCategory –Identity SharePoint -SearchApplication $searchapp
#write-host $category

#if scope exists, delete and create new
$scope = Get-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $searchapp -ea 0
if($scope -eq $null) #If scope doesn't exis, create
    write-host $scopeName " scope doesn't exist in " $searchAppName ", creating..."
    $searchapp | New-SPEnterpriseSearchQueryScope -Name $scopeName -Description $scopeDescription -DisplayInAdminUI $true
    $scope = Get-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $searchapp
    $scopeRule = New-SPEnterpriseSearchQueryScopeRule  -RuleType Url -MatchingString $scopeUrl -UrlScopeRuleType "Folder" -FilterBehavior "Include" -Url $server -Scope $scope
     write-host $scopeName " scope has been successfuly created in " $searchAppName "."
else #if scope exists, delete
    write-host $scopeName " scope already exist in " $searchAppName ", deleting..."
    Remove-SPEnterpriseSearchQueryScope -Identity $scopeName -SearchApplication $searchapp -Confirm:$false
    write-host $scopeName " scope has been successfuly removed from " $searchAppName "."