Share via

PowerShell Command

Len Hoffman 21 Reputation points
2020-11-04T17:52:35.167+00:00

runninf the below command to get a list of shared folders on a list of servers and the ACL's of them but cannot seem to figure out how to convert the output file of the Get-WmiObject Win32_Share to format into a UNC path that the Get-ACL portion of the script can use.

Can anyone help with this?

foreach ($Computer in Get-Content c:\temp\ComputerNames.txt)
{
Get-WmiObject Win32_Share -computerName $Computer | Select PSComputerName,Name, Caption, Path | where {$_.Name -NotLike “*$”} | Export-csv -append "c:\temp\ServerShares.csv" -NoTypeInformation
}

foreach ($Computer in Get-Content c:\temp\ServerShares.csv)
{
Get-ACL -Path $Computer | Select -expand access | where {$_.Name -NotLike “*$”} | Export-csv -append "c:\temp\ServerSharesACL.csv" -NoTypeInformation

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
2020-11-04T18:30:06.607+00:00

Maybe this is working for you?

foreach ($Computer in Get-Content c:\temp\ComputerNames.txt)
{
Get-WmiObject Win32_Share -computerName $Computer | Select PSComputerName,Name, Caption, Path | where {$_.Name -NotLike “*$”} | Export-csv -append "c:\temp\ServerShares.csv" -NoTypeInformation
}


foreach ($Computer in Get-Content c:\temp\ServerShares.csv)
{
$values = $Computer -split(",") -replace('"',"")
$ComputerName = $values[0]
$shareName = $values[1]
$unc = "\\$ComputerName\$shareName"
Get-ACL -Path $unc | Select -expand access | where {$_.Name -NotLike “*$”} # | Export-csv -append "c:\temp\ServerSharesACL.csv" -NoTypeInformation
}

I am pretty sure it's possible to get this easier/shorter done. But this works for me ;-)


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

Was this answer helpful?


1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 132.1K Reputation points MVP Volunteer Moderator
    2020-11-04T19:34:48.853+00:00

    There you go:

    foreach ($Computer in Get-Content c:\temp\ServerShares.csv)
        {
        $values = $Computer -split(",") -replace('"',"")
        $ComputerName = $values[0]
        $shareName = $values[1]
        $unc = "\\$ComputerName\$shareName"
        $acls = Get-ACL -Path $unc | Select -expand access | where {$_.Name -NotLike “*$”}
        foreach ($acl in $acls)
            {
            $result = "$unc" + ";" + $acl.FileSystemRights + ";" + $acl.AccessControlType + ";" + $acl.IdentityReference + ";" + $acl.IsInherited + ";" + $acl.InheritanceFlags + ";" + $acl.PropagationFlags 
            $result >> "c:\temp\ServerSharesACL.csv"
            }
        }
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.