feat: Add subfolder browsing to Get-FolderBrowser function
This commit is contained in:
parent
5194dc0198
commit
37ec5d328e
@ -158,6 +158,9 @@ function Move-Files {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
function Get-FolderBrowser {
|
function Get-FolderBrowser {
|
||||||
|
param (
|
||||||
|
[string]$ParentFolderUrl = ""
|
||||||
|
)
|
||||||
try {
|
try {
|
||||||
# Get site ID first
|
# Get site ID first
|
||||||
$siteUrl = $script:txtSiteUrl.Text.TrimEnd('/')
|
$siteUrl = $script:txtSiteUrl.Text.TrimEnd('/')
|
||||||
@ -188,7 +191,28 @@ function Get-FolderBrowser {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# Get root folder first
|
# Get root folder first
|
||||||
$root = Get-MgDriveRoot -DriveId $drive.Id
|
# Get root folder first
|
||||||
|
if ($ParentFolderUrl) {
|
||||||
|
# Get the folder ID first
|
||||||
|
# Construct the correct URI for the folder
|
||||||
|
# Remove the /sites/tax/Shared Documents part from the folder path
|
||||||
|
$relativePath = $ParentFolderUrl.Replace("/sites/tax/Shared Documents/", "").TrimStart("/")
|
||||||
|
|
||||||
|
# URL encode the relative path
|
||||||
|
$encodedPath = [System.Web.HttpUtility]::UrlEncode($relativePath)
|
||||||
|
|
||||||
|
$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)"
|
||||||
|
}
|
||||||
|
$root = $folderItem
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$root = Get-MgDriveRoot -DriveId $drive.Id
|
||||||
|
}
|
||||||
|
|
||||||
# Get folders using the root item's ID
|
# Get folders using the root item's ID
|
||||||
$folders = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $root.Id | Where-Object { $_.Folder }
|
$folders = Get-MgDriveItemChild -DriveId $drive.Id -DriveItemId $root.Id | Where-Object { $_.Folder }
|
||||||
@ -198,11 +222,33 @@ function Get-FolderBrowser {
|
|||||||
[PSCustomObject]@{
|
[PSCustomObject]@{
|
||||||
Name = $_.Name
|
Name = $_.Name
|
||||||
ServerRelativeUrl = "/sites/tax/Shared Documents/$($_.Name)"
|
ServerRelativeUrl = "/sites/tax/Shared Documents/$($_.Name)"
|
||||||
|
IsFolder = $true # Add a property to indicate it's a folder
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $folderObjects |
|
# Add ".." entry to go up a level if not at the root
|
||||||
Out-GridView -Title "Select Folder" -PassThru
|
if ($ParentFolderUrl) {
|
||||||
|
$parentUrl = Split-Path -Path $ParentFolderUrl -Parent
|
||||||
|
$folderObjects = @([PSCustomObject]@{
|
||||||
|
Name = ".."
|
||||||
|
ServerRelativeUrl = $parentUrl
|
||||||
|
IsFolder = $true
|
||||||
|
}) + $folderObjects
|
||||||
|
}
|
||||||
|
|
||||||
|
# Show the Out-GridView
|
||||||
|
$selected = $folderObjects | Out-GridView -Title "Select Folder" -PassThru
|
||||||
|
|
||||||
|
# If a folder is selected, browse into it
|
||||||
|
if ($selected -and $selected.IsFolder -eq $true -and $selected.Name -ne "..") {
|
||||||
|
return Get-FolderBrowser -ParentFolderUrl $selected.ServerRelativeUrl
|
||||||
|
}
|
||||||
|
elseif ($selected -and $selected.Name -eq "..") {
|
||||||
|
return Get-FolderBrowser -ParentFolderUrl $selected.ServerRelativeUrl
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return $selected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
$script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n"
|
$script:txtStatus.Text += "Folder Error: $($_.Exception.Message)`n"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user