Powershell Network Scan - Script
Here's the code/script for network scan I was talking about in previous post here
function HandleShare
{
$share = $args[0];
write-host ***Scanning Share $share
#write-host net use X: $share
$erroractionpreference = "SilentlyContinue";
$files = Get-ChildItem -recurse -name $share $fileType
$erroractionpreference = "Continue";
foreach($file in $files)
{
if($file.length -gt 0)
{
$filepath = $share + "\" + $file;
write-host $filepath
}
}
}
function HandleHost
{
$hostname = $args[0];
$hostname = $hostname.substring(0, $hostname.indexof(" "))
#write-host "***Scanning Host " + $hostname;
#List shares.
$shares = net view $hostname | findstr " Disk"
foreach($share in $shares)
{
if($share.length -gt 0)
{
$tmp = $share.substring(0, $share.indexof("Disk"));
$tmp = $tmp.Trim();
$tmp = $hostname + "\" + $tmp;
HandleShare $tmp;
}
}
}
function DisplayUsage
{
write-host
write-host -foregroundcolor green "Usage:"
write-host -foregroundcolor green " scan.ps1 fileType maxNoOfMachines"
write-host -foregroundcolor green " e.g. scan.ps1 *.txt 10"
write-host
}
function Main
{
$hostnames = net view | findstr "\\";
foreach($hostname in $hostnames)
{
$maxNoOfMachines = $maxNoOfMachines - 1;
if($maxNoOfMachines -lt 0)
{
exit;
}
HandleHost $hostname;
}
}
#
# Initialise
#
#Guard against un initialised variables. aka, typos
set-psdebug –strict
#using $fluff as i dint like LoadWithPartialName o/p somethign to console.
$fluff = [reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
#
# Validate input paramters
#
if($args.count -lt 2 -or $args.count -gt 2)
{
DisplayUsage;
exit;
}
if(-not([Microsoft.VisualBasic.Information]::isnumeric($args[1])))
{
DisplayUsage;
write-host -foregroundcolor Red "Error:"
write-host -foregroundcolor Red " Second paramter should be numeric."
write-host
exit;
}
#
# Convert args into variables which are used through out this script.
#
$maxNoOfMachines = $args[1];
$fileType = $args[0]; #Global variable used in HandleShare.
#$maxNoOfMachines = 30; #Global variable used in Main.
#
# Start actual work
#
Main;