Script: Collect all shares across the network.
#######################################
### Script: GetSMBShares.ps1 ###
### By: aikux.com ###
### Version: 1.0 ###
### Date: 2015-12-13 ###
#######################################
$ErrorActionPreference = "SilentlyContinue"
function Get-SMBSharesWinRM {
param (
[Parameter (Mandatory = $true, Position = 1)]
[string]$ComputerName
)
$Session = New-CimSession -ComputerName $ComputerName
Get-SmbShare -CimSession $Session | where {$_.Description -ne "Standardfreigabe" -and $_.Description -notlike "Remote*" -and $_.Description -ne "Druckertreiber"} | select Name, Path, Description, PSComputerName
$Session | Remove-CimSession
}
function Get-SMBSharesWMI {
param (
[Parameter (Mandatory = $true, Position = 1)]
[string]$ComputerName
)
gwmi win32_share -ComputerName $ComputerName | where {$_.Description -ne "Standardfreigabe" -and $_.Description -notlike "Remote*" -and $_.Description -ne "Druckertreiber"} | select Name, Path, Description, PSComputerName
}
function Get-Option {
Write-Host r
ndo you want to use WinRM or WMI?r
n
Write-Host [1] WinRM
Write-Host [2] WMIr
n
Read-Host what option you want?
}
function Get-SMBSharesOption {
param (
[Parameter (Mandatory = $true, Position = 1)]
[int]$Option,
[Parameter (Mandatory = $true, Position = 2)]
[string]$Hostname
)
If ($Option -eq "1") {
Get-SMBSharesWinRM $Hostname
}
ElseIf ($Option -eq "2") {
Get-SMBSharesWMI $Hostname
}
Else {
Write-Host "r
nyou didn't choose option [1] or [2]"
Start-Sleep 3
Exit
}
}
Write-Host "#################################################################################################"
Write-Host "########## ##########"
Write-Host "########## get SMB shares by aikux.com ##########"
Write-Host "########## ##########"
Write-Host "#################################################################################################"r
n
Write-Host choose an option you want:r
n
Write-Host "[1] type in a hostname (for example Server01.testdom.local)"
Write-Host "[2] type in a subnet (for example 192.168.0)"
Write-Host "[3] type in a path to a file which included a list of hostnames (for example C:\Temp\servers.txt)"r
n
$Mode = Read-Host what option you want?
If ($Mode -eq "1") {
$Hostname = Read-Host r
ntype in a hostname
}
ElseIf ($Mode -eq "2") {
$Subnet = Read-Host r
ntype in a subnet
}
ElseIf ($Mode -eq "3") {
$Path = Read-Host r
ntype in a path to a file which included a list of hostnames
}
Else {
Write-Host "r
nyou didn't choose option [1], [2] or [3]"
Start-Sleep 3
Exit
}
$Output = Read-Host "r
ntype in an output path (for example C:\Temp)"
If ($Hostname) {
$Service = Get-Option
Write-Host r
nget SMB shares from $Hostname
Get-SMBSharesOption $Service $Hostname | Export-Csv -Path $Output\Shares_$Hostname.txt -Encoding UTF8
Write-Host done ... $Hostnamer
n
}
ElseIf ($Subnet) {
$Shares = @()
$Service = Get-Option
Write-Host r
nchecking for hosts in subnet
$Hosts = 1..254 | ForEach-Object {Get-WmiObject Win32_PingStatus -Filter "Address='$Subnet.$_' and Timeout=200 and ResolveAddressNames='true' and StatusCode=0" | select ProtocolAddress*}
foreach ($line in $Hosts.ProtocolAddressResolved) {
Write-Host r
nget SMB shares from $line
$Shares += @(Get-SMBSharesOption $Service $line)
Write-Host done ... $liner
n
Clear-Variable line
}
$Shares | Export-Csv -Path $Output\Shares.txt -Encoding UTF8
Write-Host finished all serversr
n
}
ElseIf ($Path) {
$Shares = @()
$Service = Get-Option
$Servers = Get-Content $Path
foreach ($Server in $Servers) {
Write-Host r
nget SMB shares from $Server
$Shares += @(Get-SMBSharesOption $Service $Server)
Write-Host done ... $Serverr
n
Clear-Variable Server
}
$Shares | Export-Csv -Path $Output\Shares.txt -Encoding UTF8
Write-Host finished all serversr
n
}
Seiten:
Permanentlink zu diesem Beitrag: https://help.migraven.com/script-collect-all-shares-across-the-network/