Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

How to get/set Share permissions with PowerShell

$
0
0

I have a file server with a bunch of Shares and need to get/set Share perms and inventory stuff.  I've looked at other threads on this forum but there seems to be more than one way to do it.

What I'm trying to do is export to .csv all share names and perms so they can be viewed/edited in Excel, and then use the edited .csv file as input to another script to modify Share perms.

Here's what I've got so far:

$root = "F:\RootDir"
$fldr = Get-ChildItem "$root\*" | Where-Object { $_.PSIsContainer } | Sort $_.PSIsContainer.Name
foreach ($f in $fldr) {
    $acl = get-acl $f.FullName | foreach { $_.Access }
    # do stuff with ACLs
}

The above code seems to enumerate the Shares okay, but what about this:

    $shares = @()
    Get-WmiObject win32_share |
        ForEach-Object { $shares += $_.name.tostring() }
    $shares | Out-String

Are these two blocks of code equivalent?  Why would I use one over the other to enumerate Shares?

And then there's this code from the Hey Scripting Guy Blog

 PROCESS { 
   $input 
   $query = "Associators of {win32_LogicalShareSecuritySetting='$share'}  
   Where resultclass = win32_sid" 
   Get-WmiObject -query $query @RemoteParam |   
   Select-Object -Property @{LABEL="User";EXPRESSION= 
    {"{0}\{1}" -f $_.ReferencedDomainName, $_.AccountName}}, SID  
   } 

which I can't get to work... the script will NOT be run remotely, but rather on the File Server with admin rights.  What does this WMI query do and how can I get it to work locally?

As for *setting* Share perms, AFAIK, I will need to call Icacls.exe from PowerShell. Something like this:

Icacls.exe /G john.smith:F

Or sometimes Everyone would have full perms to the share and NTFS perms would be set on certain subfolders in the Share.  Only the first level of subfolders within the share would be used this way.

What is the best way to get an inventory of Share/subdirectory perms into a .csv that can also be used for input to modify Share/subdirectory perms?


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>