PowerShell command to generate OneDrive report for a user's on files included sub folder & Size

JT 61 Reputation points
2021-01-26T07:55:07.35+00:00

I had provided the 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

It will be good if the code to modify so that it can support MFA.

Below is the original code given but have error.

================================

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 = "username@keyman .com"

$password = "<password>"

$srcUrl = "<source Url>" ### https://domain/sites/<sitename>

$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

========================

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>

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,389 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 30,376 Reputation points Microsoft Vendor
    2021-01-26T09:33:29.87+00:00

    Exception calling "ExecuteQuery" with "0" argument(s): "List 'Documents' does not exist at site with URL

    Please check the name of your document library and set $srcLibrary to the correct name.

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.