none
net share with permissions

    Question

  • Hi

     

    having some problem setting up a net share, I would like to setup a net share and set the permissions at the same time

    This is what i run in a powershell windows

    PS D:\dfsshares> net share dfs_company=d:\dfsshares\company /GRANT:EVERYONE,READ

    The syntax of this command is:

     

    NET SHARE

    sharename

              sharename=drive:path [/GRANT:user,[READ | CHANGE | FULL]]

                                   [/USERS:number | /UNLIMITED]

                                   [/REMARK:"text"]

                                   [/CACHE:Manual | Documents| Programs | BranchCache | None]

              sharename [/USERS:number | /UNLIMITED]

                        [/REMARK:"text"]

                        [/CACHE:Manual | Documents | Programs | BranchCache | None]

              {sharename | devicename | drive:path} /DELETE

              sharename \\computername /DELETE

     

    Syntax looks okay to me

     

    Alex

    Wednesday, November 16, 2011 3:57 AM

Answers

  • This is basically the comma being parsed by PowerShell and causing NET.EXE not to get the correct parameters.

    You can try using the ` character to scape out the comma. Try this in PowerShell:

    net share dfs_company=d:\dfsshares\company /GRANT:EVERYONE`,READ


    Jose Barreto
    • Marked as answer by Alex Samad Wednesday, November 16, 2011 11:02 PM
    Wednesday, November 16, 2011 5:17 PM

All replies

  • is this because I am running this in a powershell window and not a cmd windows... ?

     

    wow is me. So for any one looking at this, the problem is powershell is striping away the " before passing it to the program

     

    you can stop that by using ` so `" will escape out the "

     

    Now if somebody can help with how i can authenticated users here that would be helpful

    • Edited by Alex Samad Wednesday, November 16, 2011 5:59 AM
    Wednesday, November 16, 2011 5:46 AM
  • This is basically the comma being parsed by PowerShell and causing NET.EXE not to get the correct parameters.

    You can try using the ` character to scape out the comma. Try this in PowerShell:

    net share dfs_company=d:\dfsshares\company /GRANT:EVERYONE`,READ


    Jose Barreto
    • Marked as answer by Alex Samad Wednesday, November 16, 2011 11:02 PM
    Wednesday, November 16, 2011 5:17 PM
  • Thanks, solved the problem for me!
    Tuesday, February 26, 2013 8:34 AM
  • But How can I run this command against remote servers?

    net share dfs_company=d:\dfsshares\company /GRANT:EVERYONE`,READ

    $servers = Get-Content .\dfsservername.txt | %{$_ }

    $DFSRoots = Import-Csv .\DFSRoot.csv | % {$_.DFSRoot}

    Invoke-Command -ComputerName $servers {NET SHARE $DFSRoots="C:\DFSRoots\$DFSRoots" /GRANT:EVERYONE`,READ}

     I tried this way but did not get success..


    • Edited by Mr. Raj Wednesday, September 23, 2015 5:14 PM
    • Proposed as answer by JaymeeH Friday, January 15, 2016 5:43 PM
    • Unproposed as answer by JaymeeH Friday, January 15, 2016 5:43 PM
    Wednesday, September 23, 2015 5:13 PM
  • In response to Mr. Raj:

    "But How can I run this command against remote servers?"

    You could try using PSEXEC (from the PSTOOLS suite) to actually invoke CMD.exe (or possibly powershell) on the remote machine and then execute the net share command in that remote shell. However, that won't help you if you are trying to script it.

    Thursday, January 14, 2016 2:10 AM
  • I have been struggling with this issue (damn you Server 2008 and your lack of SMB cmdlets!) and this was a royal pain to work out. Powershell needs the escape on the comma but also needs each option to be in escaped double quotes. 

    $u1 = "UserName1"
    $u2 = "UserName2"
    $desc = "This is a description"

    $b = "NET SHARE TEST=C:\Temp\Test `"/GRANT:DOMAIN\$u1`,CHANGE`" `"/GRANT:DOMAIN\$u2`,FULL`" `"/REMARK:$desc`""

    $sb = [scriptblock]::Create($b)

    Invoke-Command -ComputerName 'FP-01' -ScriptBlock $sb


    • Proposed as answer by JaymeeH Friday, January 15, 2016 5:43 PM
    • Edited by JaymeeH Monday, January 18, 2016 10:31 AM Clarity
    Friday, January 15, 2016 5:43 PM
  • I came up with a few issues when dealing with share names with spaces in them. You have to treat each option as a whole and put double quotes around each of them. Here's a working function that handles spaces in any of the options:

    function New-SMBSharePre2012 ($Server, $ShareName, $Path, $ACLFull, $ACLChange, $ACLRead, $Description){
    #Remotely creates a share on a pre-2012 server and grants share access
    #Builds the command line to give access to multipe groups/users at once. 
    
        #Beginning of command line with name of share and local path 
        $cmd = "NET SHARE `"$ShareName=$Path`""
        #Loop through the acess rights
        for ($i = 1; $i -lt 4; $i++){ 
            switch ($i)
            {
                1 {$currACL = $ACLFull; $access = 'FULL'}
                2 {$currACL = $ACLChange; $access = 'CHANGE'}
                3 {$currACL = $ACLRead; $access = 'READ'}
            }
            #append the rights to the command line
            foreach($g in $currACL){$cmd += " `"/GRANT:$g`,$access`""}
        }
        #End of line including description
        $cmd += " `"/REMARK:$description`""
    
        Write-Host "NET SHARE COMMAND LINE: $cmd"
        
            #Create Script Block
        $sb = [scriptblock]::Create($cmd)
    
        #Remotely invoke the command line
        Invoke-Command -ComputerName $Server -ScriptBlock $sb
    
    }
    
    
    $ACLFull = "DOMAIN\ADM_IT-Admins", "DOMAIN\DomainAdmins"
    $Server = 'FP-01'
    $ShareName = "Share Folder Name" 
    $Path = "D:\Folder\Folder Name" 
    $ACLChange = "STJH\SMB_TEAM_Folder Name"
    $ACLRead = $null
    $Description = "This is a Description"
    
    New-SMBSharePre2012 $Server $ShareName $Path $ACLFull $ACLChange $ACLRead $Description


    • Edited by JaymeeH Friday, January 22, 2016 1:20 PM
    Friday, January 22, 2016 1:19 PM