编写此PowerShell IF条件以测试URL是否存在于SharePoint列表中的正确方法是什么

l5tcr1uw  于 12个月前  发布在  Shell
关注(0)|答案(1)|浏览(109)

我在下面提供了一个脚本,它使用hubsites和相关站点填充SPO列表。我在底部附近写了一行,如果SiteUrl匹配,则应该阻止它写入更多行:

Connect-PnPOnline -Url "https://myCompany-admin.sharepoint.com" -Interactive
$Report = @()
Write-Host "Getting list of all hubsites..."
$HubSites = Get-PnPHubSite
Write-Host "Getting list of all sites excluding sites without a connected hub..."
$Sites = Get-PnPTenantSite | ? {$_.HubSiteId.Guid -ne "00000000-0000-0000-0000-000000000000"}
Write-Host "Getting the target SPO site to create items..."
$connection = Connect-PnPOnline -Url "https://myCompany.sharepoint.com/sites/HubSiteMap" -Interactive
$List = Get-PnPList "Sites" -Connection $connection
$ListItems = Get-PnPListItem -List $List

ForEach($Hub in $HubSites)
{
    # Get the ID of the Hubsite
    $HubSiteId = $Hub.ID.GUID
    # Iterate through each site collection
    ForEach($Site in $Sites)
    {
        # Get the Associated Hubsite ID for this site
        $AssociatedHubSiteID = $Site.HubSiteId.Guid
        If ( ($AssociatedHubSiteID -eq $HubSiteId) -and ($Hub.SiteUrl -ne $Site.Url) )
        {
            $ReportEntry = "" | Select-Object Hub,SiteUrl 
            $ReportEntry.Hub = $Hub.SiteUrl 
            $ReportEntry.SiteUrl = $Site.Url
            $Report += $ReportEntry        
            if ($Site.Url -notin $ListItems.FieldValuesAsText.SiteUrl) { #THIS LINE IS NOT WORKING!
                Add-PnPListItem -List $List -Values @{"Hub" = $Hub.SiteUrl; "SiteUrl" = $Site.Url}
            }
        }
    }
}

字符串
SPO列表有两个单行文本列HubSiteUrl

k2arahey

k2arahey1#

像这样使用PowerShell脚本:

Connect-PnPOnline -Url "https://myCompany-admin.sharepoint.com" -Interactive
$Report = @()
Write-Host "Getting list of all hubsites..."
$HubSites = Get-PnPHubSite
Write-Host "Getting list of all sites excluding sites without a connected hub..."
$Sites = Get-PnPTenantSite | ? {$_.HubSiteId.Guid -ne "00000000-0000-0000-0000-000000000000"}
Write-Host "Getting the target SPO site to create items..."
$connection = Connect-PnPOnline -Url "https://myCompany.sharepoint.com/sites/HubSiteMap" -Interactive
$List = Get-PnPList "Sites" -Connection $connection

ForEach($Hub in $HubSites)
{
    # Get the ID of the Hubsite
    $HubSiteId = $Hub.ID.GUID
    # Iterate through each site collection
    ForEach($Site in $Sites)
    {
        # Get the Associated Hubsite ID for this site
        $AssociatedHubSiteID = $Site.HubSiteId.Guid
        If ( ($AssociatedHubSiteID -eq $HubSiteId) -and ($Hub.SiteUrl -ne $Site.Url) )
        {
            $ReportEntry = "" | Select-Object Hub,SiteUrl 
            $ReportEntry.Hub = $Hub.SiteUrl 
            $ReportEntry.SiteUrl = $Site.Url
            $Report += $ReportEntry     

            # Check if list item with SiteUrl exists   
            $getListItem = Get-PnPListItem -List $List -Query "<View><Query><Where><Eq><FieldRef Name='SiteUrl'/><Value Type='Text'>$($Site.Url)</Value></Eq></Where></Query></View>"

            if($getListItem) {
                Write-Host "Site Url $($Site.Url) already exists in the list."
            }
            else {
                Add-PnPListItem -List $List -Values @{"Hub" = $Hub.SiteUrl; "SiteUrl" = $Site.Url}
            }
        }
    }
}

字符串
其中SiteUrl是列表中文本列的内部名称。您可以通过阅读本文获得列的确切内部名称:How to find the Internal name of columns in SharePoint Online?

相关问题