PowerShell上传整个目录下的文件到SharePoint的二级目录 2021-07-14 Website News 暂无评论 9870 次阅读 通过PowerShell上传整个目录下文件到Sharepoint的二级以上目录。 ###方法一: ```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) $Context.Load($List.RootFolder) $Context.ExecuteQuery() $TargetFolder = $Context.Web.GetFolderByServerRelativeUrl($List.RootFolder.ServerRelativeUrl + "/" + $FolderName); #Upload file(s) Foreach ($File in (dir $Folder -File)) { $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $true $FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName) $FileCreationInfo.URL = $File.Name $UploadFile = $TargetFolder.Files.Add($FileCreationInfo) $Context.Load($UploadFile) $Context.ExecuteQuery() } ``` ###方法二: ```csharp #Specify tenant admin and site URL $User = "admin@contoso.no" $SiteURL = "https://contoso.sharepoint.com" $Folder = "E:\LocalFolder" $DocLibName = "Libraryname" $FolderName = "Folder/SubFolder" #Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM 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" $Password = "password" | ConvertTo-SecureString -AsPlainText -Force #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) $Context.Load($List.RootFolder) $Context.ExecuteQuery() #Upload file(s) Foreach ($File in (dir $Folder -File)) { $FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation $FileCreationInfo.Overwrite = $true $FileCreationInfo.Content = [System.IO.File]::ReadAllBytes($File.FullName) $FileCreationInfo.URL = $List.RootFolder.ServerRelativeUrl + "/" + $FolderName + "/" + $File.Name $UploadFile = $List.RootFolder.Files.Add($FileCreationInfo) $Context.Load($UploadFile) $Context.ExecuteQuery() } ``` 标签: PowerShell, Sharepoint, Subfolder, Second SubFolder, Second 本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
评论已关闭