我有一个powershell脚本,我从互联网上的某处创建了一个SharePoint文件夹。它的工作原理,因为它可以创建一个文件夹,当我测试它,但我想知道如何修改此脚本,以检查我正在创建的文件夹是否不存在?有人可以帮忙吗?
function CreateFolder {
param
(
$SPSite,
$SiteUrl,
$FolderName,
$User,
$Password
)
$ErrorActionPreference = "Stop"
$DocLibName = "Documents"
$FullSPPath = $SPSite+ $SiteUrl
#Connect Office 365 SharePoint Online Site
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SPSite)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)
$Context.Credentials = $Creds
$List = $Context.Web.Lists.GetByTitle($DocLibName)
$Context.Load($List)
#Get the List Root Folder
$ParentFolder=$Context.web.GetFolderByServerRelativeUrl($FullSPPath)
#Create New Folder
$Folder = $ParentFolder.Folders.Add($FolderName)
$ParentFolder.Context.Load($Folder)
$ParentFolder.Context.ExecuteQuery()
Write-host "New Folder Created Successfully!"
}
答案 0 :(得分:2)
您可以执行与获取root文件夹所做的相同的操作。因此,如果新文件夹在库的根目录中被称为NewFolder,它将如下所示:
$newFolder = $Context.Web.GetFolderByServerRelativeUrl("/sites/site/library/NewFolder")
$context.Load($newFolder)
$context.ExecuteQuery()
现在您需要做的就是检查Folder对象上的exists属性:
if (!$newFolder.Exists) {
#do stuff here
}