当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 检查网站集或子网站是否存在

作者:精品下载站 日期:2024-12-14 14:20:41 浏览:13 分类:玩电脑

SharePoint Online:使用 PowerShell 检查网站集或子网站是否存在


要求:使用 PowerShell 检查 SharePoint Online 中是否存在网站或网站集。

如何检查 SharePoint Online 中是否存在网站?

您是否曾经需要检查 SharePoint Online 中是否存在某个网站?也许您刚刚获得了潜在新网站的 URL,并且希望在开始创建之前确保该网站已创建,或者您正在编写 PowerShell 脚本并需要在继续之前验证该网站是否存在你的代码。无论哪种情况,都有几种方法可以检查站点是否存在。让我们来看看它们吧!

要确认 SharePoint Online 中是否存在网站集,您可以快速转到:

  1. SharePoint 管理中心位于 https://-admin.sharep 点。 com >> 展开站点 >> 活动站点。
  2. 在“搜索站点”中输入站点名称或URL,检查站点集是否存在。

    [玩转系统] SharePoint Online:使用 PowerShell 检查网站集或子网站是否存在

好吧,现在我将向您展示如何使用 PowerShell 检查 SharePoint Online 网站是否存在。

SharePoint Online PowerShell CSOM 检查给定 URL 中是否存在网站:

如何判断某个网站是否已存在,而无需从管理中心搜索所有网站? PowerShell 来救援!此 PowerShell 脚本检查给定 URL 中是否存在网站集:


#Set Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/suppliers"
 
#Connect to SharePoint Online
Connect-SPOService -Url $AdminCenterURL -Credential (Get-Credential)

#Get all site collections
$Sites = Get-SPOSite -Limit All | Select -ExpandProperty URL

#Check if any site with the URL exists already
If($Sites -notcontains $SiteURL)
{
    Write-Host "Site Collection URL '$SiteURL' is available!" -f Green
}
Else
{
    Write-Host "Site URL '$SiteURL' is not available!" -f Yellow
}

当您尝试自动化流程并需要在采取进一步操作之前确保站点存在时,这是一个方便的脚本。通过 CSOM 检查网站或网站集是否存在如何?


#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($SiteURL, $Credentials)
{
    #Setup context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    $Web = $Ctx.Web
    $Ctx.Load($web)
    
    Try {
            $Ctx.ExecuteQuery()
            Return $True
        }
    Catch [Exception] {
      Write-host $_.Exception.Message -f Red
      Return $False
     }        
}

#Variable for Site collection URL
$URL= "https://crescent.sharepoint.com/sites/sales/"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
$SiteExists = Check-SiteExists -SiteURL $URL -Credentials $Cred

#Call the function to check site exists
if($SiteExists -eq $True)
 {
    write-host "Given Site Collection Exists!" -f Green
    #Proceed with your script
 }
 else
 {
    write-host "Site Collection doesn't Exists on given URL!" -f Red
 }

请注意,如果出现任何网络相关问题或身份验证失败,函数 Check-SiteExists 可能会返回 false。

PnP PowerShell 检查该网站是否存在于 SharePoint Online

PowerShell 是一种功能强大的脚本语言,可用于自动执行 Microsoft SharePoint Online 中的任务。让我向您展示如何使用 PnP PowerShell 检查 SharePoint Online 中是否存在网站:


#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/procurement"

Try
{
    #Connect to Tenant Admin
    Connect-PnPOnline -URL $AdminCenterURL -Interactive
     
    #Check if site exists
    $Site = Get-PnPTenantSite | Where {$_.Url -eq $SiteURL}
    If ($Site -eq $null)
    {
        Write-host "Site Collection $($SiteURL) doesn't exists!" -foregroundcolor Yellow
    }
    Else
    {
        Write-host "Site $($SiteURL) exists already!" -foregroundcolor Green
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell 检查网站集中是否存在子网站


#Parameters
$SiteCollURL = "https://crescent.sharepoint.com/sites/Retail"
$SubSiteURL = "/sites/Retail/archived/2010" #ServerRelativeUrl

Try
{
    #Connect to Tenant Admin
    Connect-PnPOnline -URL $SiteCollURL -Interactive
     
    #Get All Webs from the site
    $Webs = Get-PnPSubWeb -Recurse -IncludeRootWeb

    #Check if site exists
    $Subsite = ($Webs | Select -ExpandProperty ServerRelativeUrl) -contains $SubSiteURL
    If ($Subsite -eq $false)
    {
        Write-host "SubSite $($SubSiteURL) doesn't exists!" -foregroundcolor Yellow
    }
    Else
    {
        Write-host "Site $($SubSiteURL) exists!" -foregroundcolor Green
    }
}
Catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

这对于自动化目的或验证站点是否处于活动状态(或已被删除!)非常有用。我的其他服务器端脚本,用于在本地 SharePoint 中使用 PowerShell 检查 SharePoint 中是否存在网站集或子网站。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯