please refer to powerShell code below, i had tried to run and hit into error. I need some expertise to assist to look into this code on how to make it work. Really appreciate if someone able to assist on this
I had posted this question here, but I has been asked to repost it to SharePoint
here is the code
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"
$now=Get-Date -format "dd-MMM-yy,HH:mm:ss"
$fileFormat = Get-Date -format "dd-MMM-yy_HHmmss"
Write-Host "Script Start : '$($now)'" -ForegroundColor Yellow
$global:SourceCount = 0 ### To know the total count of the documents to be processed
$global:Processed = 0
$global:OutFilePath = "C:\Reports\files_" + $fileFormat + ".csv"
$header = "Date,Time,Type,Parent,Name,Path,FilesCount,FileSize(bytes),Remark"
Add-Content -Path $global:OutFilePath -Value "`n $header"
$username = "XXX@Piepel .com"
$password = "XXXXXXXXXXX"
$srcUrl = "https://XXXXXX-my.sharepoint.com/personal/SSSSXXXXYYYY_com"
$srcLibrary = "Documents"
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
function to create a log for the report in csv
function WriteLog
{
param (
[Parameter(Mandatory=$true)] $type, $folderName,$name,$path,$fileCount,$fileSize,$remark
)
$nowTime=Get-Date -format "dd-MMM-yy,HH:mm:ss"
$folderName = $folderName.replace(",","|") ### sometime folder / file name has comma so replace it with something
$name = $name.replace(",","|")
$path = $path.replace(",","|")
$lineContent = "$($nowTime),$($type),$($folderName),$($name),$($path),$($fileCount),$($fileSize),$($remark)"
Add-Content -Path $global:OutFilePath -Value "$lineContent"
$global:Processed = $global:Processed +1
}
function ScanFolders
{
param (
[Parameter(Mandatory=$true)] $srcfolder, $parentName
)
$remarkDetail = ""
$replacedUser=""
Write-Host "Total Count: $($global:SourceCount) Completed: $($global:Processed)" -ForegroundColor Cyan
Write-Host "Navigate to: " $srcfolder.ServerRelativeUrl -ForegroundColor Yellow
$folderItem = $srcfolder.ListItemAllFields
$srcContext.Load($f)
$srcContext.Load($folderItem)
$srcContext.ExecuteQuery()
$authorEmail = $folderItem["Author"].Email
$editorEmail = $folderItem["Editor"].Email
$filepath = $folderItem["FileDirRef"]
$fileSize = $fItem["File_x0020_Size"]
$fileName = $srcfolder.Name
$fileCol = $srcfolder.Files
$srcContext.Load($fileCol)
$srcContext.ExecuteQuery()
WriteLog "Folder" $parentName $fileName $filepath $fileCol.Count 0 $remarkDetail
foreach ($f in $fileCol)
{
$remarkDetail = ""
$replacedUser=""
$fItem = $f.ListItemAllFields
$srcContext.Load($f)
$srcContext.Load($fItem)
$srcContext.ExecuteQuery()
$authorEmail = $fItem["Author"].Email
$editorEmail = $fItem["Editor"].Email
$filepath = $fItem["FileDirRef"]
$fileSize = $fItem["File_x0020_Size"]
$fileName = $fItem["FileLeafRef"]
WriteLog "File" $srcfolder.Name $fileName $filepath 0 $fileSize $remarkDetail
}
$fL1FolderColl = $srcfolder.Folders
$srcContext.Load($fL1FolderColl);
$srcContext.ExecuteQuery();
foreach ($myFolder in $fL1FolderColl)
{
$srcContext.Load($myFolder)
$srcContext.ExecuteQuery()
ScanFolders $myFolder $srcfolder.Name
}
}
The script starts here to run ####
Write-Host "Authenticating ..." -ForegroundColor White
$srcContext = New-Object Microsoft.SharePoint.Client.ClientContext($srcUrl)
$srcContext.Credentials = $credentials
$srcWeb = $srcContext.Web
$srcList = $srcWeb.Lists.GetByTitle($srcLibrary)
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$listItems = $srcList.GetItems($query)
$srcContext.Load($srcList)
$srcContext.Load($listItems)
$srcContext.ExecuteQuery()
$global:SourceCount = $srcList.ItemCount
Write-Host "Total Count: $($global:SourceCount)" -ForegroundColor Cyan
foreach($item in $listItems)
{
if($item.FileSystemObjectType -eq "File")
{
$remarkDetail = ""
$replacedUser=""
$srcF = $item.File
$fItem = $srcF.ListItemAllFields
$srcContext.Load($srcF)
$srcContext.Load($fItem)
$srcContext.ExecuteQuery()
$authorEmail = $fItem["Author"].Email
$editorEmail = $fItem["Editor"].Email
$filepath = $fItem["FileDirRef"]
$fileSize = $fItem["File_x0020_Size"]
$fileName = $fItem["FileLeafRef"]
WriteLog "File" "Root" $fileName $filepath 0 $fileSize $remarkDetail
}
elseif ($item.FileSystemObjectType -eq "Folder")
{
$srcContext.Load($item)
$srcContext.ExecuteQuery()
$folder = $srcWeb.GetFolderByServerRelativeUrl($item.FieldValues["FileRef"].ToString())
$srcContext.Load($folder)
$srcContext.ExecuteQuery()
ScanFolders $folder "Root"
}
}
$now=Get-Date -format "dd-MMM-yy,HH:mm:ss"
Write-Host "Total Count: $($global:SourceCount) Completed: $($global:Processed)" -ForegroundColor Cyan
Write-Host "END Start : '$($now)'" -ForegroundColor Yellow
===============
the error I had encounter is
Attached below is the error encountered.
PS C:\reports> & '.\All item usage.ps1'
Script Start : '26-Jan-21,09:24:51'
Authenticating ...
Exception calling "ExecuteQuery" with "0" argument(s): "List 'Documents' does not exist at site with URL
'https://XXXXXXX.sharepoint.com/personal/XXXXXXXX_com'.";
At C:\reports\All item usage.ps1:207 char:1
- $srcContext.ExecuteQuery()
- ~~~~~~~~~~~~~~~~~~~~~~~~~~
- CategoryInfo : NotSpecified: (:) [], MethodInvocationException
- FullyQualifiedErrorId : ServerException
Total Count:
The collection has not been initialized. It has not been requested or the request has not been executed. It may need
to be explicitly requested.
At C:\reports\All item usage.ps1:213 char:9
- foreach($item in $listItems)
- ~~~~~
- CategoryInfo : OperationStopped: (:) [], CollectionNotInitializedException
- FullyQualifiedErrorId : Microsoft.SharePoint.Client.CollectionNotInitializedException
Total Count: Completed: 0
END Start : '26-Jan-21,09:24:52'
PS C:\reports>