Add user to a created share

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of João Fernandes João Fernandes 2 years ago.

  • Author
    Posts
  • #20819
    Profile photo of João Fernandes
    João Fernandes
    Participant

    Hi there.

    Im a very very noob on Powershell and im trying to create a share and i´m stuck on add a user to the share.

    Here is my script:

    $user= Read-Host -Prompt User
    $FolderPath = "c:\$user"

    $Shares=[WMICLASS]'WIN32_Share'

    $ShareName="$user$"

    New-Item -type directory -Path $FolderPath

    $Shares.Create($FolderPath,$ShareName,0)

    $Acl = Get-Acl $FolderPath
    $Acl.SetAccessRuleProtection($True, $False)
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('Administrators','FullControl','ContainerInherit, ObjectInherit', 'None', 'Allow')
    $Acl.AddAccessRule($rule)
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('Teste','Modify, Synchronize', "ContainerInherit, ObjectInherit", "None", "Allow")
    $Acl.AddAccessRule($rule)

    Get-Acl $FolderPath | Format-List
    Set-Acl $FolderPath $Acl

  • #20833
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hi João,

    I dont have a domain joined system nearby to test, but was able to achieve this on my workstation using the code below. Can you try this and let me know how you get on?

    $user= Read-Host -Prompt User
    $FolderPath = "c:\$user"
    
    $Shares=[WMICLASS]'WIN32_Share'
    
    $ShareName="$user$"
    
    New-Item -type directory -Path $FolderPath
    
    $Shares.Create($FolderPath,$ShareName,0)
    
    $Acl = Get-Acl $FolderPath
    $Acl.SetAccessRuleProtection($True, $False)
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('Administrators','FullControl','ContainerInherit, ObjectInherit', 'None', 'Allow')
    $Acl.AddAccessRule($rule)
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,'Modify, Synchronize', "ContainerInherit, ObjectInherit", "None", "Allow")
    $Acl.AddAccessRule($rule)
    
    Get-Acl $FolderPath | Format-List
    Set-Acl $FolderPath $Acl
    
  • #20836
    Profile photo of João Fernandes
    João Fernandes
    Participant

    Hi Tim.

    I tested on my workstation and it worked!

    But apparently the script was fine? I cant see any changes beside the variable $user on line16

    Thanks again

You must be logged in to reply to this topic.