[System.Net.IPAddress]$StartScanIP = "10.1.0.1" [System.Net.IPAddress]$EndScanIP = "10.2.254.254" [string]$ShareNameExclusions = "^ADMIN$*|^IPC$*|^print$*|^[A-Z]\`$" # $Watch = [System.Diagnostics.Stopwatch]::StartNew() $Watch.Start() # $ScanIPRange = @() if($EndScanIP -ne $null) { $StartIP = $StartScanIP -split '\.' [Array]::Reverse($StartIP) $StartIP = ([System.Net.IPAddress]($StartIP -join '.')).Address # $EndIP = $EndScanIP -split '\.' [Array]::Reverse($EndIP) $EndIP = ([System.Net.IPAddress]($EndIP -join '.')).Address # For ($x=$StartIP; $x -le $EndIP; $x++) { $IP = [System.Net.IPAddress]$x -split '\.' [Array]::Reverse($IP) $ScanIPRange += $IP -join '.' } } else { $ScanIPRange = $StartScanIP } Workflow Network-Scan{ param($ippool,$exclusions) $WFResult = @() foreach -parallel -throttlelimit 50 ($ip in $ippool) { $ItemReturn = inlinescript { [array]$ResultArray = "" If (Test-Connection -Computername $USING:ip -BufferSize 16 -Count 1 -Quiet) { If (Test-NetConnection -Computername $USING:ip -Port 139 -InformationLevel Quiet) { $Response = (net view $USING:ip /all 2>$null | Where-Object { $_ -match '\sDisk\s' }) ` -replace '\s\s+', ',' | ForEach-Object{ ($_ -split ',')[0] } ForEach($Line in $Response) { If ($Line -notmatch $USING:exclusions) { $ResultArray = $ResultArray + $Line } } $ResultArray = $ResultArray | ? {$_} If ($ResultArray) { try{ $hName = [System.Net.Dns]::GetHostByAddress($USING:ip).HostName } catch{ $hName = " - " } $Item = New-Object System.Object $Item | Add-Member -MemberType NoteProperty -Name "SrvIP" -Value $USING:ip $Item | Add-Member -MemberType NoteProperty -Name "SrvName" $hName $Item | Add-Member -MemberType NoteProperty -Name "Shares" $ResultArray return $Item } } } } #inlinescript $WORKFLOW:WFResult += $ItemReturn } return $WORKFLOW:WFResult } $Result = Network-Scan $ScanIPRange $ShareNameExclusions $Result | Sort-Object Version,SrvIP,Status | ` Format-Table -GroupBy Version -Wrap -Autosize ` @{Name="Host IP address";Expression = { $_.SrvIP }; Alignment="Center"}, @{Name="Host name from DNS";Expression = { $_.SrvName }; Alignment="Center"}, @{Name="Shares List";Expression = { $_.Shares -join ' , ' };Alignment="Left"} Write-Host $Result.Count "hosts found on network range " $StartScanIP "-" $EndScanIP $Watch.Stop() Write-Host "Script time:" $Watch.Elapsed