-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSetDevDriveOwner.ps1
More file actions
98 lines (77 loc) · 3.08 KB
/
Copy pathSetDevDriveOwner.ps1
File metadata and controls
98 lines (77 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function Get-DevDrive {
$devDrives = Get-Volume | Where-Object { $_.FileSystemType -eq 'ReFS' -and $_.DriveType -eq 'Fixed' }
$devDriveLetters = @()
foreach ($drive in $devDrives) {
$driveLetter = "$($drive.DriveLetter):"
Write-Host "`nDev Drive found: $driveLetter"
$devDriveLetters += $driveLetter
}
if ($devDriveLetters.Count -eq 0) {
Write-Output "No Dev Drive found on the system."
return $null
} elseif ($devDriveLetters.Count -eq 1) {
return $devDriveLetters[0]
} else {
Write-Host "Multiple Dev Drives found:"
for ($i = 0; $i -lt $devDriveLetters.Count; $i++) {
Write-Host "[$i] $($devDriveLetters[$i])"
}
$selection = Read-Host "Please select the drive you want to configure by entering the corresponding number"
if ($selection -match '^\d+$' -and [int]$selection -lt $devDriveLetters.Count) {
return $devDriveLetters[$selection]
} else {
Write-Output "Invalid selection. Exiting script."
return $null
}
}
}
function Set-DevDrivePermissions {
param (
[string]$driveLetter
)
# Ensure the drive letter is correctly formatted
if (-not $driveLetter.EndsWith(':')) {
$driveLetter += ':'
}
# Define the user
$userName = "$env:USERDOMAIN\$env:USERNAME"
# Get the current ACL (Access Control List) for the drive
$acl = Get-Acl $driveLetter
# Create a new access rule for the user with full control
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($userName, "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")
# Add the new access rule to the ACL
$acl.SetAccessRule($accessRule)
# Apply the new ACL to the drive
Set-Acl $driveLetter $acl
# Hide the drive from other users
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
$regName = "NoDrives"
$driveLetterOnly = $driveLetter.TrimEnd(':') # Get just the letter part
$driveNumber = [math]::Pow(2, ([byte][char]$driveLetterOnly) - 65)
# Set the registry key to hide the drive
If (!(Test-Path $regPath)) {
New-Item -Path $regPath -Force | Out-Null
}
Set-ItemProperty -Path $regPath -Name $regName -Value $driveNumber
Write-Output "`nDev Drive ($driveLetter) is now only accessible and visible to the user $userName."
}
function Test-DevDrivePermissions {
param (
[string]$driveLetter
)
Write-Output "`nReviewing permissions for Dev Drive ($driveLetter)..."
# Get the current ACL for the drive
$acl = Get-Acl $driveLetter
# Output the permissions
$acl.Access | Format-List | Out-Host
Write-Output "Permissions review completed."
}
# Main script execution
$devDrive = Get-DevDrive
if ($null -ne $devDrive) {
Set-DevDrivePermissions -driveLetter $devDrive
# Periodically review permissions (this could be scheduled as a task or run manually)
Test-DevDrivePermissions -driveLetter $devDrive
} else {
Write-Output "No Dev Drive was configured."
}