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 rndo you want to use WinRM or WMI?rn
    Write-Host [1] WinRM
    Write-Host [2] WMIrn
    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 "rnyou 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 "#################################################################################################"rn

Write-Host choose an option you want:rn
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)"rn
$Mode = Read-Host what option you want?

If ($Mode -eq "1") {
    $Hostname = Read-Host rntype in a hostname
    }
ElseIf ($Mode -eq "2") {
    $Subnet = Read-Host rntype in a subnet
    }
ElseIf ($Mode -eq "3") {
    $Path = Read-Host rntype in a path to a file which included a list of hostnames
    }
Else {
    Write-Host "rnyou didn't choose option [1], [2] or [3]"
    Start-Sleep 3
    Exit
}

$Output = Read-Host "rntype in an output path (for example C:\Temp)"

If ($Hostname) {
    $Service = Get-Option
    Write-Host rnget SMB shares from $Hostname
    Get-SMBSharesOption $Service $Hostname | Export-Csv -Path $Output\Shares_$Hostname.txt -Encoding UTF8
    Write-Host done ... $Hostnamern
}
ElseIf ($Subnet) {
    $Shares = @()
    $Service = Get-Option
    Write-Host rnchecking 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 rnget SMB shares from $line
        $Shares += @(Get-SMBSharesOption $Service $line)
        Write-Host done ... $linern
        Clear-Variable line
    }
    $Shares | Export-Csv -Path $Output\Shares.txt -Encoding UTF8
    Write-Host finished all serversrn
}
ElseIf ($Path) {
    $Shares = @()
    $Service = Get-Option
    $Servers = Get-Content $Path
    foreach ($Server in $Servers) {
        Write-Host rnget SMB shares from $Server
        $Shares += @(Get-SMBSharesOption $Service $Server)
        Write-Host done ... $Serverrn
        Clear-Variable Server
    }
    $Shares | Export-Csv -Path $Output\Shares.txt -Encoding UTF8
    Write-Host finished all serversrn
}

Permanentlink zu diesem Beitrag: https://help.migraven.com/script-collect-all-shares-across-the-network/