Rudis
Rudis

Reputation: 1217

Sharing in Win2012 via PowerShell

I'm newbie in PowerShell and I need create script, which create folder on Windows Server 2012 and set it as share with permission: Everyone Read, NETWORK SERVICE Full.

I wrote following script

Import-Module SmbShare
Import-Module SmbWitness

function CreateFolderIfNotExists([string]$Path)
{
    if (!(Test-Path $Path))
    {
        New-Item -ItemType Directory -Force -Path $Path
        return $True
    }

    return $False
}

if (CreateFolderIfNotExists -Path "C:\DataExport")
{
    New-SmbShare –Name "DataExport" –Path "C:\DataExport" –FullAccess "NETWORK SERVICE" –ReadAccess "Everyone"
}

The script create folder, enable sharing, but set up folder permissions and not set up sharing permissions - it means, that if I display properties of DataExport folder, tab Sharing. If I use Advanced Sharing ... button and Permissions button, I see permissions correctly, but if I use Share... button, permissions aren't set.

If I try to connect to shared folder, I don't have permissions to access to share folder.

Is there some way how to set sharing of folder?

Upvotes: 1

Views: 1947

Answers (2)

MatthewP
MatthewP

Reputation: 141

cacls is a good tool, but it sets file permissions (shown on the Security tab when you right click on a file or folder), not share permissions. You should understand that a user or group's effective permission on a share is the more restrictive of the two. So if you give a user Change in the share permissions, but only Read/Execute on the file system, that user's effective permissions are Read/Execute in that area.

If you liked working with cacls, rmtshare.exe is another deprecated tool that can do what you want. cacls will set the file permissions and rmtshare will deal with the share. You will have to look around to find it, at my work we use an older copy from the resource kit.

Example rmtshare usage:

 rmtshare \\servername\sharename=C:\Websites\wwwroot /remark:"Website Share"
 rmtshare \\servername\sharename /grant Administrators:F /grant system:f /remove everyone
 rmtshare \\servername\sharename /grant "servername\Local Website Users":C
 rmtshare \\servername\sharename /grant "servername\Local Website Viewers":R

Upvotes: 0

Rudis
Rudis

Reputation: 1217

Finaly I found tool CACLS (which is currently deprecated, but works) and via this tool I can set sharing permissions:

function GrantSharePermission([string]$Path, [string]$User, [string]$Permission)
{
    $Command = "cacls " + $Path + " /G " + $User + ":" + $Permission + " /T /E"
    iex $Command
}

Upvotes: 1

Related Questions