From bbf46c9eb85f458ae69f609a60f89bc380af17b4 Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Fri, 21 Feb 2025 09:42:26 -0800 Subject: [PATCH] fix: Add null checks and error handling in `Get-XlsFilesRecursive` --- SharePointFunctions.ps1 | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/SharePointFunctions.ps1 b/SharePointFunctions.ps1 index 98acc77..ed741cc 100644 --- a/SharePointFunctions.ps1 +++ b/SharePointFunctions.ps1 @@ -280,8 +280,24 @@ function Get-XlsFilesRecursive { $allFiles = @() + # Check if DriveId or DriveItemId is null or empty + if ([string]::IsNullOrEmpty($DriveId)) { + $script:txtStatus.Text += "Error: DriveId is null or empty`n" + return $allFiles + } + if ([string]::IsNullOrEmpty($DriveItemId)) { + $script:txtStatus.Text += "Error: DriveItemId is null or empty`n" + return $allFiles + } + # Get items in the current folder - $items = Get-MgDriveItemChild -DriveId $DriveId -DriveItemId $DriveItemId + try { + $items = Get-MgDriveItemChild -DriveId $DriveId -DriveItemId $DriveItemId -ErrorAction Stop + } + catch { + $script:txtStatus.Text += "Error getting items: $($_.Exception.Message)`n" + return $allFiles + } foreach ($item in $items) { $script:txtStatus.Text += "Checking item: $($item.Name)`n"