Why Should I Learn PowerShell? Real World Example Saves the Day!

Why Should I Learn PowerShell? Real World Example Saves the Day!

  • Comments 5
  • Likes

Summary: Guest blogger, Tim Bolton, shares a real world story that brings home the need to learn Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here, and it’s time for the Weekend Scripter. Today we have a guest blogger, Tim Bolton.

Photo of Tim Bolton

Tim Bolton has been involved in the IT Pro community for over 18 years. Tim received the Microsoft Certifications MCITP Enterprise Administrator on Windows Server 2008 and MCTS Microsoft Exchange Server 2010, Configuration. Tim is currently working as a Microsoft consultant and systems engineer at Software Solutions. Tim and his family have recently relocated to the Dallas/Fort Worth area.

Blog: Tim Bolton – MCITP – MCTS, Sharing information about IT and things I have broken, fixed, and seen 
Twitter: @jsclmedave
LinkedIn: Tim Bolton

Take it away Tim…

Working within a large corporate environment can produce some strange IT issues. Often these issues demand immediate attention and answers, usually with a manager hovering over you.

Recently while I was working in one of those large corporate environments, which had a large network that had just inherited over a thousand servers, a security issue was raised. Each of the production servers are required to have RSA installed and running to ensure secure log ons.

Because this was one of those scenarios where the servers were recently inherited, it proved to be extremely difficult to provide an accurate answer for, “Can you confirm that the RSA service is running on all of the servers within our scope?”

SCCM was not an option at this time and management wanted an answer “now,” so there was not time to open a service ticket for assistance. Logging on to over a thousand servers to verify if the RSA service was running was obviously not an option, and other third-party applications were not at our disposal.

Windows Powershell to the rescue!

With guidance from Windows Powershell MVP, Claus Nielsen, who happened to be online and available, I was able to provide the answer that was needed in a timely manner. I already had a text file (MyServers.txt) with all of the servers in our scope listed, so that part was already accomplished.

The service that the RSA application ran as is OASVC_Local. I simply needed to check for that service on each server in the text file.

Using the appropriate credentials, I pulled the servers from the text file by using Get-Content, and I placed them into a variable that I called $Servers. Here is the command I used.

$Servers = Get-Content "C:\Temp\MyServers.txt"

Now, using the Foreach command, I started checking for the service on the remote servers. The Foreach command will loop through each server in the $Servers variable. Each loop will use the variable $Server which will contain one item from the variable $Servers list of servers. By using the variable $Server (the computer name in this case) and Get-WMIobject to connect to remote servers, the script will query for the service OASVC_Local, and populate the variable $Service with a "Service" object that will contain the properties for each object. Here is the command I arrived at using.

Foreach ($Server in $Servers)

{$Service = Get-WMIobject win32_service -computername $Server -Filter "Name = 'OASVC_Local'"

I also want to determine if the service is running or not. I query if the service is running by using the If statement. If the state equals “running,” then write "RSA is Running for $Server" to a file called RSA.txt, and also write it to the console screen. I wanted to have the results appear on the console screen for quick viewing, in addition to a text file that I could use as a simple search for the key word “NOT” to identify servers that needed attention. (Note: NOT running -BackgroundColor red.) Here is that portion of the script.

If ($Service.state -eq "running")

 {

Write-Output "RSA is Running for $Server" | out-file -append C:\Temp\RSA.txt

Write-Host "RSA is Running for $Server”

} else

 {

Write-Output "RSA is NOT Running for $Server" | out-file -append C:\Temp\RSA.txt

Write-Host "RSA is NOT Running for $Server" -BackgroundColor red

}

}

The following image shows the commands that I typed and the output that I received in the Windows PowerShell console.

Image of command output

Here is the script in its entirety. Simple, quick—and it provided me with the information that I needed in seconds. I actually found fewer than 10 servers out of a thousand, that needed to be checked as to why the RSA service was not running. This was a manageable task, which was corrected before the lunch hour.

$Servers = Get-Content "C:\Temp\MyServers.txt"

Foreach ($Server in $Servers)

{$Service = Get-WMIobject win32_service -computername $Server -Filter "Name = 'OASVC_Local'"

If ($Service.state -eq "running")

 {

Write-Output "RSA is Running for $Server" | out-file -append C:\Temp\RSA.txt

Write-Host "RSA is Running for $Server"

} else

 {

Write-Output "RSA is NOT Running for $Server" | out-file -append C:\Temp\RSA.txt

Write-Host "RSA is NOT Running for $Server" -BackgroundColor red

}

}

Without guidance and explanation from Windows PowerShell MVP, Claus Nielsen, this would have taken “me” most of the day to configure—time that I did not have. This is yet another “real world” reason that seems to iterate throughout the IT community about learning Windows PowerShell. How many of us can reach out to a Windows PowerShell MVP that “may” be available to assist you with management’s emergencies? I was lucky today, but what will my managers ask tomorrow?

If you are an IT administrator, I would highly suggest that you become familiar with Windows PowerShell. It may save the day and make you out to be the “go to” person when an issue arises. This may also come into play when reviews are due. Harness the power of Windows PowerShell to show your company why you and your skills stand out above the others.

~Tim

Thank you, Tim. This is a great real world example of using Windows PowerShell to save the day! It is also a tribute to the amazing Windows PowerShell community.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Great post Tim and wonderful real world example of how PowerShell comes into play on a regular basis. It is also a great example of the quality of the PowerShell community! Well done Claus and thanks for sharing your story Tim!

  • Cool stuff!

    If you give GET-WMIObject a set of servers to work against, it will do so concurrently making your script run MUCH faster.  

    Try this:

    $Servers = Get-Content "C:\Temp\MyServers.txt"

    Foreach ($Service in  Get-WMIobject win32_service -computername $Servers -Filter "Name = 'OASVC_Local'")

    {

    If ($Service.state -eq "running")

    {

      Write-Output "RSA is Running for $Server" | out-file -append C:\Temp\RSA.txt

      Write-Host "RSA is Running for $Server"

    } else

    {

      Write-Output "RSA is NOT Running for $Server" | out-file -append C:\Temp\RSA.txt

      Write-Host "RSA is NOT Running for $Server" -BackgroundColor red

    }

    }

  • @jeffery - I don't think so.

  • @jrv why not?

  • Sorry for the necropost, but I was validating this for a list of my servers and logged into one that this script was telling me was not running RSA.  When I logged in, I saw that it was running RSA.  Is there a better way?