ตรวจสอบว่าคอลเลกชันไซต์ SharePoint ไซต์ รายการ เอกสาร คอลัมน์มีอยู่ใน PowerShell หรือไม่
ต่อไปนี้เป็นสคริปต์ PowerShell ที่ดีของฉันเพื่อตรวจสอบว่า URL ที่กำหนดของวัตถุคอลเลกชันไซต์ ไซต์ รายการ เอกสาร (ไฟล์) และคอลัมน์มีอยู่ใน SharePoint หรือไม่ คุณสามารถใช้ฟังก์ชันตัวช่วยเหล่านี้ได้ทุกที่ที่จำเป็น
PowerShell เพื่อตรวจสอบว่ามีไซต์คอลเลกชันอยู่หรือไม่:
นี่คือฟังก์ชันที่กำหนดเองและการใช้งานฟังก์ชันกับ PowerShell เพื่อตรวจสอบว่าไซต์คอลเลกชันมีอยู่หรือไม่ SharePoint PowerShell เพื่อตรวจสอบว่าไซต์คอลเลกชันมีอยู่หรือไม่:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom Function to Check if Site Collection Exists in Given URL
Function Check-SiteExists($SiteURL)
{
return (Get-SPSite $SiteURL -ErrorAction SilentlyContinue) -ne $null
}
#Variable for Site collection
$SiteURL= "https://demo.crescent.com/sites/operations"
#Call the function to check site exists
if(Check-SiteExists($SiteURL))
{
write-host "Yes, Given Site Collection Exists!" -f Green
#Proceed with your script
}
else
{
write-host "No, Site Collection doesn't Exists on given URL!" -f Red
}
หรือคุณสามารถลอง:
$SiteURL = "https://portal.crescent.com/"
Try{
$Site=Get-SPSite $SiteURL -ErrorAction SilentlyContinue
}
catch{
write-host Site Collection with URL:$SiteURL Does not Exists!
return
}
สคริปต์ PowerShell เพื่อตรวจสอบว่ามีไซต์ย่อย SharePoint อยู่ใน URL ที่ระบุหรือไม่:
#Custom Function to Check subsite(Web) Exists or Not
Function Check-WebExists($WebURL)
{
return (Get-SPWeb $WebURL -ErrorAction SilentlyContinue) -ne $null
}
SharePoint PowerShell เพื่อตรวจสอบว่ามีรายการอยู่หรือไม่:
#Function to Check if List Exists
Function Check-ListExists()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ListName )
Return (Get-SPWeb $SiteURL).lists.TryGetList($ListName) -ne $null
}
#Variable for Site collection
$varSiteURL= "https://intranet.crescent.com/sites/Sales"
$varListName = "Invoice"
#Call the function to check list exists
if(Check-ListExists -SiteURL $varSiteURL -ListName $varListName)
{
write-host "Yes, Given List do Exists!"
#Proceed with your script
}
ตรวจสอบว่ามีคอลัมน์ไซต์อยู่แล้วโดยใช้ PowerShell
ตรวจสอบว่าคอลัมน์ของไซต์มีอยู่โดยใช้ PowerShell หรือไม่:
#Function to Check if File Exists
Function Check-SiteColumnExists()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$ColumnName )
Return (Get-SPWeb $SiteURL).Fields.ContainsField($ColumnName)
}
#Variable for Site collection
$varSiteURL= "https://intranet.crescent.com"
$varColumnName = "Department"
#Call the function to check list exists
if(Check-SiteColumnExists -SiteURL $varSiteURL -ColumnName $varColumnName)
{
write-host "Yes, Given Site Column do Exists!"
#Proceed with your script
}
มีเอกสารตรวจสอบ SharePoint PowerShell อยู่
และสุดท้าย เรามาตรวจสอบไฟล์ที่มีอยู่ในตำแหน่งที่กำหนดกัน:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Function to Check if File Exists
Function Check-FileExists()
{
Param( [Parameter(Mandatory=$true)] [string]$SiteURL, [Parameter(Mandatory=$true)] [string]$FileURL )
Return (Get-SPWeb $SiteURL).GetFile($FileURL).Exists
}
#Variable for Site collection
$varSiteURL= "https://intranet.crescent.com"
$varFileURL = "https://intranet.crescent.com/Sales/Invoices/Inv-5060203.docx"
#Call the function to check list exists
if(Check-FileExists -SiteURL $varSiteURL -FileURL $varFileURL)
{
write-host "Yes, Given File does Exist!"
#Proceed with your script
}
