$configPath = Join-Path $PSScriptRoot "config.json" function Load-Config { if (Test-Path $configPath) { try { $script:config = Get-Content $configPath | ConvertFrom-Json $script:txtSiteUrl.Text = $config.SiteUrl return $true } catch { Write-Error "Invalid config file: $_" return $false } } return $false } function Save-Config { param( [string]$SiteUrl, [string]$ProductionLibrary, [string]$TempLibrary ) $config = @{ SiteUrl = $SiteUrl ProductionLibrary = $ProductionLibrary TempLibrary = $TempLibrary } $config | ConvertTo-Json | Set-Content $configPath } function Connect-SharePoint { return Connect-Graph } function List-XlsFiles { if (-not (Connect-SharePoint)) { return } $folderPath = $script:txtFolder.Text $query = " .xls .xlsx " $items = Get-PnPListItem -List $script:txtProdLib.Text -PageSize 2000 -Query $query if (-not [string]::IsNullOrWhiteSpace($folderPath)) { $folderFilter = "*$($folderPath.Replace('\','/'))*" $items = $items | Where { $_.FieldValues.FileRef -like $folderFilter } } foreach ($item in $items) { $fileList += [PSCustomObject]@{ OriginalPath = $item.FieldValues.FileRef TempPath = "$($script:txtTempLib.Text)/$($item.FieldValues.FileLeafRef)" } } $fileList | ConvertTo-Json | Set-PnPFileContent -Folder $tempLibrary -FileName "FileList.json" $script:txtStatus.Text += "Found $($fileList.Count) XLS files`n" } function Convert-Files { if (-not (Connect-SharePoint)) { } $excel = New-Object -ComObject Excel.Application $excel.Visible = $false try { Get-PnPFile -Url "/sites/yoursite/$($script:txtTempLib.Text)/FileList.json" -Path $env:TEMP -Filename "FileList.json" $files = Get-Content "$env:TEMP\FileList.json" | ConvertFrom-Json foreach ($file in $files) { try { $workbook = $excel.Workbooks.Open((Convert-PnPFile -Url $file.OriginalPath -Path $env:TEMP -Filename $file.OriginalFileName)) $newName = [System.IO.Path]::ChangeExtension($file.OriginalFileName, ".xlsx") $workbook.SaveAs([System.IO.Path]::Combine($env:TEMP, $newName), 51) # 51 = xlsx Add-PnPFile -Folder "Documents" -Path "$env:TEMP\$newName" $script:txtStatus.Text += "Converted $($file.OriginalFileName) to XLSX`n" } finally { $workbook.Close() } } } finally { $excel.Quit() } } function Move-Files { if (-not (Connect-SharePoint)) { } $files = Get-Content "$env:TEMP\FileList.json" | ConvertFrom-Json foreach ($file in $files) { Move-PnPFile -SourceUrl $file.OriginalPath -TargetUrl $file.TempPath -Force $xlsxPath = [System.IO.Path]::ChangeExtension($file.OriginalPath, ".xlsx") Move-PnPFile -SourceUrl "$($script:txtProdLib.Text)/$xlsxPath" -TargetUrl $xlsxPath -Force $script:txtStatus.Text += "Moved $($file.OriginalFileName) and its XLSX version`n" } } function Get-FolderBrowser { try { # Get site ID first $siteUrl = $script:txtSiteUrl.Text.TrimEnd('/') # Get site directly using the URL $encodedUrl = [System.Web.HttpUtility]::UrlEncode($siteUrl) $site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop if (-not $site) { throw "Site not found at $siteUrl" } $script:txtStatus.Text += "Found site: $($site.webUrl)`n" # Get drive (document library) $drives = Get-MgSiteDrive -SiteId $site.id # Handle "Shared Documents" special case $libraryName = if ($script:txtProdLib.Text -eq "Shared Documents") { "Documents" } else { $script:txtProdLib.Text } $drive = $drives | Where-Object { $_.Name -eq $libraryName } if (-not $drive) { $script:txtStatus.Text += "Available libraries: $($drives.Name -join ', ')`n" throw "Library not found: $libraryName" } # Get folders $folders = Get-MgDriveRoot -DriveId $drive.Id | Get-MgDriveItemChild -DriveId $drive.Id $folders = $folders | Where-Object { $_.Folder } # Create custom objects for the grid view $folderObjects = $folders | ForEach-Object { [PSCustomObject]@{ Name = $_.Name ServerRelativeUrl = "/sites/tax/Shared Documents/$($_.Name)" } } return $folderObjects | Out-GridView -Title "Select Folder" -PassThru } catch { $script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n" return $null } }