151 lines
4.8 KiB
PowerShell
151 lines
4.8 KiB
PowerShell
$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 {
|
|
try {
|
|
$siteUrl = $script:txtSiteUrl.Text
|
|
|
|
if (-not $siteUrl) {
|
|
throw "Please enter a Site URL"
|
|
}
|
|
|
|
# Simplified interactive auth
|
|
Connect-PnPOnline -Url $siteUrl -Interactive `
|
|
-WarningAction SilentlyContinue
|
|
|
|
return $true
|
|
}
|
|
catch {
|
|
$script:txtStatus.Text += "Connection Error: $($_.Exception.Message)`n"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
function List-XlsFiles {
|
|
if (-not (Connect-SharePoint)) {
|
|
return
|
|
}
|
|
|
|
$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
|
|
|
|
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 -SiteUrl "https://yoursite.sharepoint.com")) {
|
|
return
|
|
}
|
|
|
|
$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 -SiteUrl "https://yoursite.sharepoint.com")) {
|
|
return
|
|
}
|
|
|
|
$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 {
|
|
# Verify library exists and is accessible
|
|
$library = Get-PnPList -Identity $script:txtProdLib.Text -Includes RootFolder,HasUniqueRoleAssignments
|
|
if ($library.HasUniqueRoleAssignments) {
|
|
throw "This library has unique permissions - verify app has access"
|
|
}
|
|
|
|
# Get folders with elevated permissions
|
|
$ctx = Get-PnPContext
|
|
$ctx.Load($library.RootFolder.Folders)
|
|
$ctx.ExecuteQuery()
|
|
|
|
return $library.RootFolder.Folders |
|
|
Select-Object Name, ServerRelativeUrl |
|
|
Out-GridView -Title "Select Folder" -PassThru
|
|
}
|
|
catch {
|
|
$script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n"
|
|
return $null
|
|
}
|
|
}
|