Compare commits
No commits in common. "ffbe44da0a1d1ee4698ec607bd22ff2c2ceb400f" and "b55058161a14350bb0f6c877f9df875b3b249af5" have entirely different histories.
ffbe44da0a
...
b55058161a
@ -5,7 +5,8 @@ Import-Module Microsoft.Graph.Files
|
|||||||
|
|
||||||
# Import our components
|
# Import our components
|
||||||
. "$PSScriptRoot\GraphConfig.ps1"
|
. "$PSScriptRoot\GraphConfig.ps1"
|
||||||
. "$PSScriptRoot\SharePointFunctions.ps1"
|
$SharePointFunctions = "$PSScriptRoot\SharePointFunctions.ps1"
|
||||||
|
Import-Module $SharePointFunctions -Force
|
||||||
. "$PSScriptRoot\EventHandlers.ps1"
|
. "$PSScriptRoot\EventHandlers.ps1"
|
||||||
|
|
||||||
# Create main form
|
# Create main form
|
||||||
|
|||||||
@ -2,6 +2,17 @@
|
|||||||
|
|
||||||
$script:config = $null # Initialize config variable
|
$script:config = $null # Initialize config variable
|
||||||
|
|
||||||
|
# Export functions that will be called from other scripts
|
||||||
|
Export-ModuleMember -Function @(
|
||||||
|
'Load-Config',
|
||||||
|
'Save-Config',
|
||||||
|
'Get-FolderBrowser',
|
||||||
|
'Connect-SharePoint',
|
||||||
|
'List-XlsFiles',
|
||||||
|
'Convert-Files',
|
||||||
|
'Move-Files'
|
||||||
|
)
|
||||||
|
|
||||||
# Load configuration from config.json
|
# Load configuration from config.json
|
||||||
function Load-Config {
|
function Load-Config {
|
||||||
try {
|
try {
|
||||||
@ -458,50 +469,36 @@ function Convert-Files {
|
|||||||
|
|
||||||
# Convert the file to XLSX
|
# Convert the file to XLSX
|
||||||
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
||||||
# Before the Excel conversion, get the full paths and clean them
|
# Use Join-Path to properly handle paths with spaces
|
||||||
$xlsxPath = Join-Path $env:TEMP $xlsxFileName
|
$xlsxPath = Join-Path $env:TEMP $xlsxFileName
|
||||||
$downloadPath = Join-Path $env:TEMP $originalFileName
|
$downloadPath = Join-Path $env:TEMP $originalFileName
|
||||||
|
|
||||||
# Clean the paths and ensure they're valid
|
# Convert the paths to proper Windows paths
|
||||||
$xlsxPath = [System.IO.Path]::GetFullPath($xlsxPath)
|
$xlsxPath = [System.IO.Path]::GetFullPath($xlsxPath)
|
||||||
$downloadPath = [System.IO.Path]::GetFullPath($downloadPath)
|
$downloadPath = [System.IO.Path]::GetFullPath($downloadPath)
|
||||||
|
|
||||||
# Verify the download file exists
|
|
||||||
if (-not (Test-Path -LiteralPath $downloadPath)) {
|
|
||||||
throw "Downloaded file not found at: $downloadPath"
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
# Open Excel
|
# Open Excel
|
||||||
$excel = New-Object -ComObject Excel.Application
|
$excel = New-Object -ComObject Excel.Application
|
||||||
$excel.Visible = $false
|
$excel.Visible = $false
|
||||||
$excel.DisplayAlerts = $false
|
$excel.DisplayAlerts = $false # Suppress Excel alerts
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Write-Host "Opening workbook: $downloadPath"
|
|
||||||
$workbook = $excel.Workbooks.Open($downloadPath)
|
$workbook = $excel.Workbooks.Open($downloadPath)
|
||||||
|
|
||||||
Write-Host "Saving as XLSX: $xlsxPath"
|
# Save as XLSX using the full path
|
||||||
try {
|
$workbook.SaveAs([string]$xlsxPath, 51) # 51 = xlOpenXMLWorkbook (*.xlsx)
|
||||||
# Save directly to the final path
|
$workbook.Close($true) # True to save changes
|
||||||
$workbook.SaveAs([string]$xlsxPath, 51) # 51 = xlWorkbookDefault (xlsx format)
|
|
||||||
$workbook.Close($true)
|
|
||||||
}
|
|
||||||
catch {
|
|
||||||
$script:txtStatus.Text += "Error during SaveAs: $($_.Exception.Message)`n"
|
|
||||||
throw "SaveAs failed: $($_.Exception.Message)" # Re-throw the exception to be caught by the outer catch
|
|
||||||
}
|
|
||||||
|
|
||||||
Start-Sleep -Seconds 2 # Give Excel time to finish
|
Start-Sleep -Seconds 1 # Give Excel time to finish saving
|
||||||
|
|
||||||
|
# Verify the file was created
|
||||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||||
throw "Failed to create XLSX file at: $xlsxPath"
|
throw "Excel SaveAs succeeded but file not found at: $xlsxPath"
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
# Cleanup
|
# Proper cleanup of Excel COM objects
|
||||||
$excel.Quit()
|
$excel.Quit()
|
||||||
if ($workbook) {
|
if ($workbook) {
|
||||||
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
|
||||||
@ -509,11 +506,6 @@ function Convert-Files {
|
|||||||
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
|
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
|
||||||
[System.GC]::Collect()
|
[System.GC]::Collect()
|
||||||
[System.GC]::WaitForPendingFinalizers()
|
[System.GC]::WaitForPendingFinalizers()
|
||||||
|
|
||||||
# Force cleanup of any remaining Excel processes
|
|
||||||
Get-Process -Name "EXCEL" -ErrorAction SilentlyContinue |
|
|
||||||
Where-Object { $_.SI -eq [System.Security.Principal.WindowsIdentity]::GetCurrent().SessionId } |
|
|
||||||
Stop-Process -Force
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
$script:txtStatus.Text += "Successfully converted to: $xlsxFileName`n"
|
||||||
@ -521,25 +513,13 @@ function Convert-Files {
|
|||||||
# Upload the converted file to SharePoint
|
# Upload the converted file to SharePoint
|
||||||
try {
|
try {
|
||||||
# Verify file exists before trying to read it
|
# Verify file exists before trying to read it
|
||||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
if (-not (Test-Path $xlsxPath)) {
|
||||||
throw "Converted file not found at: $xlsxPath"
|
throw "Converted file not found at: $xlsxPath"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the destination folder from the TempLibrary field
|
# Get the destination folder
|
||||||
$tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
$destinationFolder = $script:txtTempLib.Text
|
||||||
"Documents"
|
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content"
|
||||||
} else {
|
|
||||||
$script:txtTempLib.Text
|
|
||||||
}
|
|
||||||
|
|
||||||
# Get the drive for the temp library
|
|
||||||
$tempDrive = $drives | Where-Object { $_.Name -eq $tempLibrary }
|
|
||||||
if (-not $tempDrive) {
|
|
||||||
throw "Temp library not found: $tempLibrary"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Construct the upload URI for the temp library
|
|
||||||
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($tempDrive.Id)/root:/$($xlsxFileName):/content"
|
|
||||||
|
|
||||||
# Read the file content
|
# Read the file content
|
||||||
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
||||||
@ -547,11 +527,11 @@ function Convert-Files {
|
|||||||
# Upload the file
|
# Upload the file
|
||||||
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
||||||
|
|
||||||
$script:txtStatus.Text += "Successfully uploaded to temp library: $tempLibrary/$xlsxFileName`n"
|
$script:txtStatus.Text += "Successfully uploaded to SharePoint: $destinationFolder/$xlsxFileName`n"
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
||||||
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
if (-not (Test-Path $xlsxPath)) {
|
||||||
$script:txtStatus.Text += "File not found at: $xlsxPath`n"
|
$script:txtStatus.Text += "File not found at: $xlsxPath`n"
|
||||||
}
|
}
|
||||||
continue # Move to the next file
|
continue # Move to the next file
|
||||||
@ -562,6 +542,24 @@ function Convert-Files {
|
|||||||
continue # Move to the next file
|
continue # Move to the next file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Upload the converted file to SharePoint
|
||||||
|
try {
|
||||||
|
# Get the destination folder
|
||||||
|
$destinationFolder = $script:txtTempLib.Text
|
||||||
|
$uploadUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$($destinationFolder)/$($xlsxFileName):/content"
|
||||||
|
|
||||||
|
# Read the file content
|
||||||
|
$fileContent = [System.IO.File]::ReadAllBytes($xlsxPath)
|
||||||
|
|
||||||
|
# Upload the file
|
||||||
|
Invoke-MgGraphRequest -Uri $uploadUri -Method PUT -Body $fileContent -ContentType "application/octet-stream"
|
||||||
|
|
||||||
|
$script:txtStatus.Text += "Successfully uploaded to SharePoint: $destinationFolder/$xlsxFileName`n"
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
||||||
|
continue # Move to the next file
|
||||||
|
}
|
||||||
|
|
||||||
# Clean up temporary files
|
# Clean up temporary files
|
||||||
try {
|
try {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user