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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 107.9K Reputation points MVP
    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


1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 107.9K Reputation points MVP
    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


Your answer

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