Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurstX/d8e449a5162451d9022f1c3a24087497 to your computer and use it in GitHub Desktop.
Save BurstX/d8e449a5162451d9022f1c3a24087497 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
! UPDATE THE VARIABLES IN THE BEGINNING
OF THE SCRIPT ACCORDING TO YOUR ENVIRONMENT
x
The script sets up the environment to reproduce the problem
stated in https://social.msdn.microsoft.com/Forums/en-US/7c93cb0c-d500-477b-b9fa-83f604299a28/expansion-of-wildcard-will-possibly-exceed-10000-terms?forum=sharepointsearch
The script creates:
1) a file share on the local drive with a tree of folders
2) a content source to crawl this file share
3) a crawl rule to crawl the file share on behalf of a specific user
(maybe the crawl rule is not critical for the purpose of this test,
but it's required in our environment)
#>
Add-PSSnapin Microsoft.SharePoint.PowerShell;
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# PARAMETER VARIABLES TO UPDATE ACCORDING TO YOUR ENVIRONMENT
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# SP Search Application name
$searchAppName = 'D26_SearchServiceApplication';
# letter of the disk where the file share folder will be stored
$fileShareDisk = 'C:';
# name of the folder which will be maped as the file share root
$fileShareName = 'smb_apps';
# Amount of folders (and files, one in each folder) to create.
# Wildcard search returns results for $foldersCount=100,
# but fails for $foldersCount=11000.
$foldersCount = 20*1000;
# FQDN of the current machine the script is run on.
# E.g. the test IS NOT valid for 'localhost' (wildcard search DOES NOT work),
# but IS valid for 4-part FQDN like 'abc.def.xyz.com'
# (wildcard search DOES work) (actual FQDN on the machines in
# our corporate environment are 4-part)
$machineFQDN = 'AlphanumericMachineName.abc.xyz.com';
# SP Search content source name to create for the file share
$contentSourceName = 'FileShareWildcardSearchTest';
# switch defining if a crawl rule must be created to crawl the file share
# (may not be needed; but in our environment we use a crawl rule; if your
# SP Search crawls the file share with no errors you may set $createCrawlRule = $false)
$createCrawlRule = $true;
# crawl rule account name
# (may not be needed; but in our environment we use a crawl rule and
# therefore we need this 'reader' account to be used in the crawl rule configuration)
$crawlRuleAccountName = 'domain\username'; # TODO: enter the value 'domain\username'
# crawl rule account password
# (may not be needed; but in our environment we use a crawl rule and
# therefore we need a 'reader' account to be used in the crawl rule configuration)
$crawlRuleAccountPassword = ''; # TODO: enter the plain text password
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# END OF VARIABLE LIST
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# variables whose values are calculated based on the vars in the beginning of the script (above)
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# disk path to the folder which will be maped as the file share root
$fileShareRootDiskPath = "$fileShareDisk\$fileShareName";
# relative path to the base folder where all the test subfolders and files will be created
$fileShareContentsFolderPath = "$fileShareName\no_bkp\TEST_archived_SP-Data";
# absolute path to the base folder where all the test subfolders and files will be created
$fileShareContentsFolderDiskPath = "$fileShareDisk\$fileShareContentsFolderPath";
# network path to the file share (used in the crawl rule, in the content source definition)
$fileShareNetworkPath = "file://$machineFQDN/$($fileShareContentsFolderPath.Replace('\', '/'))";
# network path to the file share used in the test KQL query
# (used just for output purposes, not to configure the environment)
$fileShareNetworkPathForQuery = $fileShareNetworkPath.TrimStart('file:').TrimEnd('/').Replace('/', '\');
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# END OF variables whose values are calculated based on the vars in the beginning of the script
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# create the folder with the subfolder tree
if(!(Test-Path -Path $fileShareContentsFolderDiskPath))
{
for($i = 1; $i -le $foldersCount; $i++)
{
$newGuid = [Guid]::NewGuid().ToString().Replace('-', '').Replace('{', '').Replace('}', '').toLower();
$folder = New-Item -ItemType Directory -Force -Path "$fileShareContentsFolderDiskPath\$($newGuid)_000\xyz.server-shmerver.abc.com(443)\engagements\$i"
fsutil file createnew "`"$($folder.FullName)\$i.docx`"" 1024
}
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# create a file share with the created tree
$share = Get-SmbShare -Name $fileShareName -ErrorAction SilentlyContinue
if($share -eq $null)
{
<#
New-SMBShare –Name "WildcardSearchTestFileShare" –Path $fileShareFolderDiskPath `
-ContinuouslyAvailable $True `
–FullAccess Everyone `
-ChangeAccess Everyone `
-ReadAccess Everyone
#>
New-SMBShare –Name $fileShareName –Path $fileShareRootDiskPath
#Get-SmbShare 'WildcardSearchTestFileShare' | Add-AccessControlEntry -Principal Everyone -LogicalShareRights FullControl
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# create a content source
$ssa = Get-SPEnterpriseSearchServiceApplication $searchAppName
$cs = Get-SPEnterpriseSearchCrawlContentSource $contentSourceName -SearchApplication $ssa -ErrorAction SilentlyContinue
if($cs -eq $null)
{
$cs = New-SPEnterpriseSearchCrawlContentSource -SearchApplication $ssa -Type File -name $contentSourceName -StartAddresses $fileShareNetworkPath
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# create a crawl rule to read the file share on behalf of a particular user
if($createCrawlRule)
{
$existingRules = Get-SPEnterpriseSearchCrawlRule -SearchApplication $ssa
$ruleFound = $false;
foreach($rule in $existingRules)
{
if($rule.Path -eq "$fileShareNetworkPath/*")
{
$ruleFound = $true;
break;
}
}
if(!$ruleFound)
{
$crawlRule = New-SPEnterpriseSearchCrawlRule -SearchApplication $ssa `
-Path "$fileShareNetworkPath/*" `
-Type InclusionRule `
-AuthenticationType BasicAccountRuleAccess `
-AccountName $crawlRuleAccountName `
-AccountPassword $(ConvertTo-SecureString $crawlRuleAccountPassword -AsPlainText -Force)
}
}
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# start a full crawl on the newly created content source
if($cs.CrawlState -eq "Idle") {
Write-Host "CrawlState was Idle, crawling Full" -ForegroundColor Green
$cs.StartFullCrawl()
Do {
Write-Host "`r#" -ForegroundColor Yellow -NoNewline
Start-Sleep 5
} while ($cs.CrawlState -ne "Idle")
Write-Host "`nFinsihed crawling" -ForegroundColor Green
} else{
Write-Host "Didn't start crawl, CrawlState was "$cs.CrawlState -ForegroundColor Yellow
}
write-host '! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !'
write-host 'Ensure that the full crawl finished for the newly created Content Source ' -NoNewline
write-host "$contentSourceName'" -ForegroundColor Yellow
write-host
write-host 'and AFTER THE FULL CRAWL (and success values in the crawl log) try the KQL query:' -ForegroundColor Cyan
Write-Host "`n`t Path:`"$fileShareNetworkPathForQuery\*\xyz.server-shmerver.abc.com(443)\engagements`""
write-host
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment