Compare commits
No commits in common. "7822f074c4cf5e07914dbe3e3ce3849b5450db08" and "d729b88606a314721575fb390b5e7d8a02c64103" have entirely different histories.
7822f074c4
...
d729b88606
@ -8,3 +8,8 @@ function Convert-Files_Click { # Rename to avoid conflict
|
|||||||
$script:txtStatus.Text += "Starting conversion...`n"
|
$script:txtStatus.Text += "Starting conversion...`n"
|
||||||
Convert-Files # Call the function in SharePointFunctions.ps1
|
Convert-Files # Call the function in SharePointFunctions.ps1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function Move-Files_Click { # Rename to avoid conflict
|
||||||
|
$script:txtStatus.Text += "Moving files...`n"
|
||||||
|
Move-Files # Call the function in SharePointFunctions.ps1
|
||||||
|
}
|
||||||
|
|||||||
15
MainForm.ps1
15
MainForm.ps1
@ -60,6 +60,12 @@ $btnConvert.Size = New-Object System.Drawing.Size(120, 30)
|
|||||||
$btnConvert.Text = "Convert to XLSX"
|
$btnConvert.Text = "Convert to XLSX"
|
||||||
$form.Controls.Add($btnConvert)
|
$form.Controls.Add($btnConvert)
|
||||||
|
|
||||||
|
$btnMove = New-Object System.Windows.Forms.Button
|
||||||
|
$btnMove.Location = New-Object System.Drawing.Point(300, 170)
|
||||||
|
$btnMove.Size = New-Object System.Drawing.Size(120, 30)
|
||||||
|
$btnMove.Text = "Move Files"
|
||||||
|
$form.Controls.Add($btnMove)
|
||||||
|
|
||||||
# Create Checkbox for Recursive Listing
|
# Create Checkbox for Recursive Listing
|
||||||
$chkRecursive = New-Object System.Windows.Forms.CheckBox
|
$chkRecursive = New-Object System.Windows.Forms.CheckBox
|
||||||
$chkRecursive.Location = New-Object System.Drawing.Point(20, 205)
|
$chkRecursive.Location = New-Object System.Drawing.Point(20, 205)
|
||||||
@ -90,9 +96,10 @@ $dataGridView.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeCo
|
|||||||
$form.Controls.Add($dataGridView)
|
$form.Controls.Add($dataGridView)
|
||||||
|
|
||||||
# Add columns to DataGridView
|
# Add columns to DataGridView
|
||||||
$dataGridView.ColumnCount = 2
|
$dataGridView.ColumnCount = 3
|
||||||
$dataGridView.Columns[0].Name = "Original Path"
|
$dataGridView.Columns[0].Name = "Original Path"
|
||||||
$dataGridView.Columns[1].Name = "Original File Name"
|
$dataGridView.Columns[1].Name = "Original File Name"
|
||||||
|
$dataGridView.Columns[2].Name = "Temp Path"
|
||||||
|
|
||||||
# Add Save Config button
|
# Add Save Config button
|
||||||
$btnSaveConfig = New-Object System.Windows.Forms.Button
|
$btnSaveConfig = New-Object System.Windows.Forms.Button
|
||||||
@ -101,7 +108,8 @@ $btnSaveConfig.Size = New-Object System.Drawing.Size(80, 20)
|
|||||||
$btnSaveConfig.Text = "Save Config"
|
$btnSaveConfig.Text = "Save Config"
|
||||||
$btnSaveConfig.Add_Click({
|
$btnSaveConfig.Add_Click({
|
||||||
Save-Config -SiteUrl $txtSiteUrl.Text `
|
Save-Config -SiteUrl $txtSiteUrl.Text `
|
||||||
-ProductionLibrary $txtProdLib.Text
|
-ProductionLibrary $txtProdLib.Text `
|
||||||
|
-TempLibrary $txtTempLib.Text
|
||||||
})
|
})
|
||||||
|
|
||||||
# Add Connect button
|
# Add Connect button
|
||||||
@ -118,6 +126,7 @@ $btnConnect.Add_Click({
|
|||||||
$form.Controls.AddRange(@(
|
$form.Controls.AddRange(@(
|
||||||
$txtSiteUrl,
|
$txtSiteUrl,
|
||||||
$txtProdLib,
|
$txtProdLib,
|
||||||
|
$txtTempLib,
|
||||||
$txtFolder,
|
$txtFolder,
|
||||||
$btnBrowse,
|
$btnBrowse,
|
||||||
$btnSaveConfig,
|
$btnSaveConfig,
|
||||||
@ -132,11 +141,13 @@ if (-not (Load-Config)) {
|
|||||||
else {
|
else {
|
||||||
$txtSiteUrl.Text = $config.SiteUrl
|
$txtSiteUrl.Text = $config.SiteUrl
|
||||||
$txtProdLib.Text = $config.ProductionLibrary
|
$txtProdLib.Text = $config.ProductionLibrary
|
||||||
|
$txtTempLib.Text = $config.TempLibrary
|
||||||
}
|
}
|
||||||
|
|
||||||
# Attach event handlers
|
# Attach event handlers
|
||||||
$btnList.Add_Click({ List-XlsFiles_Click })
|
$btnList.Add_Click({ List-XlsFiles_Click })
|
||||||
$btnConvert.Add_Click({ Convert-Files_Click })
|
$btnConvert.Add_Click({ Convert-Files_Click })
|
||||||
|
$btnMove.Add_Click({ Move-Files_Click })
|
||||||
$btnBrowse.Add_Click({
|
$btnBrowse.Add_Click({
|
||||||
try {
|
try {
|
||||||
if (-not (Connect-SharePoint)) {
|
if (-not (Connect-SharePoint)) {
|
||||||
|
|||||||
@ -282,7 +282,7 @@ function List-XlsFiles {
|
|||||||
$script:txtStatus.Text += "Saving file list...`n"
|
$script:txtStatus.Text += "Saving file list...`n"
|
||||||
# Display each file on a new line
|
# Display each file on a new line
|
||||||
foreach ($file in $fileList) {
|
foreach ($file in $fileList) {
|
||||||
$row = @($file.OriginalPath, $file.OriginalFileName)
|
$row = @($file.OriginalPath, $file.OriginalFileName, $file.TempPath)
|
||||||
$script:dataGridView.Rows.Add($row)
|
$script:dataGridView.Rows.Add($row)
|
||||||
}
|
}
|
||||||
#$fileList | ForEach-Object {
|
#$fileList | ForEach-Object {
|
||||||
@ -341,6 +341,7 @@ function Get-XlsFilesRecursive {
|
|||||||
$allFiles += [PSCustomObject]@{
|
$allFiles += [PSCustomObject]@{
|
||||||
OriginalPath = $item.WebUrl
|
OriginalPath = $item.WebUrl
|
||||||
OriginalFileName = $item.Name
|
OriginalFileName = $item.Name
|
||||||
|
TempPath = "$($script:txtTempLib.Text)/$($item.Name)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -385,6 +386,7 @@ function Get-XlsFilesCurrentFolder {
|
|||||||
$allFiles += [PSCustomObject]@{
|
$allFiles += [PSCustomObject]@{
|
||||||
OriginalPath = $item.WebUrl
|
OriginalPath = $item.WebUrl
|
||||||
OriginalFileName = $item.Name
|
OriginalFileName = $item.Name
|
||||||
|
TempPath = "$($script:txtTempLib.Text)/$($item.Name)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -394,6 +396,11 @@ function Get-XlsFilesCurrentFolder {
|
|||||||
|
|
||||||
function Convert-Files {
|
function Convert-Files {
|
||||||
try {
|
try {
|
||||||
|
# Ensure the Testing library exists before proceeding
|
||||||
|
if (-not (Ensure-TestingLibraryExists)) {
|
||||||
|
throw "Failed to ensure Testing library exists"
|
||||||
|
}
|
||||||
|
|
||||||
# Get site and drive information first
|
# Get site and drive information first
|
||||||
$script:txtStatus.Text += "Getting SharePoint site information...`n"
|
$script:txtStatus.Text += "Getting SharePoint site information...`n"
|
||||||
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
||||||
@ -641,19 +648,25 @@ function Convert-Files {
|
|||||||
throw "Converted file not found at: $xlsxPath"
|
throw "Converted file not found at: $xlsxPath"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get the folder path from the original file
|
# Get the destination folder from the TempLibrary field
|
||||||
$folderPath = $script:txtFolder.Text
|
$tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
||||||
$uploadFileName = $xlsxFileName # Use the converted file name
|
"Documents"
|
||||||
|
} else {
|
||||||
# Construct the upload URI for the original library
|
$script:txtTempLib.Text
|
||||||
$uploadPath = if ($folderPath) {
|
|
||||||
# Extract the relative path if needed
|
|
||||||
$relativePath = $folderPath -replace "/sites/tax/Shared Documents/", ""
|
|
||||||
"$relativePath/$uploadFileName" # Include folder structure
|
|
||||||
} else {
|
|
||||||
$uploadFileName # Just use the filename to save in root
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Get the drive for the temp library
|
||||||
|
$tempDrive = $drives | Where-Object { $_.Name -eq $tempLibrary }
|
||||||
|
if (-not $tempDrive) {
|
||||||
|
throw "Temp library not found: $tempLibrary"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Use TempPath from the JSON configuration for the upload path
|
||||||
|
$uploadFileName = $xlsxFileName # Use the converted file name
|
||||||
|
|
||||||
|
# Construct the upload URI for the temp library, properly encoding the path
|
||||||
|
$uploadPath = $uploadFileName # Just use the filename to save in root
|
||||||
|
|
||||||
# URL encode the path but preserve forward slashes and encode spaces as %20
|
# URL encode the path but preserve forward slashes and encode spaces as %20
|
||||||
$encodedPath = $uploadPath.Split('/') |
|
$encodedPath = $uploadPath.Split('/') |
|
||||||
ForEach-Object {
|
ForEach-Object {
|
||||||
@ -663,7 +676,7 @@ function Convert-Files {
|
|||||||
|
|
||||||
# Fix the URI construction to avoid the colon issue
|
# Fix the URI construction to avoid the colon issue
|
||||||
$baseUri = "https://graph.microsoft.com/v1.0/drives"
|
$baseUri = "https://graph.microsoft.com/v1.0/drives"
|
||||||
$driveId = $script:driveId
|
$driveId = $tempDrive.Id
|
||||||
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
|
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
|
||||||
|
|
||||||
$script:txtStatus.Text += "Uploading to: $uploadUri`n"
|
$script:txtStatus.Text += "Uploading to: $uploadUri`n"
|
||||||
@ -674,7 +687,7 @@ 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 library: /sites/tax/$libraryName/$uploadPath`n"
|
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadFileName`n"
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
||||||
@ -767,9 +780,7 @@ function Move-Files {
|
|||||||
$originalPath = $file.OriginalPath
|
$originalPath = $file.OriginalPath
|
||||||
$originalFileName = $file.OriginalFileName
|
$originalFileName = $file.OriginalFileName
|
||||||
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
$xlsxFileName = $originalFileName -replace "\.xls[m]?$", ".xlsx"
|
||||||
|
$tempPath = $xlsxFileName # Remove the ConvertedFiles folder prefix
|
||||||
# Use the full TempPath to maintain folder structure
|
|
||||||
$tempPath = $file.TempPath.Replace(".xls", ".xlsx")
|
|
||||||
|
|
||||||
$script:txtStatus.Text += "Moving file: $xlsxFileName`n"
|
$script:txtStatus.Text += "Moving file: $xlsxFileName`n"
|
||||||
|
|
||||||
@ -778,16 +789,8 @@ function Move-Files {
|
|||||||
$sourceUri = "https://graph.microsoft.com/v1.0/drives/$($tempDrive.Id)/root:/" +
|
$sourceUri = "https://graph.microsoft.com/v1.0/drives/$($tempDrive.Id)/root:/" +
|
||||||
[System.Web.HttpUtility]::UrlEncode($tempPath).Replace("+", "%20")
|
[System.Web.HttpUtility]::UrlEncode($tempPath).Replace("+", "%20")
|
||||||
|
|
||||||
# For the destination, we need to extract the folder path
|
|
||||||
$folderPath = $script:txtFolder.Text
|
|
||||||
$destinationPath = if ($folderPath) {
|
|
||||||
"$folderPath/$xlsxFileName"
|
|
||||||
} else {
|
|
||||||
$xlsxFileName
|
|
||||||
}
|
|
||||||
|
|
||||||
$destinationUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/" +
|
$destinationUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/" +
|
||||||
[System.Web.HttpUtility]::UrlEncode($destinationPath).Replace("+", "%20")
|
[System.Web.HttpUtility]::UrlEncode($originalFileName).Replace("+", "%20")
|
||||||
|
|
||||||
$script:txtStatus.Text += "Source URI: $sourceUri`n"
|
$script:txtStatus.Text += "Source URI: $sourceUri`n"
|
||||||
$script:txtStatus.Text += "Destination URI: $destinationUri`n"
|
$script:txtStatus.Text += "Destination URI: $destinationUri`n"
|
||||||
@ -795,9 +798,8 @@ function Move-Files {
|
|||||||
# Move the file
|
# Move the file
|
||||||
$moveResponse = Invoke-MgGraphRequest -Uri $sourceUri -Method PATCH -Body @{
|
$moveResponse = Invoke-MgGraphRequest -Uri $sourceUri -Method PATCH -Body @{
|
||||||
parentReference = @{
|
parentReference = @{
|
||||||
path = "/drives/$($drive.Id)/root:/$folderPath"
|
path = "/drives/$($drive.Id)/root:/$($script:txtFolder.Text)"
|
||||||
}
|
}
|
||||||
name = $xlsxFileName
|
|
||||||
} -ContentType "application/json"
|
} -ContentType "application/json"
|
||||||
|
|
||||||
$script:txtStatus.Text += "Successfully moved file: $xlsxFileName`n"
|
$script:txtStatus.Text += "Successfully moved file: $xlsxFileName`n"
|
||||||
@ -815,3 +817,140 @@ function Move-Files {
|
|||||||
$script:txtStatus.Text += "Stack Trace: $($_.Exception.StackTrace)`n"
|
$script:txtStatus.Text += "Stack Trace: $($_.Exception.StackTrace)`n"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Function to ensure the temp library exists
|
||||||
|
function Ensure-TestingLibraryExists {
|
||||||
|
try {
|
||||||
|
# Get the library name from TempLibrary field
|
||||||
|
$tempLibraryName = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
||||||
|
"Documents"
|
||||||
|
} else {
|
||||||
|
$script:txtTempLib.Text
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "Checking if $tempLibraryName library exists...`n"
|
||||||
|
} else {
|
||||||
|
Write-Host "Checking if $tempLibraryName library exists..."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get site and drive information
|
||||||
|
$site = Invoke-MgGraphRequest -Method GET -Uri "https://graph.microsoft.com/v1.0/sites/sutterhill.sharepoint.com:/sites/tax" -ErrorAction Stop
|
||||||
|
|
||||||
|
if (-not $site) {
|
||||||
|
throw "Could not find SharePoint site"
|
||||||
|
}
|
||||||
|
|
||||||
|
$drives = Get-MgSiteDrive -SiteId $site.id
|
||||||
|
$tempLibrary = $drives | Where-Object { $_.Name -eq $tempLibraryName }
|
||||||
|
|
||||||
|
if (-not $tempLibrary) {
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "$tempLibraryName library not found. Creating library...`n"
|
||||||
|
} else {
|
||||||
|
Write-Host "$tempLibraryName library not found. Creating library..."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Create the library
|
||||||
|
$createLibraryUri = "https://graph.microsoft.com/v1.0/sites/$($site.id)/lists"
|
||||||
|
$body = @{
|
||||||
|
displayName = $tempLibraryName
|
||||||
|
list = @{
|
||||||
|
template = "documentLibrary"
|
||||||
|
}
|
||||||
|
} | ConvertTo-Json
|
||||||
|
|
||||||
|
Invoke-MgGraphRequest -Uri $createLibraryUri -Method POST -Body $body -ContentType "application/json"
|
||||||
|
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "$tempLibraryName library created successfully.`n"
|
||||||
|
} else {
|
||||||
|
Write-Host "$tempLibraryName library created successfully."
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "$tempLibraryName library already exists.`n"
|
||||||
|
} else {
|
||||||
|
Write-Host "$tempLibraryName library already exists."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $true
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "Error ensuring library exists: $($_.Exception.Message)`n"
|
||||||
|
} else {
|
||||||
|
Write-Host "Error ensuring library exists: $($_.Exception.Message)"
|
||||||
|
}
|
||||||
|
return $false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function Upload-ConvertedFile {
|
||||||
|
param (
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string]$xlsxPath,
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string]$uploadFolder,
|
||||||
|
[Parameter(Mandatory=$true)]
|
||||||
|
[string]$uploadFileName
|
||||||
|
)
|
||||||
|
|
||||||
|
try {
|
||||||
|
# Verify file exists before trying to read it
|
||||||
|
if (-not (Test-Path -LiteralPath $xlsxPath)) {
|
||||||
|
throw "Converted file not found at: $xlsxPath"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Get the destination folder from the TempLibrary field
|
||||||
|
$tempLibrary = if ($script:txtTempLib.Text -eq "Shared Documents") {
|
||||||
|
"Documents"
|
||||||
|
} 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, properly encoding the path
|
||||||
|
$uploadPath = $uploadFileName # Just use the filename to save in root
|
||||||
|
|
||||||
|
# URL encode the path but preserve forward slashes and encode spaces as %20
|
||||||
|
$encodedPath = $uploadPath.Split('/') |
|
||||||
|
ForEach-Object {
|
||||||
|
[System.Web.HttpUtility]::UrlEncode($_).Replace("+", "%20")
|
||||||
|
} |
|
||||||
|
Join-String -Separator '/'
|
||||||
|
|
||||||
|
# Fix the URI construction to avoid the colon issue
|
||||||
|
$baseUri = "https://graph.microsoft.com/v1.0/drives"
|
||||||
|
$driveId = $tempDrive.Id
|
||||||
|
$uploadUri = "${baseUri}/${driveId}/root:/${encodedPath}:/content"
|
||||||
|
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "Uploading to: $uploadUri`n"
|
||||||
|
}
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "Successfully uploaded to temp library: /sites/tax/$tempLibrary/$uploadFolder/$uploadFileName`n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
if ($script:txtStatus) {
|
||||||
|
$script:txtStatus.Text += "Error uploading file: $($_.Exception.Message)`n"
|
||||||
|
} else {
|
||||||
|
Write-Host "Error uploading file: $($_.Exception.Message)"
|
||||||
|
}
|
||||||
|
throw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user