Sharing a folder and setting permissions in PowerShell

放肆的年华 提交于 2020-01-01 07:40:03

问题


I need a script to run on Vista Ultimate to share an external drive and assign full control to Everyone. I've got a batch file to create the share using net share, but there doesn't seem to be a way to change the permissions. I reckon this must be possible in PowerShell, but I have no idea where to start.


回答1:


Two answers.

In PowerShell, the Get-ACL cmdlet will retrieve the existing permissions. You then modify those using .NET commands, and run Set-ACL to apply it back to the folder - the help for these two cmdlets includes examples, and you can download the book examples from www.sapienpress.com for "Windows PowerShell: TFM" = the book also contains explicit examples.

However, it is not worth your time. Practically speaking, file ACLs are a royal pain to deal with and incredibly complicated. Microsoft has already written lovely tools to do this, like Cacls, and it's far easier just to use those.

Now that's all FILE permissions - you may also be interested in changing the permissions on the SHARE itself. The tool for that is SUBINACL, and you can download it from Microsoft. See also http://cwashington.netreach.net/depo/view.asp?Index=1127&ScriptType=vbscript.




回答2:


In case you're searching for an answer to this question, but you're running Windows 7 (instead of Vista), as I was, you might be interested to know that permissions can be set in the NET SHARE command, now, directly.

For instance,

NET SHARE Movies=M:\Movies /GRANT:Everyone`,READ

will create a share and give Everyone read-only permissions to it.

Instead of READ, you can use CHANGE or FULL as well.




回答3:


As of WMF 4:

New-SmbShare –Name ShareName –Path C:\LocalFolder –FullAccess Username

http://technet.microsoft.com/en-us/library/jj635722.aspx




回答4:


The Carbon PowerShell module has two functions that will do this for you: Install-SmbShare and Grant-Permission. I would download it and give it a try.

Install-SmbShare -Name MyShare -Path X:\ -FullAccess 'Everybody' `
                 -Description 'My super-awesome file share!' 
Grant-Permission -Identity Everyone -Permission FullControl -Path X:\

Disclaimer: I am the author and creator of Carbon. I recommend using this module because there are a lot of caveats and potholes to look out for when installing shares and granting permissions. These two function take care of that for you.



来源:https://stackoverflow.com/questions/399971/sharing-a-folder-and-setting-permissions-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!