feat: Add Load-Config and Save-Config functions to SharePointFunctions.ps1
This commit is contained in:
parent
b6c9a15a15
commit
004a3d239e
@ -1,3 +1,41 @@
|
||||
# Load configuration from config.json
|
||||
function Load-Config {
|
||||
try {
|
||||
$configPath = "$PSScriptRoot\config.json"
|
||||
if (Test-Path $configPath) {
|
||||
$script:config = Get-Content $configPath -Raw | ConvertFrom-Json
|
||||
return $true
|
||||
} else {
|
||||
return $false
|
||||
}
|
||||
} catch {
|
||||
$script:txtStatus.Text += "Error loading configuration: $($_.Exception.Message)`n"
|
||||
return $false
|
||||
}
|
||||
}
|
||||
|
||||
# Save configuration to config.json
|
||||
function Save-Config {
|
||||
param (
|
||||
[string]$SiteUrl,
|
||||
[string]$ProductionLibrary,
|
||||
[string]$TempLibrary
|
||||
)
|
||||
try {
|
||||
$configPath = "$PSScriptRoot\config.json"
|
||||
$configData = @{
|
||||
SiteUrl = $SiteUrl
|
||||
ProductionLibrary = $ProductionLibrary
|
||||
TempLibrary = $TempLibrary
|
||||
}
|
||||
$configJson = ConvertTo-Json -InputObject $configData -Compress
|
||||
$configJson | Out-File $configPath
|
||||
$script:txtStatus.Text += "Configuration saved successfully.`n"
|
||||
} catch {
|
||||
$script:txtStatus.Text += "Error saving configuration: $($_.Exception.Message)`n"
|
||||
}
|
||||
}
|
||||
|
||||
function Get-FolderBrowser {
|
||||
param (
|
||||
[string]$ParentFolderUrl = ""
|
||||
@ -155,3 +193,87 @@ function Get-FolderBrowser {
|
||||
return $null
|
||||
}
|
||||
}
|
||||
|
||||
function Connect-SharePoint {
|
||||
return Connect-Graph
|
||||
}
|
||||
|
||||
function List-XlsFiles {
|
||||
try {
|
||||
$script:txtStatus.Text += "Connecting to SharePoint...`n"
|
||||
if (-not (Connect-SharePoint)) {
|
||||
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
|
||||
|
||||
$script:txtStatus.Text += "Searching for XLS files...`n"
|
||||
|
||||
# If a specific folder is selected, search only in that folder
|
||||
if (-not [string]::IsNullOrWhiteSpace($folderPath)) {
|
||||
$script:txtStatus.Text += "Searching in folder: $folderPath`n"
|
||||
# Get the folder ID first
|
||||
# Construct the correct URI for the folder
|
||||
# Remove the /sites/tax/Shared Documents/ part from the folder path
|
||||
$relativePath = $folderPath.Replace("/sites/tax/Shared Documents/", "").TrimStart("/")
|
||||
|
||||
# URL encode the relative path, but replace + with %20 for spaces
|
||||
$encodedPath = [System.Web.HttpUtility]::UrlEncode($relativePath).Replace("+", "%20")
|
||||
|
||||
$folderUri = "https://graph.microsoft.com/v1.0/drives/$($drive.Id)/root:/$encodedPath"
|
||||
$script:txtStatus.Text += "Folder URI: $folderUri`n"
|
||||
try {
|
||||
$folderItem = Invoke-MgGraphRequest -Method GET -Uri $folderUri -ErrorAction Stop
|
||||
} catch {
|
||||
throw "Folder not found: $($_.Exception.Message)"
|
||||
}
|
||||
$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.WebUrl
|
||||
OriginalFileName = $item.Name
|
||||
TempPath = "$($script:txtTempLib.Text)/$($item.Name)"
|
||||
}
|
||||
$script:txtStatus.Text += "Found XLS file: $($item.Name)`n"
|
||||
}
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user