Following on from my initial PowerShell post I decided to expand on that script.

The idea behind this PowerShell script was to obtain and display Windows Server information instantly. I was thinking along the lines of someone walking up to your cubicle and throwing any of the following questions at you:

  • How much disk space do we have on server xxx?
  • When was server xxx last rebooted?
  • Who are the local administrators on server xxx?
  • What shares exist on server xxx?
  • Etc

How quickly could you obtain these answers? How many different tools and utilities would you start firing up?  Would this result in a whirlwind of whirlwind of CTRL + TAB between various applications? The answers to the above questions (and more) could be obtained by using any of the following tools and utilities e.g. 

  • Windows Explorer
  • The Computer Management console
  • VMWare console
  • Event Viewer
  • SCOM
  • SSMS
  • Remote connecting to the server

More than likely you probably have many other scripts, tools or utilities that gather this information daily.  However, what I’m really trying to achieve is to answer all those questions in the minimum amount of key strokes and time. How about 4 keyboard strokes!! Yes, 4!

  1. Keyboard Windows Key (1 key stroke)
  2. Type ‘qc’ (2 key strokes) in the run/search box
  3. Press {enter}  (1 key stroke)

Now that’s fancy! Someone high up and important stands behind you and watches an old school DOS window popup, and all the server information stream out in the same time that it takes them to pull up and chair.

Shorterned example of the output:

PowerShell Quick Server Check

How to install

  1. Download these two files: (quickcheckBATcode) and (quickcheckPScode)
  2. Save the contents of [quickcheckBATfile.docx] into a [qc.bat] file within [c:\windows\system32] folder.
  3. Save the contents of [quickcheckPScode.docx] into a [QuickCheck.ps1] file within [c:\windows\system32] folder.

Note: It’s not best practice to save into this system folder, but we doing this to achieve the minimum number of key strokes as mentioned above. I’m also supplying word files as wordpress has restrictions on what file types can be uploaded. The bat file simply calls the PowerShell (ps1) file.

How to execute

  1. Keyboard Windows Key or click on the Windows Start Icon
  2. Type ‘qc’ in the run/search box and press {enter}
  3. Type in your servername at the prompt

QuickCheck v1 currently returns the following information:

  • Operating System
  • Server Specification
  • Current Disk Space
  • Last Reboot time
  • Network Shares
  • Services (running under a service account)
  • Services (set to automatic, but stopped)

Thanks for reading.

 

Advertisements

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.