@@ -32,19 +32,19 @@ if ($userExists) {
3232 Write-Output " Creating User '$username '."
3333 New-LocalUser - Name $username - Password $password - FullName " VanderStack Share User" - Description " User for vanderstack-share access" - Confirm:$false
3434
35- # Confirm user creation
3635 # Get the user object from local users
3736 $userExists = Get-LocalUser - Name $username - ErrorAction SilentlyContinue
3837
38+ # Confirm user creation
3939 if ($userExists ) {
4040 Write-Output " User '$username ' has been created successfully."
4141
4242 # Add the user to the 'Users' group
4343 Write-Output " Adding User '$username ' to the Users group."
4444 Add-LocalGroupMember - Group " Users" - Member $username
4545
46- Write-Output " Setting User '$username ' account status to disabled to prevent login."
4746 # Disable the user's ability to log in interactively by setting their account to disabled
47+ Write-Output " Setting User '$username ' account status to disabled to prevent login."
4848 Disable-LocalUser - Name $username
4949
5050 } else {
@@ -62,6 +62,42 @@ if (-Not (Test-Path -Path $folderPath)) {
6262 Write-Host " The folder '$folderPath ' does not exist. Creating it now..."
6363 New-Item - Path $folderPath - ItemType Directory - Force | Out-Null
6464 Write-Host " Folder created successfully."
65+
66+ # Get NTFS access rules
67+ $acl = Get-Acl - Path $folderPath
68+
69+ # Disable NTFS access permissions inheritance and do not copy the existing permissions
70+ $acl.SetAccessRuleProtection ($true , $false )
71+
72+ # Create access rule for local users granting read/write access to the folder
73+ $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
79+ )
80+
81+ # Add access to Users
82+ $acl.SetAccessRule ($usersAccessRule )
83+
84+ # Create access rule for Administrators granting full access to the folder
85+ $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
91+ )
92+
93+ # Add access to Users
94+ $acl.SetAccessRule ($adminAccessRule )
95+
96+ # Update NTFS access rules
97+ 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+
65101} else {
66102 Write-Host " The folder '$folderPath ' already exists."
67103}
@@ -74,39 +110,12 @@ if ($existingShare) {
74110
75111} else {
76112
77- # Create the share. Deny access to "Everyone" otherwise it will be accessible by default.
78- Write-Output " Sharing the folder '$folderPath ' as '$shareName '. without any user permissions."
79- New-SmbShare - Name $shareName - Path $folderPath - NoAccess " Everyone"
80-
81- # Grant the user read and write access to the share
82- Write-Output " Granting read and write access to user '$username ' for share '$shareName '."
83- Grant-SmbShareAccess - Name $shareName - AccountName $username - AccessRight Change - Confirm:$false
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."
84117}
85118
86119# Prevent the window from closing after the program ends
87120Write-Host " Press any key to close this window..."
88- [void ][System.Console ]::ReadKey()
89-
90-
91- # Define folder path and share name
92- $folderPath = " C:\foo"
93- $shareName = " foo"
94-
95- # Create the folder if it doesn't exist
96- if (-Not (Test-Path - Path $folderPath )) {
97- New-Item - Path $folderPath - ItemType Directory | Out-Null
98- Write-Host " Folder '$folderPath ' created."
99- } else {
100- Write-Host " Folder '$folderPath ' already exists."
101- }
102-
103- # Grant "Everyone" full access to the folder
104- $acl = Get-Acl - Path $folderPath
105- $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(" Everyone" , " FullControl" , " ContainerInherit,ObjectInherit" , " None" , " Allow" )
106- $acl.SetAccessRule ($accessRule )
107- Set-Acl - Path $folderPath - AclObject $acl
108- Write-Host " Granted 'Everyone' full access to '$folderPath '."
109-
110- # Share the folder with "Everyone" having full access
111- New-SmbShare - Name $shareName - Path $folderPath - FullAccess " Everyone"
112- Write-Host " Folder '$folderPath ' shared as '$shareName ' with 'Everyone' full access."
121+ [void ][System.Console ]::ReadKey()
0 commit comments