如何使用PowerShell从SharePoint获取所有文档的列表?
我想使用PowerShell从SharePoint检索所有文档,但目前只能检索特定站点的文档 这是我正在使用的代码:如何使用PowerShell从SharePoint获取所有文档的列表?,powershell,sharepoint,sharepoint-online,Powershell,Sharepoint,Sharepoint Online,我想使用PowerShell从SharePoint检索所有文档,但目前只能检索特定站点的文档 这是我正在使用的代码: #Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program File
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
#Set Parameters
$SiteURL="https://xxx.sharepoint.com/sites/CRMDevelopment"
$LibraryName="Documents"
$ReportOutput = "C:\users\xxx\downloads\VersionHistory.csv"
Try {
#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
#Get the web & Library
$Web=$Ctx.Web
$Ctx.Load($Web)
$List = $Web.Lists.GetByTitle($LibraryName)
$Ctx.ExecuteQuery()
#Get All Files of from the document library - Excluding Folders
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>"
$ListItems=$List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$VersionHistoryData = @()
#Iterate throgh each version of file
Foreach ($Item in $ListItems)
{
##more code goes here.
}
}
#加载SharePoint CSOM程序集
添加类型-路径“C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
添加类型-路径“C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
#设置参数
$SiteURL=”https://xxx.sharepoint.com/sites/CRMDevelopment"
$LibraryName=“文档”
$ReportOutput=“C:\users\xxx\downloads\VersionHistory.csv”
试一试{
#设置要连接的凭据
$Cred=获取凭据
$Credentials=新对象Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username,$Cred.Password)
#设置上下文
$Ctx=新对象Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials=$Credentials
#获取web和库
$Web=$Ctx.Web
$Ctx.Load($Web)
$List=$Web.Lists.GetByTitle($LibraryName)
$Ctx.ExecuteQuery()
#从文档库获取的所有文件-不包括文件夹
$Query=新对象Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml=“0”
$ListItems=$List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$VersionHistoryData=@()
#通过文件的每个版本进行迭代
Foreach($ListItems中的项目)
{
##这里有更多的代码。
}
}
演示通过PnP PowerShell和SharePoint online management shell迭代所有网站集,如果要访问所有网站集中的库,则该帐户需要访问所有网站集(默认情况下,管理员没有访问所有网站集的权限)
#区域变量
$AdminName=”admin@xxx.onmicrosoft.com"
$Password=“Password”
#端域变量
#地区证书
[SecureString]$SecurePass=converttoSecureString$Password-AsPlainText-Force
$credentials=新对象-TypeName System.Management.Automation.PSCredential-argumentlist$AdminName,$(convertto securestring$Password-asplaintext-force)
#endregion凭据
#配置参数
$AdminSiteURL=”https://xxx-admin.sharepoint.com"
#$ReportOutput=“C:\users\xxx\downloads\VersionHistory.csv”
#连接到SharePoint联机管理中心
连接SPOService-Url$AdminSiteURL–凭据$credentials
#获取所有网站集
$SiteCollections=获取SPOSite-限制所有
写入主机“找到的网站集总数:$sitecolections.count-f黄色
#循环浏览每个网站集并检索详细信息
Foreach($SiteCollections中的站点)
{
#针对特定站点的测试
#如果($Site.URL-eq)https://xxx.sharepoint.com/sites/lee"){
写入主机“处理网站集:$Site.URL-f黄色
连接PnPOnline-Url$Site.Url-credentials$credentials
$items=获取PnPListItem-列出文档-查询“0”
Foreach($项目中的项目)
{
写入主机$Item.FieldValues[“FileRef”]
}
#}
}
写入主机“-”
为什么要使用两个不同的SharePoint版本进行标记?在线和2013不一样。你用哪一种?
#region Variables
$AdminName = "admin@xxx.onmicrosoft.com"
$Password = "password"
#endregion Variables
#region Credentials
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $(convertto-securestring $Password -asplaintext -force)
#endregion Credentials
#Config Parameters
$AdminSiteURL="https://xxx-admin.sharepoint.com"
#$ReportOutput = "C:\users\xxx\downloads\VersionHistory.csv"
#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –Credential $credentials
#Get All site collections
$SiteCollections = Get-SPOSite -Limit All
Write-Host "Total Number of Site collections Found:"$SiteCollections.count -f Yellow
#Loop through each site collection and retrieve details
Foreach ($Site in $SiteCollections)
{
#Test for specific site
#if($Site.URL -eq "https://xxx.sharepoint.com/sites/lee"){
Write-Host "Processing Site Collection :"$Site.URL -f Yellow
Connect-PnPOnline -Url $Site.URL -credentials $credentials
$items=Get-PnPListItem -List Documents -Query "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>"
Foreach ($Item in $items)
{
Write-Host $Item.FieldValues["FileRef"]
}
#}
}
Write-Host "--"