Skip to content

Commit b886451

Browse files
authored
Update create-user-and-share.ps1
1 parent 89e6b38 commit b886451

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

create-user-and-share.ps1

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,7 @@ if ($existingShare) {
7777
# Create the share. Deny access to "Everyone" otherwise it will be accessible by default.
7878
Write-Output "Sharing the folder '$folderPath' as '$shareName'. without any user permissions."
7979
New-SmbShare -Name $shareName -Path $folderPath -NoAccess "Everyone"
80-
# Define variables
81-
$ShareName = "NewShare"
82-
$SharePath = "C:\Path\To\Share"
8380

84-
# Create the folder if it doesn't exist
85-
if (-Not (Test-Path $SharePath)) {
86-
New-Item -ItemType Directory -Path $SharePath -Force
87-
}
88-
89-
# Create the share
90-
New-SmbShare -Name $ShareName -Path $SharePath -Description "A restricted share with no default access" -FullAccess ""
91-
92-
# Remove "Everyone" access
93-
Revoke-SmbShareAccess -Name $ShareName -AccountName "Everyone"
94-
95-
# Verify permissions
96-
Get-SmbShareAccess -Name $ShareName
9781
# Grant the user read and write access to the share
9882
Write-Output "Granting read and write access to user '$username' for share '$shareName'."
9983
Grant-SmbShareAccess -Name $shareName -AccountName $username -AccessRight Change -Confirm:$false
@@ -102,3 +86,27 @@ Get-SmbShareAccess -Name $ShareName
10286
# Prevent the window from closing after the program ends
10387
Write-Host "Press any key to close this window..."
10488
[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."

0 commit comments

Comments
 (0)