Creating a Content Source Scope Rule using PowerShell

Posted Wednesday, August 11, 2010 1:33 PM by CoreyRoth

Two weeks ago, I posted about how to create scopes and scope rules using PowerShell.  At the end of the post I mentioned that there was no rule type for creating scope rules by content type.  I knew there had to be a way to do this, so I created a scope rule using the UI and then I used the Get-SPEnterpriseSearchQueryScopeRule cmdlet to return all the scope rules on my content source.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$scope = Get-SPEnterpriseSearchQueryScope -Identity "File Share" -SearchApplication $searchapp
Get-SPEnterpriseSearchQueryScopeRule -Scope $scope

EnterpriseSearchPowerShellGetScopeRulesContentSource

From this result, the answer was immediately obvious to me.  ContentSource is a managed property and we already know how to create scope rules like that using the PropertyQuery rule type.  Remember how we can query by content source?  Let’s give it a try and see what happens.  I am going to create a new scope and have it use my BCS content source called Products.  Here is what the syntax looks like.

$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$scope = New-SPEnterpriseSearchQueryScope -Name "My Scope" -Description "My scope created in PowerShell" -SearchApplication $searchapp -DisplayInAdminUI $true
New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty ContentSource -PropertyValue Products -FilterBehavior Include -url http://sp2010 -scope $scope -SearchApplication $searchapp

I’ll only mention the relevant parameters today.  If you are curious about the other ones, see the previous post where I go into detail on each one.  On the New-SPEnterpriseSearchQueryScopeRule method, we set the RuleType parameter to ContentSource.  Then, we set the PropertyValue to the name of the content source.  In my case, I set it to Products.  Executing the script above, I get the following result.

EnterpriseSearchPowerShellNewScopeRuleContentSource

It appears to have worked, but let’s verify it in the UI.

EnterpriseSearchPowerShellNewScopeRuleContentSourceUI

Excellent.  The new scope was created with the content source rule and it has 43 items included.  Now, I can pretty much script all of my search settings in PowerShell.  Once you have your scopes created, if you want to create scope display groups be sure and check out the SharePoint PowerShell Community Toolkit.

Comments

# Twitter Trackbacks for Creating a Content Source Scope Rule using PowerShell - Corey Roth - DotNetMafia.com - Tip of the Day [dotnetmafia.com] on Topsy.com

Pingback from  Twitter Trackbacks for                 Creating a Content Source Scope Rule using PowerShell - Corey Roth - DotNetMafia.com - Tip of the Day         [dotnetmafia.com]        on Topsy.com

# re: Creating a Content Source Scope Rule using PowerShell

Thursday, August 12, 2010 2:40 PM by Steve

Cory,

Great article!  With the information above I was able to add a new scope for a content source but I can't seem to get a subset of results back for just the specified content source/scope.  Instead I get results back from all content sources.  It is like it is ignoring my scope when I select it next to the search box in the Fast Search Center site.

I have enabled the scope dropdown in the UI and it shows my new scope.  I have verified that ContentSource is a Managed Property/Crawled Property (FAST SearchAdmin >> Managed Properties).  Any thoughts?  Here is the commands I used to create my scope and rule:

$searchapp = Get-SPEnterpriseSearchServiceApplication "FASTQuerySSA"

$scope = New-SPEnterpriseSearchQueryScope -Name "ContentSrcScope" -Description "A scope for a single content source" -SearchApplication $searchapp -DisplayInAdminUI $true

New-SPEnterpriseSearchQueryScopeRule -RuleType PropertyQuery -ManagedProperty ContentSource -PropertyValue "ContentSrc" -FilterBehavior Include -url http://sp2010.domain.com -scope $scope -SearchApplication $searchapp

# re: Creating a Content Source Scope Rule using PowerShell

Thursday, August 12, 2010 3:01 PM by CoreyRoth

So your content source is called ContentSrc correct? What does it look like in the UI, does the scope rule show created.  Is there an error next to the scope?  If so, does it go away after compiling the scope rules?  I haven't tested my script in FAST yet, but it should be the same.  I'll try it when I get a chance.

# re: Creating a Content Source Scope Rule using PowerShell

Thursday, August 12, 2010 3:10 PM by Steve

Update:  Even though I can see the new scope in my scopes drop down, when I run the command Get-SPEnterpriseSearchQueryScope the CompilationState is still QueryExpanded and it shows that theLastCompilationTime is 1/1/0001.  Meaning it hasn't been compiled yet.  Could this be my problem?  If so how do you force a compile?

# re: Creating a Content Source Scope Rule using PowerShell

Thursday, August 12, 2010 3:18 PM by CoreyRoth

@Steve You can compile it through the UI.  Look for the start compilation link in your service application.  You can also download the SharePoint PowerShell Community Toolkit and execute the command in PowerShell.

www.dotnetmafia.com/.../introducing-the-sharepoint-powershell-community-toolkit.aspx

# re: Creating a Content Source Scope Rule using PowerShell

Thursday, August 12, 2010 3:25 PM by Steve

To answer your questions, yes my content source is called ContentSrc (I made it generic for this post).  There is no error next to the scope in Central Admin.  Update Status shows Ready but Items shows 0 (zero).  

When I run the command Get-SPEnterpriseSearchQueryScope the CompilationState is QueryExpanded.  So, I am guessing it is not compiled and therefore will not return any results.  Correct?

So how do you compile a scope?  I have been looking for the last hour or so and can't find anything.

# re: Creating a Content Source Scope Rule using PowerShell

Thursday, August 12, 2010 3:44 PM by Steve

I don't have the option to compile the scope from my FASTContentSSA.  When I select the drop down for my scope I do not see an option to compile.  All I see is Edit Properties and Rules.., Make Copy as Shared, and Delete.  I wonder if this is due to FAST and you have to compile a different way?  

Also, I see on the command New-SPEnterpriseSearchQueryScope there is an option called -CompilationType but the docs out on TechNet don't give you any clue as to what type of data that parameter expects.  I am going to look at that more.  I will also take a look at the toolkit.

# re: Creating a Content Source Scope Rule using PowerShell

Tuesday, November 30, 2010 3:06 PM by Lee C.

Cory,

Thanks for the helpful info!  You got me really close, bu there is one thing preventing me from scripting all of my Search Server crawling setup.  I'm hoping you can help me to conquer the remaining item.

I cannot figure out how to script Crawl Rules that use forms authentication (FormRuleAccess).  How can I supply the Form URL, username, and password via PowerShell script?

# re: Creating a Content Source Scope Rule using PowerShell

Monday, December 6, 2010 2:53 PM by CoreyRoth

@Lee that's a good question.  I suspect, you will have to create the crawl rule and get an object back and then look in the API to see how to set the rest of those properties.  I'll try to take a look when I get a chance.

# re: Creating a Content Source Scope Rule using PowerShell

Tuesday, December 7, 2010 10:02 AM by Lee C.

If I could find it in the API documentation I would not have asked for help.  :]  This is the only API doc I have found:

download.microsoft.com/.../SharePtServTechRef.doc

Do you know of a better one?

# re: Creating a Content Source Scope Rule using PowerShell

Wednesday, December 8, 2010 9:10 AM by CoreyRoth

@Lee Have you looked at the details of Net-SPEnterpriseSearchCrawlRule?

technet.microsoft.com/.../ff608119.aspx

# re: Creating a Content Source Scope Rule using PowerShell

Friday, December 10, 2010 6:04 AM by Gareth M

Thanks for this great article Corey, saved me lots of time.  

The only thing I would add is to do a Scope Update from the CA: Search Service app main screen, so that the scope can be used straightaway rather than waiting for the schedule to kick in.

# FAST Search Scopes « Sladescross's Blog

Saturday, June 18, 2011 2:20 AM by FAST Search Scopes « Sladescross's Blog

Pingback from  FAST Search Scopes « Sladescross's Blog

Leave a Comment

(required)
(required)
(optional)
(required)