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

[玩转系统] 检查 PowerShell 中是否存在 SharePoint 网站集、网站、列表、文档、列

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

检查 PowerShell 中是否存在 SharePoint 网站集、网站、列表、文档、列


以下是我的漂亮 PowerShell 脚本,用于检查 SharePoint 中是否存在网站集、网站、列表、文档(文件)和列对象的给定 URL。您可以在需要时使用这些辅助函数。

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 中是否存在 SharePoint 子网站的 PowerShell 脚本:


#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
}

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

取消回复欢迎 发表评论:

关灯