PowerShell - get-NetworkComputerShares.ps1
- Jon Boyette
- Jan 10, 2022
- 2 min read
Save the following as the get-NetworkComputerShares.ps1, runs and discovers ALL network shares and repots to where defined, it will prompt twice at the end for the file path to write to. This works its way through EVERY AD computer and discovers any shared folders or drives to csv
<#
.SYNOPSIS
Get Share Infornmation on Network Computers
.DESCRIPTION
This script uses WMI and Active Directory to query all computers on a network within
the domain to document shares. It converts the share type information to readable share type.
.NOTES
File Name : get-NetworkComputerShares.ps1
Version : 1.1
Date : 02/18/2012
Author : Ted Wagner
Requires : PowerShell V2
.EXAMPLE
PS c:\foo> .\get-NetworkComputerShares.ps1
#>
#Requires -Version 2.0
# Convert Share Type to Type Description
function get-ShareType($Share){
$ShareType = $(switch($Share){
"0" {"Disk Drive"}
"1" {"Print Queue"}
"2" {"Device"}
"3" {"IPC"}
"2147483648" {"Disk Drive Admin"}
"2147483649" {"Print Queue Admin"}
"2147483650" {"Device Admin"}
"2147483651" {"IPC Admin"}
}
)
return $ShareType
}
Clear-Host
Import-Module ActiveDirectory
# Set Variables
$ErrorActionPreference = "silentlycontinue"
$counter = 0
$TotalProcessedCount = 0
$TotalSharesCount = 0
$ShareList = @()
$FailList = @()
# Let's set the error action preference to continue. Trap using a trap statement below, display the error, and always continue.
# Get all AD Computer objects, select only the property "Name"
$Computers = Get-ADComputer -Filter 'Name -like "*"' | Select -Expand Name
foreach ($Computer in $Computers){
$Counter++
$TotalShares = $Computers.count
$progress = [int]($Counter / $TotalShares * 100)
Write-Progress -Activity "Getting inventory of Computers" -status "Processing shares on computer: $Computer - $counter out of $TotalShares" -perc $progress
if (Test-Connection -Quiet -Count 1 -ComputerName $Computer){
$TotalProcessedCount++
try{
$Shares = get-wmiobject -class "Win32_Share" -computername $Computer -ErrorVariable WMIError -ErrorAction SilentlyContinue | Select Name, Description, Path, Type | Where-Object { $_.type -eq "0"}
# Make sure get-wmiobject returns data then populate array
if ($Shares){
foreach ($Share in $Shares) {
$TotalSharesCount++
$ShareSummary = "" | Select ComputerName, Description, Name, Path, ShareType
$ShareSummary.Computername = $Computer
$ShareSummary.Name = $Share.Name
$ShareSummary.Description = $Share.Description
$ShareSummary.Path = $Share.Path
$ShareSummary.ShareType = get-ShareType $Share.Type
$ShareList += $ShareSummary
}
}
}
catch{
Write-warning "Error on computer number $counter - $Computer"
$FailSummary = "" | Select ComputerName, Error
$FailSummary.Computername = $Computer
$FailSummary.Error = $WMIError[0].Exception
$FailList += $FailSummary
$WMIError.clear()
continue
}
finally
{
$script:Exception = $_; continue.
}
}
}
# Output data to CSV files - only output for RPC errors if errors received
$CSVFile = Read-Host "Enter filename/path for the Share Summary Report";$ShareList | Export-Csv $CSVFile -NoTypeInformation
if ($FailList){
$CSVFile = Read-Host "Enter filename/path for the Share Failed Summary Report";$FailList | Export-Csv $CSVFile -NoTypeInformation
}
Write-Host "Processed $TotalSharesCount shares on $TotalProcessedCount computers"
Comments