PowerShell上传整个目录下的文件到SharePoint的二级目录(文件大于2M) 2021-08-03 Website News 暂无评论 5386 次阅读 之前的脚本上传大于2M的文件会失败,需要使用FileStream函数来进行上传。 ```csharp [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 #Specify tenant admin and site URL $User = "jz@one.com" $Password = "password@2022" | ConvertTo-SecureString -AsPlainText -Force $SiteURL = "https://one.sharepoint.com/sites/Test/" $Folder = "D:\Test\files\" $DocLibName = "ERetail" $FolderName = "OldReport/report(SN)" #Add references to SharePoint client assemblies Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.Client.Runtime\v4.0_16.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.Client.Runtime.dll" #Bind to site collection $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password) $Context.Credentials = $Creds #Retrieve list $List = $Context.Web.Lists.GetByTitle($DocLibName) #$List = $Context.Web.Lists.GetByTitle("Certificates") $Context.Load($List.RootFolder) $Context.ExecuteQuery() $TargetFolder = $Context.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + "/" + $FolderName); #Upload file(s) Foreach ($File in (dir $Folder -File)) { $FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open) $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $true $FileCreationInfo.ContentStream = $FileStream $FileCreationInfo.URL = $File.Name $UploadFile = $TargetFolder.Files.Add($FileCreationInfo) $Context.Load($UploadFile) $Context.ExecuteQuery() } ``` 标签: PowerShell, Sharepoint, Subfolder, 二级目录, FileStream 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭