You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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-PnPTenantSite filter to include/exclude specific site templates (e.g., STS#0 for classic team sites, SITEPAGEPUBLISHING#0 for communication sites)
  • Search Query: Modify the Query parameter 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 SiteUrl containing 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-File if you need to track failures for later review.
  • Permissions: If using Global Admin rights, the -ReuseConnection flag will help reduce repeated login prompts during the bulk operation.

内容的提问来源于stack exchange,提问作者Kiran Kumar Talikoti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:13:12