ค้นหาเว็บไซต์

SharePoint Online: ตรวจสอบว่าไซต์คอลเลกชันหรือไซต์ย่อยมีอยู่โดยใช้ PowerShell


ความต้องการ: ตรวจสอบว่ามีไซต์หรือไซต์คอลเลกชันอยู่ใน SharePoint Online โดยใช้ PowerShell หรือไม่

จะตรวจสอบได้อย่างไรว่ามีไซต์อยู่ใน SharePoint Online หรือไม่

คุณเคยจำเป็นต้องตรวจสอบว่ามีไซต์อยู่ใน SharePoint Online หรือไม่? บางทีคุณอาจเพิ่งได้รับ URL สำหรับไซต์ใหม่ที่เป็นไปได้ และคุณต้องการตรวจสอบให้แน่ใจว่ามีการสร้างไซต์นั้นแล้วก่อนที่จะเริ่มสร้าง หรือคุณกำลังเขียนสคริปต์ PowerShell และจำเป็นต้องตรวจสอบว่าไซต์นั้นมีอยู่ก่อนดำเนินการต่อ รหัสของคุณ ไม่ว่าในกรณีใด มีหลายวิธีในการตรวจสอบว่ามีไซต์อยู่หรือไม่ มาดูพวกเขากันดีกว่า!

เพื่อยืนยันว่ามีไซต์คอลเลกชันอยู่ใน SharePoint Online หรือไม่ คุณสามารถไปที่:

  1. ศูนย์การจัดการ SharePoint ที่ https://-admin.sharepoint com >> ขยายไซต์ >> ไซต์ที่ใช้งานอยู่
  2. ป้อนชื่อไซต์หรือ URL ใน "ค้นหาไซต์" เพื่อตรวจสอบว่าไซต์คอลเลกชันมีอยู่หรือไม่

เอาล่ะ ตอนนี้ฉันจะแสดงวิธีใช้ 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\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\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 อาจส่งคืนค่าเท็จในกรณีที่เกิดปัญหาเกี่ยวกับเครือข่ายหรือการตรวจสอบสิทธิ์ล้มเหลว

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 ภายในองค์กรหรือไม่

บทความที่เกี่ยวข้อง