Skip to content

Commit 0c82a08

Browse files
committed
adjust permissions for mounting share
1 parent e556a29 commit 0c82a08

File tree

1 file changed

+64
-22
lines changed

1 file changed

+64
-22
lines changed

create-user-and-share.ps1

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ if ($userExists) {
3030

3131
# Create the new user
3232
Write-Output "Creating User '$username'."
33-
New-LocalUser -Name $username -Password $password -FullName "VanderStack Share User" -Description "User for vanderstack-share access" -Confirm:$false
33+
New-LocalUser -Name $username -Password $password -FullName "VanderStack Share User" -Description "User for $shareName access" -Confirm:$false
3434

3535
# Get the user object from local users
3636
$userExists = Get-LocalUser -Name $username -ErrorAction SilentlyContinue
@@ -44,6 +44,7 @@ if ($userExists) {
4444
Add-LocalGroupMember -Group "Users" -Member $username
4545

4646
# Disable the user's ability to log in interactively by setting their account to disabled
47+
# Account being disabled does not prevent mounting or file access with NTFS and share access to Users
4748
Write-Output "Setting User '$username' account status to disabled to prevent login."
4849
Disable-LocalUser -Name $username
4950

@@ -67,37 +68,56 @@ if (-Not (Test-Path -Path $folderPath)) {
6768
$acl = Get-Acl -Path $folderPath
6869

6970
# Disable NTFS access permissions inheritance and do not copy the existing permissions
71+
Write-Host "Disabling access control inheritance. Access will require an explicitly allow rule."
7072
$acl.SetAccessRuleProtection($true, $false)
7173

72-
# Create access rule for local users granting read/write access to the folder
74+
$FullControl = [System.Security.AccessControl.FileSystemRights]::FullControl
75+
$ReadWrite = $FullControl -band (-bnot [System.Security.AccessControl.FileSystemRights]::ExecuteFile)
76+
77+
# Define inheritance and propagation flags
78+
$InheritanceFlags = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [System.Security.AccessControl.InheritanceFlags]::ObjectInherit
79+
$PropagationFlags = [System.Security.AccessControl.PropagationFlags]::None
80+
$AccessControlType = [System.Security.AccessControl.AccessControlType]::Allow
81+
82+
# this is not required to mount or make changes
83+
# $usernameAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
84+
# "$username",
85+
# $FullControl,
86+
# $InheritanceFlags,
87+
# $PropagationFlags,
88+
# $AccessControlType
89+
# )
90+
#
91+
# $acl.SetAccessRule($usernameAccessRule)
92+
# Write-Host "Granted $($usernameAccessRule.FileSystemRights) access for $($usernameAccessRule.IdentityReference) to '$folderPath'."
93+
94+
# Without Read/Write access for Users touch results in permission denied
7395
$usersAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
74-
"Users", # group the rule applies to
75-
"ReadData, WriteData", # grant read and write permissions
76-
"ContainerInherit,ObjectInherit", # apply permissions to subfolders and files
77-
"None", # no specific flags for the rule
78-
"Allow" # rule type is allow rather than deny
96+
"Users",
97+
$ReadWrite,
98+
$InheritanceFlags,
99+
$PropagationFlags,
100+
$AccessControlType
79101
)
80102

81-
# Add access to Users
82-
$acl.SetAccessRule($usersAccessRule)
103+
# without this rule mount has permissions but touching a file results in permission denied
104+
# $acl.SetAccessRule($usersAccessRule)
105+
Write-Host "Granted $($usersAccessRule.FileSystemRights) access for $($usersAccessRule.IdentityReference) to '$folderPath'."
83106

84107
# Create access rule for Administrators granting full access to the folder
85108
$adminAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
86-
"Administrators", # group the rule applies to
87-
"FullControl", # grant read and write permissions
88-
"ContainerInherit,ObjectInherit", # apply permissions to subfolders and files
89-
"None", # no specific flags for the rule
90-
"Allow" # rule type is allow rather than deny
109+
"Administrators",
110+
$FullControl,
111+
$InheritanceFlags,
112+
$PropagationFlags,
113+
$AccessControlType
91114
)
92115

93-
# Add access to Users
94116
$acl.SetAccessRule($adminAccessRule)
117+
Write-Host "Granted $($adminAccessRule.FileSystemRights) for $($adminAccessRule.IdentityReference) to '$folderPath'."
95118

96119
# Update NTFS access rules
97120
Set-Acl -Path $folderPath -AclObject $acl
98-
Write-Host "Granted Read/Write access for 'Users' (local only) to '$folderPath'."
99-
Write-Host "Granted Full Control for 'Administrators' to '$folderPath'."
100-
101121
} else {
102122
Write-Host "The folder '$folderPath' already exists."
103123
}
@@ -110,10 +130,32 @@ if ($existingShare) {
110130

111131
} else {
112132

113-
# Share the folder with the group "Users" having read/write
114-
Write-Output "Sharing the folder '$folderPath' as '$shareName'. with Read/Write granted to Users."
115-
New-SmbShare -Name $shareName -Path $folderPath -ChangeAccess "Users"
116-
Write-Host "Folder '$folderPath' shared as '$shareName' with 'Users' granting Read/Write control."
133+
Write-Output "Sharing the folder '$folderPath' as '$shareName'."
134+
New-SmbShare -Name $shareName -Path $folderPath
135+
136+
# $usernameShareRule = @{
137+
# Name = $shareName
138+
# AccountName = $username
139+
# AccessRight = "Full"
140+
# }
141+
142+
$usersShareRule = @{
143+
Name = $shareName
144+
AccountName = "Users"
145+
AccessRight = "Change"
146+
}
147+
148+
# this is not required to mount or make changes
149+
# Write-Host "Granting $($usernameShareRule.AccountName) $($usernameShareRule.AccessRight) access to '$shareName'."
150+
# Grant-SmbShareAccess @usernameShareRule -Confirm:$false
151+
152+
# Without this rule mount results in permission denied
153+
# Without this rule granting change touch results in permission denied
154+
# Full is not required to read/write/delete/list directory contents
155+
Write-Host "Granting $($usersShareRule.AccountName) $($usersShareRule.AccessRight) access to '$shareName'."
156+
Grant-SmbShareAccess @usersShareRule -Confirm:$false
157+
158+
Revoke-SmbShareAccess -Name $shareName -AccountName "Everyone" -Confirm:$false
117159
}
118160

119161
# Prevent the window from closing after the program ends

0 commit comments

Comments
 (0)