refactor: Update List-XlsFiles to use Graph API with detailed logging
This commit is contained in:
parent
e6e8207019
commit
0a4df5e4dc
@ -36,34 +36,70 @@ function Connect-SharePoint {
|
||||
}
|
||||
|
||||
function List-XlsFiles {
|
||||
try {
|
||||
$script:txtStatus.Text += "Connecting to SharePoint...`n"
|
||||
if (-not (Connect-SharePoint)) {
|
||||
return
|
||||
throw "Failed to connect to SharePoint"
|
||||
}
|
||||
|
||||
$script:txtStatus.Text += "Getting site...`n"
|
||||
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
||||
|
||||
$script:txtStatus.Text += "Getting document library...`n"
|
||||
$drives = Get-MgSiteDrive -SiteId $site.id
|
||||
$libraryName = if ($script:txtProdLib.Text -eq "Shared Documents") {
|
||||
"Documents"
|
||||
} else {
|
||||
$script:txtProdLib.Text
|
||||
}
|
||||
$drive = $drives | Where-Object { $_.Name -eq $libraryName }
|
||||
|
||||
if (-not $drive) {
|
||||
throw "Library not found: $libraryName. Available libraries: $($drives.Name -join ', ')"
|
||||
}
|
||||
|
||||
$script:txtStatus.Text += "Getting root folder...`n"
|
||||
$root = Get-MgDriveRoot -DriveId $drive.Id
|
||||
|
||||
$fileList = @()
|
||||
$folderPath = $script:txtFolder.Text
|
||||
$query = "<View Scope='RecursiveAll'><Query><Where>
|
||||
<And>
|
||||
<Contains><FieldRef Name='FileRef'/><Value Type='Text'>.xls</Value></Contains>
|
||||
<Not><Contains><FieldRef Name='FileRef'/><Value Type='Text'>.xlsx</Value></Contains></Not>
|
||||
</And>
|
||||
</Where></Query></View>"
|
||||
|
||||
$items = Get-PnPListItem -List $script:txtProdLib.Text -PageSize 2000 -Query $query
|
||||
$script:txtStatus.Text += "Searching for XLS files...`n"
|
||||
|
||||
# If a specific folder is selected, search only in that folder
|
||||
if (-not [string]::IsNullOrWhiteSpace($folderPath)) {
|
||||
$folderFilter = "*$($folderPath.Replace('\','/'))*"
|
||||
$items = $items | Where { $_.FieldValues.FileRef -like $folderFilter }
|
||||
$script:txtStatus.Text += "Searching in folder: $folderPath`n"
|
||||
# Get the folder ID first
|
||||
$folderItem = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:$($folderPath)"
|
||||
$items = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $folderItem.id
|
||||
} else {
|
||||
$script:txtStatus.Text += "Searching in root folder`n"
|
||||
$items = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $root.Id
|
||||
}
|
||||
|
||||
foreach ($item in $items) {
|
||||
$script:txtStatus.Text += "Checking file: $($item.Name)`n"
|
||||
if ($item.Name -like "*.xls" -and $item.Name -notlike "*.xlsx") {
|
||||
$fileList += [PSCustomObject]@{
|
||||
OriginalPath = $item.FieldValues.FileRef
|
||||
TempPath = "$($script:txtTempLib.Text)/$($item.FieldValues.FileLeafRef)"
|
||||
OriginalPath = $item.WebUrl
|
||||
OriginalFileName = $item.Name
|
||||
TempPath = "$($script:txtTempLib.Text)/$($item.Name)"
|
||||
}
|
||||
$script:txtStatus.Text += "Found XLS file: $($item.Name)`n"
|
||||
}
|
||||
}
|
||||
|
||||
$fileList | ConvertTo-Json | Set-PnPFileContent -Folder $tempLibrary -FileName "FileList.json"
|
||||
if ($fileList.Count -gt 0) {
|
||||
$script:txtStatus.Text += "Saving file list...`n"
|
||||
$fileList | ConvertTo-Json | Out-File "$env:TEMP\FileList.json"
|
||||
$script:txtStatus.Text += "Found $($fileList.Count) XLS files`n"
|
||||
} else {
|
||||
$script:txtStatus.Text += "No XLS files found`n"
|
||||
}
|
||||
}
|
||||
catch {
|
||||
$script:txtStatus.Text += "Error listing files: $($_.Exception.Message)`n"
|
||||
}
|
||||
}
|
||||
|
||||
function Convert-Files {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user