Powershell。在 SharePoint 365 中创建子文件夹

Powershell。在 SharePoint 365 中创建子文件夹

我有以下脚本,可在“Documents”列表下创建一个名为昨天日期的文件夹。但是,我需要在另一个名为“FinancialReports”的子文件夹下创建此文件夹。有人能帮我吗?

$FolderName = ((get-date).adddays(-1)).tostring('yyyy-MM-dd') 
$User = "[email protected]"  
$Password = "Password321"
$SiteURL = "https://mydomain.sharepoint.com/sites/Accounts/"
$LibraryName = "Documents"

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, (ConvertTo-SecureString $Password -AsPlainText -Force))  
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get the Library by Name
$List = $Ctx.Web.Lists.GetByTitle($LibraryName)

#sharepoint online create folder powershell
    $NewFolder = $List.RootFolder.Folders.Add($FolderName)
    $Ctx.ExecuteQuery()

答案1

这应该非常简单,添加路径到FinancialReports

    $FolderName = ((get-date).adddays(-1)).tostring('yyyy-MM-dd') 
    $User = "[email protected]"  
    $Password = "Password321"
    $SiteURL = "https://mydomain.sharepoint.com/sites/Accounts/"
    $LibraryName = "Documents"
# Add this
    $FinancialReports="c:\path\to\financial\reports\"

替换$NewFolder

#sharepoint online create folder powershell
    $NewFolder = $List.RootFolder.Folders.Add("$(FinancialReports)\$(FolderName)")
    $Ctx.ExecuteQuery()

你应该可以走了

干杯

答案2

所以我最终使用了 PNP,这使得事情变得容易多了。

使用一个命令添加 PNP

“安装模块 SharePointPnPPowerShellOnline”

以下是指南: https://docs.microsoft.com/en-us/powershell/sharepoint/sharepoint-pnp/sharepoint-pnp-cmdlets?view=sharepoint-ps

我完成的代码现在是:

$FolderName = ((get-date).adddays(-1)).tostring('yyyy_MM_dd') 
$User = "[email protected]"  
$Password = "Password321"
$SiteURL = "https://mydomain.sharepoint.com/sites/Accounts/"

$spfolder = "shared documents/FinancialReporting/$FolderName"

$sp = $password | ConvertTo-SecureString -AsPlainText -Force
$Cred = New-Object system.management.automation.pscredential -ArgumentList $user, $sp

Connect-PnPOnline -Url $SiteUrl -Credentials $Cred  
# the default "Documents" list = "shared documents" for some reason...
Add-PnPFolder -name $FolderName -folder "shared documents/FinancialReporting" 

相关内容