Hi I am still fairly new to powershell. I was asked about getting a list of all servers that have shares on the network. I came across a nice script that does this, but I would also like it to show the permissions of each share as well if possible and have it exported out to a csv or html. Any help is much appreciated.<\/p>\n
Here is the script that I have.<\/p>\n
# Add powershell modules for Active Directory \nimport-module activedirectory;\n \n# Get list of servers from Active Directory \n$servers = get-adcomputer -filter {operatingsystem -like \"*server*\"} | where {$_.enabled -eq $true};\n \n# Loop through each server found \nforeach ($server in $servers) {\n \n # Check if server can be pinged \n if(test-connection -cn $server.name -quiet -count 1) {\n \n # Output to screen the name of the server \n write-host \"-------------\"; \n write-host $server.name; \n write-host \"-------------\";\n \n # Get the list of shared folders \n $shares = gwmi win32_share -computer $server.name;\n \n # Loop through each shared folder found \n foreach ($share in $shares) {\n \n # Exclude default drive shares, other system default shares and printers\n if(($share.name -ne “print$”) -and ($share.name -ne “prnproc$”) -and ($share.path -notlike “*LocalsplOnly*”) -and ($share.name -notmatch “^.\\$”) -and ($share.name -ne “ipc$”) -and ($share.name -ne “sysvol”) -and ($share.name -ne “netlogon”)-and ($share.name -ne “admin$”)) { \n \n # Output to screen the name and path of the shared folder \n write-host $share.name $share.path; \n }\n \n }\n \n write-host \"\";\n \n }\n}\n \n<\/code><\/pre>","upvoteCount":7,"answerCount":3,"datePublished":"2019-07-03T16:54:20.000Z","author":{"@type":"Person","name":"spiceuser-67jd0","url":"https://community.spiceworks.com/u/spiceuser-67jd0"},"suggestedAnswer":[{"@type":"Answer","text":"
Advertisement
Hi I am still fairly new to powershell. I was asked about getting a list of all servers that have shares on the network. I came across a nice script that does this, but I would also like it to show the permissions of each share as well if possible and have it exported out to a csv or html. Any help is much appreciated.<\/p>\n
Here is the script that I have.<\/p>\n
# Add powershell modules for Active Directory \nimport-module activedirectory;\n \n# Get list of servers from Active Directory \n$servers = get-adcomputer -filter {operatingsystem -like \"*server*\"} | where {$_.enabled -eq $true};\n \n# Loop through each server found \nforeach ($server in $servers) {\n \n # Check if server can be pinged \n if(test-connection -cn $server.name -quiet -count 1) {\n \n # Output to screen the name of the server \n write-host \"-------------\"; \n write-host $server.name; \n write-host \"-------------\";\n \n # Get the list of shared folders \n $shares = gwmi win32_share -computer $server.name;\n \n # Loop through each shared folder found \n foreach ($share in $shares) {\n \n # Exclude default drive shares, other system default shares and printers\n if(($share.name -ne “print$”) -and ($share.name -ne “prnproc$”) -and ($share.path -notlike “*LocalsplOnly*”) -and ($share.name -notmatch “^.\\$”) -and ($share.name -ne “ipc$”) -and ($share.name -ne “sysvol”) -and ($share.name -ne “netlogon”)-and ($share.name -ne “admin$”)) { \n \n # Output to screen the name and path of the shared folder \n write-host $share.name $share.path; \n }\n \n }\n \n write-host \"\";\n \n }\n}\n \n<\/code><\/pre>","upvoteCount":7,"datePublished":"2019-07-03T16:54:20.000Z","url":"https://community.spiceworks.com/t/get-all-servers-with-shares/719386/1","author":{"@type":"Person","name":"spiceuser-67jd0","url":"https://community.spiceworks.com/u/spiceuser-67jd0"}},{"@type":"Answer","text":"You can use Get-ACL to list the permissions.<\/p>\n
FYI, Write-Host is discouraged, because it de-serializes objects to output them in the console, which breaks the pipeline. Here is a post from Jeffrey Snover, lead developer of the original Powershell team, that explains the issue.<\/p>\n