Server Testing with PowerShell for Novice Users


I was recently introduced to PowerShell and am still learning more about PowerShell, but thought I would post a blog to show the power of the shell with the task that I just completed.

The story begins when I was asked to come up with an automated script that would allow us to run some tests on remote servers, to ensure the availability of the servers after any patches have been updated on them. This is to ensure that the server hasn’t had any errors and that it is working normally (which was my task). Initially I was nowhere close to being familiar with PowerShell to know what it could do. But studying PowerShell for a while now tells me that it is a tool worth learning. It has some great powers and allows us to do things that really make life easier for us. Anyway, let’s carry on with what I was able to accomplish with PowerShell.

Here is the summary of the tasks I had to do:-

  • Ensuring that the server gives a ping response
  • Logging into the remote servers via PowerShell
  • Retrieve all service information for SQL services to ensure that they are running
  • Retrieving event-log information to ensure there were no ‘critical’ and ‘error’ severity level messages

After a lot of hard work I finally came up with decent code. How did I do it? Well, here it goes.

I started using quite a few cmdlet in PowerShell to get an idea of how I can finish my task efficiently. I then picked the code that I thought was best and turned them into functions, which I saved in a .ps1 file. I created another .ps1 file that calls the functions file and executes the function for me.

I will now explain what each function does.

Saving Credentials

$Credential = Get-Credential

This is the first stage of my functions.ps1 file. Here I get a pop up screen asking me for a username and a password which is saved for use later in this automated testing process.

FSPS.Get Credential

Function to Check Ping Response

The function is called Get-Connection.

Function Get-Connection
$Servers = Get-Content -path C:\MasterScript\Servers.txt
Write-Host “Getting Information from servers.txt file” -foregroundcolor green
foreach ($S in $Servers)
Write-Host “Testing each server for ping response” -foregroundcolor green
Test-Connection -computername $S -Count 5 | Export-Csv -Path C:\MasterScript\$((Get-Date).ToString(‘yyyyMMdd_HHmmss’))_$($S)_PingResults.csv -append
Write-Host “Ping completed and information inserted in PingResults.csv file” -foregroundcolor green

The Get-Connection function first goes to the file specified in the $Server path and takes the ip addresses from the file, and then executes the Test-Connection command one at a time for each ip address. The three Write-Host commands informs the user of the current test status. The main idea is so the user knows the progress of the test.

$Servers = Get-Content -path C:\MasterScript\Servers.txt
Write-Host “Getting Information from servers.txt file” -foregroundcolor green
Foreach ($S in $Servers)
Write-Host “Testing each server for ping response” -foregroundcolor green
Test-Connection -computername $S -Count 5 | Export-Csv -Path C:\MasterScript\\$((Get-Date).ToString(‘yyyyMMdd_hhmmss’))_$($S)_PingResults.csv -append

Once the script gains the ip address it then does a test-connection five times using the credentials that we saved at the beginning of this test process, followed by the creation of a log file and outputting the results into that file.

My filenames follow the format, yyyyMMdd_HHmmss_ipaddress_testname. The purpose is to make sure that the most recent test files are shown in datetime order when sorted on filename then grouped by server name.

Function to Retrieve SQL Server Instances

The next function is called Get-Services. Firstly the function reads the servers.txt file to get the ipaddresses of the servers and for each server, it creates a New-PSSession. That PSSession is then used in the Invoke-Command cmdlet which tells the function to start the session and execute the code in the script block to collect the service information for all SQL instances.

Function Get-Services
$Servers = Get-Content -path C:\MasterScript\Servers.txt
Write-Host “Getting Information from servers.txt file” -foregroundcolor green
foreach($S in $Servers)
Write-Host “Starting new PSSession” -foregroundcolor green
$sess= New-PSSession -ComputerName $S -Credential $credential
Write-Host “Getting SQL Services information from the server” -foregroundcolor green
Invoke-Command -Session $sess -ScriptBlock {Get-WmiObject win32_service | Select name, startmode, status, state | Where-Object {$ -match “sql”}} | Export-Csv -Path c:\MasterScript\$((Get-Date).ToString(‘yyyyMMdd_HHmmss’))_$($S)_SQLInstances.csv
Write-Host “SQL server test completed and information inserted into SQLInstances.csv file” -foregroundcolorgreen
Write-Host “Removing PSSession” -foregroundcolor green
Remove-PSSession *

Once the SQL information is collected it inserts the information into a csv file, which is created within the function. Once the information is inserted into the file, the PSSession is then removed so that a new session can be created for the next server.

Function to retrieve event log error messages

The next function is called Get-Errors. This functions enters a PSSession using the credentials from $credential similar to the previous function. It then carries out the event logs check automatically. We then retrieve the error and critical severity level messages, which are then stored in a csv file for the user to view.

Function Get-Errors
$Servers = Get-Content -path C:\MasterScript\Servers.txt
 foreach($S in $Servers)
Write-Host “Entering PSSession” -foregroundcolor green
Enter-PSSession -ComputerName $S -Credential $credential;
Write-Host “Getting ‘Error’ events from event logs and inserting the information into ErrorMessages.csv file” -foregroundcolor green
Get-EventLog system | Where-Object {$_.EntryType -match “Error”} | Where-Object {$_.TimeWritten -ge (Get-Date).AddHours(-5)} | Export-Csv -path c:\MasterScript\$((Get-Date).ToString(‘yyyyMMdd_HHmmss’))_$($S)_ErrorMessages.csv -append;
Write-Host “Getting ‘critical’ events from event logs and inserting the information into CriticalMessages.csv file” -foregroundcolor green
Get-EventLog system | Where-Object {$_.EntryType -match “0”} | Where-Object {$_.TimeWritten -ge (Get-Date).AddHours(-24)} | Export-Csv -path c:\MasterScript\$((Get-Date).ToString(‘yyyyMMdd_HHmmss’))_$($S)_CriticalErrorMessages.csv -append;
 Write-Host “EventLog test completed” -foregroundcolor green

Function to Exit PSSession

The final function is called Exit-Session which I am using to end the PSSession that I entered in the previous function.

Function Exit-Session
Write-Host “Exit PSSession” -foregroundcolor green
Return (Exit-PSSession)
Write-Host “All tests completed” -foregroundcolor green

How to Use the Functions

First let’s take a look at what servers.txt looks like


Note: For multiple server tests, add the ip address of each server one after the other like you see above.

Now let’s have a look at the file where all my functions sit so you have a better understanding how things fit together. All the functions are in the file names Master.ps1


Notice that all my functions are written one after the other.

As mentioned earlier I made another .ps1 file that calls the functions from master.ps1.


I then execute C:\MasterScript\executefunctions.ps1 in PowerShell.

This is what the output looks like in PowerShell.


It calls the function from the Master.ps1 file where all the functions are sitting.

The four files that were created during the process are saved in a folder in C drive:

They will contain the relevant information that you need.

FSPS.Outputfilelist21 e1432135909322


These tests are good when you are running them for a few servers only, but if a person is to test 100 servers, then it would be quite a headache to go and look at every single file.

The next steps would be to automate this process, where PowerShell informs the user of what files need to be seen, e.g. when a ping response fails the user should be informed, or when there is a critical error in one of the servers then the user is found, so the user only needs check specific files.


Writing the code took me a while, and I referred to multiple blogs and sites on the internet and in some cases PowerShell books also. Here are some links that can be helpful for a PowerShell beginner or someone who wants to learn more on PowerShell.

Latest from this author