Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
joe944
Jan 31, 2004

What does not destroy me makes me stronger.
Scripting noob here working on my first powershell script and I'm wondering if anyone would be able to help me out.

What I'm trying to do is periodically check the external IP address of a machine and determine if it has changed or not. Preferably if the IP has changed it would create an event log that will be picked up by another monitoring tool.

Feel free to tell me I'm retarded for what I've got so far. It's not complete and doesn't quite do what I want yet, but I think I'm on the right track. I need to do some serious reading up on powershell in general but I've picked up a little bit working on this script so far.

code:
$filename = "path\iplog.txt"
$time = " "+(Get-Date).ToString()
$source = "http://whatismyip.org"
$client = new-object System.Net.WebClient
$webpage = $client.downloadString($source)
$lines = $webpage.split("'n")
foreach ($line in $lines) {
    $value = "6522 "+$line +$time
    $value >> $filename
}
$oldip = get-eventlog -logname 'IP Check' -newest 1 | select message

Write-EventLog  -logname 'IP Check' -source 'IP Check' -eventid 1000 -entrytype information -message $line

Adbot
ADBOT LOVES YOU

joe944
Jan 31, 2004

What does not destroy me makes me stronger.
It was a rough draft. :D My goal with $oldip was to pull the IP from the last event log and compare it to the recently polled IP from whatismyip. I'd like to use a loop to send out a different type of event log if the IP changes and perhaps send out an email.

The path for the iplog.txt was correct. That was the original log that I wrote to; using events was something I tacked quickly at the end but I think would be a superior method. I may still keep that log file just to have a consolidated list of past IP's. Would there be a way to only append the log when a new IP is discovered instead of everytime the script is run?

Appreciate the help.

Edit: That's awesome! I will have to break down the details and figure out why you did what you did tomorrow but that looks like exactly what I need to get this to work.

joe944 fucked around with this message at 11:15 on Nov 10, 2011

joe944
Jan 31, 2004

What does not destroy me makes me stronger.
You're awesome, I got everything working perfectly. Now to do some experimentation for more advanced features!

I think powershell is going to come in very handy..

joe944
Jan 31, 2004

What does not destroy me makes me stronger.

MEAT TREAT posted:

I did something stupid. I associated .ps files with notepad++ because I was tired of having them open in regular notepad, but I've lost the ability to right-click and run with powershell.

Does anyone know how I can make it work again?

What OS are you running? Does right click > open with > choose default program and select powershell work?

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply