Hey Ho PowerShell, Let’s go!

November 17, 2012

I’ve recently been pondering on the best approach to learning PowerShell. I’ve watched a few podcasts, skimmed through some free tutorials on the internet and not a day goes by that I don’t receive a newsletter or come across a PowerShell article.

Over my career I’ve been exposed to various programming and scripting languages and am fairly confident using T-SQL, vbscript, classic ASP, html/dhtml and some javascript.

I’ve decided to approach learning PowerShell by creating scripts for any routine Windows Server and SQL Server admin tasks that I would normally use a GUI or other method to obtain the information.

As an example, I quite often need to know when a particular server was last rebooted. There are a number of methods to obtain this information (e.g.  Scanning the event logs) and one way that I’ve personally been using is a great utility called uptime.exe (http://support.microsoft.com/kb/232243). It’s a great utility which works really well and there is no need to replace it but I figured I’d see how hard it would be to replicate using PowerShell.

Here is the code.

$servername = Read-Host 'Server name'
$wmi = Get-WmiObject -Class Win32_OperatingSystem -Computer $servername
$wmi.ConvertToDateTime($wmi.LastBootUpTime)
(get-date) - $wmi.ConvertToDateTime($wmi.LastBootUpTime)
Write-Host –NoNewLine "Closing in 10 seconds..."
Start-Sleep -s 10

PowerShell Script Uptime

Line 1 (prompting for a servername), 4 (extra formatting) and 5 and 6 (delay closing) are all unnecessary. It could be done just using line 2 and 3 replacing ‘$servername’ with the actual servername. Two lines to retrieve last server reboot time! Simply brilliant.

I saved this as lastreboot.ps1 and then created a batch file to call this PowerShell script and saved the batch file into C:\windows\system32. By saving the batch file in this windows folder, I can now run it directly from the windows ‘run’ box.

PowerShell uptime batch file

On my Win 7 laptop, it’s this quick to execute: windows key and type psuptime. I could go further and assign a shortcut key to the batch file, e.g. CTRL + ALT + U. At the prompt, type in your servername.

Run PowerShell batch file

As per below, we can see the server was last rebooted on 25 of October, which was 19 days ago.

PowerShell Last Reboot Output

There are a few other checks and information that I’d like to add to this script.

  • Export the above to a text file
  • Display Errors in last 24 hours from the event logs
  • Display latest SQL error log file entries
  • SQL database and Agent service running check
  • Next scheduled reboot time (windows updates)
  • Any installed software or patches in the last 24/48hrs
  • Disk space

Thanks for reading.

Advertisements

One Response to “Hey Ho PowerShell, Let’s go!”


  1. […] on from my initial PowerShell post I decided to expand on that […]


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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: