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.

Friday, December 15, 2006

Powershell network scan

Simple scan all shares on all machines a given windows domain for a give file type. say scan local network for mp3?
should be handy right ;)

Thursday, December 14, 2006

IE automation

Lately had to work on asp.net based workflows. Awfully, painful having to fill in all that test data over several pages just to get to say page 8/10 to do some basic unit testing. Thanks to DHTML/JavaScript, No Nunits/NunitAsp to rescue here!

Being as mind buggering as that, i had following options:

  1. Don't do any unit testing but loose friends and eventually job.
  2. Do unit testing but loose sanity.
  3. Quit and find another job. but how far could i run?
  4. Find some "free" IE automation tool or GUI testing tool which allows easy recording and playing back browser sessions. Preferably via nunit but beggars can't be choosers so anything else will do, just make it "quick and easy".
  5. If not found, write one. Like it, ride my hobby horse and charge on to windmills.
Options 1-3: are out of question as i am lazy.
Option 2 :If money can't buy you friends, unit testing ones own code definitely can. hmm... what a life to live.
Option 4: sounded promising and it indeed was. There is Watir, "Web Application Testing in Ruby" and some such in perl too. Then there is Selenium. They are pretty good frameworks and here's some comparison
http://opensource.thoughtworks.com/papers/WatirAndSelenium.jsp

oh! and now there is Watij (pronounced wattage not Wat-is-e)

while oogling and googling these things it became clear that it can't be that difficult to write a tool to scratch my itch exactly as i want it.

It is as simple as hosting a browser, hook up a few change event handler for recording and playback is just is even easier. How difficult can it be to find a unique identifier for whatever html element has been clicked, get its value and save it say an xml file. To play the session back, read each element id from xml and do document.all(id) and then call corresponding set value. If the tool is meant to help me nunit test my code, i can surely put a silly id on everything that has a some interesting behaviour. Guess pop-ups will a hassle but perhaps some nifty autoit script can come to help.

let see ...