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
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?
thanks,
ricky c.
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.
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?
[…] can find more detail about this one-liner in an earlier post. Share this:TwitterFacebookLike this:LikeBe the first to like […]