Thursday, April 24, 2008

Powershell

I'm in a class for the second half of the week, the upshot of which is I've got a new scripting language to blog about... :-)

Here's the start of a script I'm converting to Powershell from perl. It examines my IP address, and determines what should be started (or stopped) when I'm at work.

The perl variant looks like this:

$address = gethostbyname("L353K79XPL");
$address = join(".",unpack("C"x4,$address));
if ($address =~ /123.321/) { ## fake subnet, sorry
debug_msg("on work network");
StartService("", "Wuser32");
StartService("", "ASMAgent");
StopService("", "RapApp");
StopService("", "VPatch");
StopService("", "BlackICE");
StopService("", "tunnelguardservice");
}


Here's the powershell version. It might not look it, but getting the IP is much more understandable to me.

if ((get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled = true")[0].IPAddress -match "123.321.\d+.\d+")
{
write-host "at work"
start-service Wuser32
start-service asmagent
stop-service rapapp
stop-service vpatch
stop-service blackice
stop-service tunnelguardservice
}


More fun to come.

No comments:

counter free hit invisible