Powershell one-liner to check the time on a bunch of servers for DST

9 03 2007

Over the weekend I need to check a bunch of servers (mostly Domain Controllers) to see if the DST (Daylight Savings Time) change occurs correctly.  I decided to see how I could do it in Powershell and this is what I came up with: 

Get-Content servers.txt | %{$x = net time \\$_; $x[0];If($x[2].contains(“Local”)){$x[2]}} | Add-Content Servertime.txt

This is a line of Powershell code that will open servers.txt (which is just a list of servers) and for each server it will run the “net time” command.  I pass the first line of the output of the “net time” command $x[0] and check to see if the third line contains “Local”.  If it does I pass that line as well ( to see the local time on the servers that are not in my timezone.)  I then send this to Servername.txt

Quick, easy one line of code.  You’ve gotta love Powershell!  Thanks Microsoft



4 responses

4 10 2007

Hi! I listen to your podcast and know you talk about vb scripts, but i’m not a big time admin, just a home user and enjoy fiddling around with vbs script… could you occasionnally talk about that subject?

ricky c.

9 03 2008

I know this is an old entry, but I just discovered the podcast and am working my way through them as I learn. As it’s 03/09/08 today, I thought I’d try out this one-liner to check out all my machines and share a “duh” moment I had. I kept getting errors running the script, and made some illogical assumptions while trying to figure out the problem. In the end I realized that the “net view” command I had run to generate my list of servers had left the “\\” prefix on all the machine names; when I script on my own I take that into account, but doing a copy / paste of this script had turned each command into “net view \\\\server”, causing the net command to fail on a syntax error 🙂 I wasted a few minutes messing around with the pasted text before I bothered to look at the input file.

7 04 2010
Harold Stultiens

I know this is an old one but worked fine a couple of weeks ago and now I am getting error’s on the Net time command. It thinks it is looking for net.exe and fails. How would I correct this?

5 07 2012
Episode 1 – Fundamental Cmdlets « PowerScripting Podcast

[…] can find more detail about this one-liner in an earlier post. Share this:TwitterFacebookLike this:LikeBe the first to like […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: