Friday, December 22, 2006

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;

Thursday, December 21, 2006

Christmas gift wrapping scam.

We all know what it is like to shop till you drop and then come home and gift wrap them all. Not so much fun is it? Enter Gift Wrapping Services for "a price of a small fee". Just what we want, have cuppa tea, think over have we missed anything or anyone, while someone gift wraps all them million things. One thing off your mind right?

Wrong. Hand over your shopping to any Tom, Dick and Harry and they could switch your expensive perfume with a potato or may be an onion. It wont be funny when come Christmas morning, whoever tears into their gift and finds what, "a potato!".

So the best option is to get your gifts wrapped while you actually buy them otherwise you better know what you are doing.