Skip to content

Commit 950c66c

Browse files
committed
feat(FileManagement): add Find-MovedFolder and Set-BackupPrivilege functions
* Introduced `Find-MovedFolder` to locate directories at a specified path and depth. * Added `Set-BackupPrivilege` to enable or disable SeBackupPrivilege for the current process.
1 parent f02f3bd commit 950c66c

File tree

5 files changed

+125
-23
lines changed

5 files changed

+125
-23
lines changed

.github/workflows/scripts/New-Manifest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ function New-Manifest {
184184
$savepath = "$workingdirectory\modules\AdminToolbox.FileManagement"
185185
$Params = @{
186186
CompatiblePSEditions = "Desktop", "Core"
187-
FunctionsToExport = 'Get-FileManagement', 'Find-ComputersFiles', 'Get-FileOwner', 'Get-FolderSize', 'Invoke-Robocopy', 'Remove-All', 'Remove-DisabledADProfiles', 'Remove-OlderThan', 'Get-ShareReport', 'Get-FolderName', 'Get-FileName', 'Use-WSLnano', 'Get-EmptyDirectory', 'Get-LastUsedDirectory'
187+
FunctionsToExport = 'Get-FileManagement', 'Find-ComputersFiles', 'Get-FileOwner', 'Get-FolderSize', 'Invoke-Robocopy', 'Remove-All', 'Remove-DisabledADProfiles', 'Remove-OlderThan', 'Get-ShareReport', 'Get-FolderName', 'Get-FileName', 'Use-WSLnano', 'Get-EmptyDirectory', 'Get-LastUsedDirectory', 'Find-MovedFolder', 'Set-BackupPrivilege'
188188
Path = "$savepath\AdminToolbox.FileManagement.psd1"
189189
Author = "Taylor Lee"
190190
Description = "File Management Functions"

modules/AdminToolbox.FileManagement/ChangeLog.md

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,5 @@
4545
* **1.16.166** CI Maintenance Release
4646
* **1.17.0.0** Add workflow versioning
4747
* **1.17.0.1** CI Maintenance Release
48-
* **1.17.0.61** CI Maintenance Release
49-
* **1.17.0.62** CI Maintenance Release
50-
* **1.17.0.63** CI Maintenance Release
51-
* **1.17.0.64** CI Maintenance Release
52-
* **1.17.0.65** CI Maintenance Release
53-
* **1.17.0.66** CI Maintenance Release
54-
* **1.17.0.67** CI Maintenance Release
55-
* **1.17.0.68** CI Maintenance Release
56-
* **1.17.0.69** CI Maintenance Release
57-
* **1.17.0.70** CI Maintenance Release
58-
* **1.17.0.71** CI Maintenance Release
59-
* **1.17.0.72** CI Maintenance Release
60-
* **1.17.0.73** CI Maintenance Release
61-
* **1.17.0.74** CI Maintenance Release
62-
* **1.17.0.75** CI Maintenance Release
63-
* **1.17.0.76** CI Maintenance Release
64-
* **1.17.0.77** CI Maintenance Release
65-
* **1.17.0.78** CI Maintenance Release
66-
* **1.17.0.79** CI Maintenance Release
67-
* **1.17.0.80** CI Maintenance Release
68-
* **1.17.0.81** CI Maintenance Release
69-
* **1.17.0.82** CI Maintenance Release
48+
* **1.17.0.82** CI Maintenance Release
49+
* **1.18.0.0** Add Find-MovedFolder and Set-BackupPrivilege functions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<#
2+
.DESCRIPTION
3+
Finds directories at a specified path and depth, displaying their names and full paths.
4+
5+
.PARAMETER Path
6+
The root path to search for directories.
7+
8+
.PARAMETER Depth
9+
The depth to search for directories.
10+
11+
.EXAMPLE
12+
Find-MovedFolder -Path "E:\folder\topsharefolder" -Depth 2 | Out-GridView
13+
14+
.NOTES
15+
Uses Out-GridView for interactive selection.
16+
17+
.LINK
18+
https://github.com/TheTaylorLee/AdminToolbox
19+
#>
20+
21+
function Find-MovedFolder {
22+
param (
23+
[Parameter(Mandatory)]
24+
[string]$Path,
25+
[Parameter(Mandatory)]
26+
[int]$Depth
27+
)
28+
29+
begin {
30+
get-elevation
31+
Set-BackupPrivilege -Enable $true
32+
}
33+
process {
34+
Get-ChildItem $Path -Depth $Depth -Directory | Select-Object Name, FullName
35+
}
36+
end {
37+
Set-BackupPrivilege -Enable $false
38+
}
39+
}

modules/AdminToolbox.FileManagement/Public/Get-FileManagement.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ function Get-FileManagement {
44

55
Write-Host "File Functions" -ForegroundColor green
66
Write-Host "Find-ComputersFiles ..Finds queried files across 1 or more Computers" -ForegroundColor cyan
7+
Write-Host "Find-MovedFolder ..Finds directories at a specified path and depth" -ForegroundColor cyan
78
Write-Host "Get-EmptyDirectory ..Gets a list of directories with no files in them" -ForegroundColor cyan
89
Write-Host "Get-FileName ..Gets a gui for selecting a file" -ForegroundColor cyan
910
Write-Host "Get-FileOwner ..Gets CSV of file owners for a path" -ForegroundColor cyan
@@ -15,6 +16,7 @@ function Get-FileManagement {
1516
Write-Host "Remove-All ..Removes many files quickly to free up space" -ForegroundColor cyan
1617
Write-Host "Remove-DisabledADProfiles ..Removes local profiles of disabled AD users" -ForegroundColor cyan
1718
Write-Host "Remove-OlderThan ..Removes folders and files older than" -ForegroundColor cyan
19+
Write-Host "Set-BackupPrivilege ..Enables SeBackupPrivilege for the current process" -ForegroundColor cyan
1820
Write-Host "Use-WSLnano ..Use WSL to get a nano editor in PWSH" -ForegroundColor cyan
1921
Write-Host " "
2022
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<#
2+
.DESCRIPTION
3+
Enables or disables the SeBackupPrivilege for the current process.
4+
5+
SeBackupPrivilege allows file content retrieval, even if the security descriptor on the file might not grant such access. A caller with SeBackupPrivilege enabled obviates the need for any ACL-based security check.
6+
7+
.Parameter Enable
8+
Specifies whether to enable ($true) or disable ($false) the SeBackupPrivilege.
9+
10+
.EXAMPLE
11+
Set-BackupPrivilege -Enable $true
12+
13+
.Notes
14+
Requires administrative privileges.
15+
16+
.Link
17+
https://github.com/TheTaylorLee/AdminToolbox
18+
#>
19+
20+
function Set-BackupPrivilege {
21+
param (
22+
[Parameter(Mandatory = $true)]
23+
[bool]$Enable
24+
)
25+
26+
begin {
27+
get-elevation
28+
Add-Type @"
29+
using System;
30+
using System.Runtime.InteropServices;
31+
public class BackupPriv {
32+
[DllImport("advapi32.dll", SetLastError = true)]
33+
public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAll, ref TOKEN_PRIVILEGES NewState, uint BufferLength, ref TOKEN_PRIVILEGES PreviousState, out uint ReturnLength);
34+
[DllImport("kernel32.dll")]
35+
public static extern IntPtr GetCurrentProcess();
36+
[DllImport("advapi32.dll", SetLastError = true)]
37+
public static extern bool OpenProcessToken(IntPtr ProcessHandle, uint DesiredAccess, out IntPtr TokenHandle);
38+
[DllImport("advapi32.dll", SetLastError = true)]
39+
public static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
40+
[StructLayout(LayoutKind.Sequential)]
41+
public struct TOKEN_PRIVILEGES { public uint PrivilegeCount; public LUID Luid; public uint Attributes; }
42+
[StructLayout(LayoutKind.Sequential)]
43+
public struct LUID { public uint LowPart; public int HighPart; }
44+
public const uint TOKEN_ADJUST_PRIVILEGES = 0x20;
45+
public const uint TOKEN_QUERY = 0x8;
46+
public const uint SE_PRIVILEGE_ENABLED = 0x2;
47+
public const uint SE_PRIVILEGE_DISABLED = 0x0;
48+
}
49+
"@
50+
}
51+
process {
52+
$process = [BackupPriv]::GetCurrentProcess()
53+
$token = [IntPtr]::Zero
54+
$success = [BackupPriv]::OpenProcessToken($process, [BackupPriv]::TOKEN_ADJUST_PRIVILEGES -bor [BackupPriv]::TOKEN_QUERY, [ref]$token)
55+
if (-not $success) {
56+
Write-Error "Failed to open process token. Error: $([System.Runtime.InteropServices.Marshal]::GetLastWin32Error()). SeBackupPrivilege not set."
57+
}
58+
59+
$luid = New-Object BackupPriv+LUID
60+
$success = [BackupPriv]::LookupPrivilegeValue($null, "SeBackupPrivilege", [ref]$luid)
61+
if (-not $success) {
62+
Write-Error "Failed to open process token. Error: $([System.Runtime.InteropServices.Marshal]::GetLastWin32Error()). SeBackupPrivilege not set."
63+
}
64+
65+
$tp = New-Object BackupPriv+TOKEN_PRIVILEGES
66+
$tp.PrivilegeCount = 1
67+
$tp.Luid = $luid
68+
$tp.Attributes = if ($Enable) { [BackupPriv]::SE_PRIVILEGE_ENABLED } else { [BackupPriv]::SE_PRIVILEGE_DISABLED }
69+
70+
$prevTp = New-Object BackupPriv+TOKEN_PRIVILEGES
71+
$returnLength = 0
72+
$success = [BackupPriv]::AdjustTokenPrivileges($token, $false, [ref]$tp, [System.Runtime.InteropServices.Marshal]::SizeOf($prevTp), [ref]$prevTp, [ref]$returnLength)
73+
if (-not $success) {
74+
Write-Error "Failed to open process token. Error: $([System.Runtime.InteropServices.Marshal]::GetLastWin32Error()). SeBackupPrivilege not set."
75+
}
76+
}
77+
78+
end {
79+
}
80+
81+
}

0 commit comments

Comments
 (0)