使用PowerShell在SharePoint Online/Office 365批量创建自定义结果源
Hey Kiran, I’ve got you covered with a PowerShell solution using PnP PowerShell (the go-to tool for SharePoint Online automation) to bulk create custom result sources across your 100+ sites. Here’s a step-by-step breakdown and script tailored to your needs:
Prerequisites
- Install the PnP PowerShell module if you haven’t already:
Install-Module -Name PnP.PowerShell -Force -AllowClobber - Ensure you have Site Collection Administrator permissions on all target sites (or Global Admin rights for broader access to all sites in your tenant)
- Have your tenant admin URL ready (e.g.,
https://yourcompany-admin.sharepoint.com)
Bulk Result Source Creation Script
# Connect to your SharePoint Online Admin Center Connect-PnPOnline -Url "https://yourtenant-admin.sharepoint.com" -Interactive # Option 1: Fetch all modern sites (customize the filter to target your specific sites) $sites = Get-PnPTenantSite -Template GROUP#0, STS#3 | Select-Object -ExpandProperty Url # Option 2: Load sites from a CSV file (uncomment below and comment Option 1 if using this) # $sites = Import-Csv -Path "C:\Your\Path\To\Sites.csv" | Select-Object -ExpandProperty SiteUrl # Define your custom result source settings (adjust these to match your requirements) $resultSourceConfig = @{ Name = "Custom Internal Document Search" Description = "Searches only internal document libraries across our organization" Query = "contentclass:STS_ListItem_DocumentLibrary AND IsDocument:1 AND Path:https://yourtenant.sharepoint.com/sites/*" ProviderId = "b09a7990-05ea-4af9-81ef-edfab16c4e31" # Default SharePoint search provider ID ResultSourceType = "Microsoft.Search.Internal" } # Loop through each site to create the result source foreach ($siteUrl in $sites) { try { Write-Host "Processing site: $siteUrl" -ForegroundColor Cyan # Reuse existing connection to avoid repeated login prompts Connect-PnPOnline -Url $siteUrl -Interactive -ReuseConnection # Check if the result source already exists to avoid duplicates $existingSource = Get-PnPResultSource -Name $resultSourceConfig.Name -ErrorAction SilentlyContinue if ($existingSource) { Write-Host "Result source '$($resultSourceConfig.Name)' already exists in $siteUrl. Skipping." -ForegroundColor Yellow } else { # Create the custom result source New-PnPResultSource @resultSourceConfig Write-Host "Successfully created result source in $siteUrl!" -ForegroundColor Green } } catch { Write-Host "Failed to process $siteUrl. Error: $_" -ForegroundColor Red } } Write-Host "Bulk result source creation process completed!" -ForegroundColor Green
Key Customization Tips
- Site Targeting: Adjust the
Get-PnPTenantSitefilter to include/exclude specific site templates (e.g.,STS#0for classic team sites,SITEPAGEPUBLISHING#0for communication sites) - Search Query: Modify the
Queryparameter using SharePoint’s KQL (Keyword Query Language) to match your desired search results. Test your query first in the SharePoint Search Query Tool to ensure accuracy. - Provider ID: Use the default SharePoint provider ID unless you need to connect to an external search provider (like Bing) — you can find alternate provider IDs in SharePoint’s search settings.
- CSV Option: If you have a predefined list of sites, use the CSV method: create a CSV file with a column named
SiteUrlcontaining each site’s full URL.
Important Notes
- Test First: Run the script on a small subset of sites before scaling to all 100+ to verify the result source works as expected.
- Error Logging: Extend the script to log errors to a file using
Out-Fileif you need to track failures for later review. - Permissions: If using Global Admin rights, the
-ReuseConnectionflag will help reduce repeated login prompts during the bulk operation.
内容的提问来源于stack exchange,提问作者Kiran Kumar Talikoti
相关产品推荐
相关产品推荐

