如何使用 Power Shell 列出 Share Point 中的文件 | system.net.webclient



我可以使用System.net.webclient从在线共享点下载文件,但是在System.net.webclient类(https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=netframework-4.8(中没有看到任何相关的方法来列出文件夹中的文件。下面是用于下载文件的代码片段。此处列出文件的任何帮助将不胜感激。

$wc = New-Object System.Net.WebClient 
$wc.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $SecurePassword)
$wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$wc.DownloadData($Source, $DownloadPath)

我们需要使用CSOM 和 PowerShell 从 SharePoint 联机库中的文件夹中获取所有文件,然后使用 Web 客户端下载所有文件,以下示例代码供您参考。

Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.dll" 
Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions16ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
#Setup Credentials to connect
#Config Variables
$SiteURL = "https://tenant.sharepoint.com/sites/team"
$UserName="test@tenant.onmicrosoft.com"
$Password ="password"
#Folder's Site Relative Path 
$FolderURL= "Shared Documents/Subfolder"
$LocalPath="C:tempSubfolder"
#Get Credentials to connect
#$Cred = Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))  
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) 
$Ctx.Credentials = $Credentials 
#Get the Folder and Files
$Folder=$Ctx.Web.GetFolderByServerRelativeUrl($FolderURL) 
$Ctx.Load($Folder) 
$Ctx.Load($Folder.Files) 
$Ctx.ExecuteQuery() 
#Iterate through each File in the folder 
Try 
{
Foreach($File in $Folder.Files) 
{       
$wc = New-Object System.Net.WebClient 
$wc.Credentials =$Credentials
$wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$FilePath=$SiteURL+"/"+$FolderURL+"/"+$File.Name        
$DownloadPath=$LocalPath+""+$File.Name      
$wc.DownloadFile($FilePath, $DownloadPath)
$wc.Dispose()
}
}
catch 
{
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}   

相关内容

  • 没有找到相关文章

最新更新