krusty
krusty

Reputation: 103

Permissions on shared folder with PowerShell

I need to set the permissions for "Everyone" to Change and Read with PowerShell on a shared folder. I don't know how to do it, can you help me?

enter image description here

The following code works fine on Windows Server 2012. I need to run it on Windows Server 2008:

[WMICLASS]"Win32_Share"|%{$_.Create($path,$sharefoldername,0)} | Out-Null
    Grant-SmbShareAccess -name $sharefoldername -CimSession $Server -AccountName Everyone -AccessRight Change –Force | Out-Null

Upvotes: 0

Views: 2245

Answers (2)

krusty
krusty

Reputation: 103

The working answer that I found is this:

        #Username/Group to give permissions to
        $trustee = ([wmiclass]'Win32_trustee').psbase.CreateInstance()
        $trustee.Domain = $null
        $trustee.Name = "Everyone"

        #Accessmask values
        #$fullcontrol = 2032127
        $change = 1245631
        #$read = 1179785

        #Create access-list
        $ace = ([wmiclass]'Win32_ACE').psbase.CreateInstance()
        $ace.AccessMask = $change
        $ace.AceFlags = 3
        $ace.AceType = 0
        $ace.Trustee = $trustee

        #Securitydescriptor containting access
        $sd = ([wmiclass]'Win32_SecurityDescriptor').psbase.CreateInstance()
        $sd.ControlFlags = 4
        $sd.DACL = $ace
        $sd.group = $trustee
        $sd.owner = $trustee


        $share = Get-WmiObject Win32_Share -List -ComputerName $server
        $share.create($advsharing, $sharefoldername, 0, "16777216", "Description"", $sd)

Upvotes: 1

Olaf Reitz
Olaf Reitz

Reputation: 694

Take a look at the SmbShare cmdlets:

Import-Module SmbShare
Get-Command -Noun Smb*

for an existing share you can use Grand-SmbShareAccess, you can use Get-Help to get some examples how it is used.

Get-Help Grant-SmbShareAccess -Examples

Upvotes: 0

Related Questions