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

[玩转系统] SharePoint Online:使用 PowerShell 检查文件夹是否存在

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

SharePoint Online:使用 PowerShell 检查文件夹是否存在


要求:检查 SharePoint Online 文档库中是否存在文件夹。

[玩转系统] SharePoint Online:使用 PowerShell 检查文件夹是否存在

SharePoint Online:PowerShell 检查文件夹是否存在

使用 SharePoint Online PowerShell 脚本时,您可能需要在创建新文件夹或删除现有文件夹之前确定 SharePoint Online 中是否存在文件夹。在这篇博文中,我们将向您展示如何使用 PowerShell 快速检查 SharePoint Online 中是否存在文件夹!

以下是用于 SharePoint Online 检查文件夹是否存在的 PowerShell:


#Load SharePoint CSOM 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"

#Function to check if folder Exists
Function Check-SPOFolderExists()
{
  param
    (
        [Parameter(Mandatory=$true)] [string] $SiteURL,
        [Parameter(Mandatory=$true)] [string] $FolderRelativeURL
    )
    Try {
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials

        #Get the Web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.ExecuteQuery()

        #Check Folder Exists
        Try { 
            $Folder = $Web.GetFolderByServerRelativeUrl($FolderRelativeURL)
            $Ctx.Load($Folder)
            $Ctx.ExecuteQuery() 

            Write-host -f Green "Folder Exists!"
        }
        Catch {
            Write-host -f Yellow "Folder Doesn't Exist!"
        }        
    }
    Catch {
        write-host -f Red "Error Checking Folder Exists!" $_.Exception.Message
    }
}
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/"
$FolderRelativeURL="/Shared Documents/2018"

#Call the function 
Check-SPOFolderExists -SiteURL $SiteURL -FolderRelativeURL $FolderRelativeURL 

或者,您可以使用我的另一篇文章中使用的其他方法:SharePoint Online:使用 PowerShell 创建文件夹

PnP PowerShell 检查 SharePoint Online 中是否存在文件夹

检查给定位置中是否存在文件夹的 PnP PowerShell 方法如下:


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$FolderURL = "/sites/Operations/Shared Documents/Classifieds" #Server Relative URL

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Try to Get the Folder 
$Folder = Get-PnPFolder -Url $FolderURL -ErrorAction SilentlyContinue

#sharepoint online powershell To check if folder exists
If($Folder -ne $null)
{
    Write-Host -f Green "Folder exists!"
}
Else
{
    Write-Host -f Yellow "Folder does not exists!"
}

好的,上面的两个脚本检查指定的 URL 中是否存在给定的文件夹。如何检查某个文件夹在 SharePoint Online 中是否具有特定子文件夹?


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$ParentFolderURL = "/Shared Documents" #Site Relative URL
$FolderName = "Classified"

Try{
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Try to Get the Folder from given parent folder
    $Folder = Get-PnPFolderItem -FolderSiteRelativeUrl $ParentFolderURL -ItemType Folder -ItemName $FolderName

    #Powershell To check if folder exists
    If($Folder -ne $null)
    {
        Write-Host -f Green "Folder exists!"
    }
    Else
    {
        Write-Host -f Yellow "Folder does not exists!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

如何使用 PowerShell 检查文档库是否有特定文件夹?

下面的脚本检查给定的文档库是否有特定的文件夹(在其根级别)。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Operations"
$LibraryName = "Documents"
$FolderName = "Classified"

Try{
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Try to Get the Folder from document library
    $Folder = Get-PnPFolder -List $ListName | Select -ExpandProperty Name | Where {$_.Name -eq $FolderName}

    # check if folder exists in the document library - ONLY AT ROOT LEVEL
    If($Folder -ne $null)
    {
        Write-Host -f Green "Folder exists!"
    }
    Else
    {
        Write-Host -f Yellow "Folder does not exists!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

本文展示了我们可以使用 PowerShell 来确定 SharePoint Online 中是否存在文件夹。使用这些步骤,您可以在创建文件夹之前轻松验证文件夹是否已存在于文档库或列表中,或者验证特定文件夹是否已被删除。

如果您想创建一个不存在的新文件夹怎么办?要使用 PowerShell 在 SharePoint Online 中创建文件夹,请参阅:如何使用 PowerShell 在 SharePoint Online 中创建新文件夹?

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

取消回复欢迎 发表评论:

关灯