Test SQL Server with Windows PowerShell - Part 1

Muthusamy

Network Administration - This series will introduce you to the methods and procedures to check the current state of the operating system, instances of SQL Server and database, . with Windows PowerShell.

Instead of using Vbscript, bat files, or sql client implementations, . we'll show you the power of Windows PowerShell in checking SQL Servers status.

Prerequisites

  1. Install .Net 2.0
  1. On the client computer, you need to install Windows PowerShell 1.0.
  1. Your login needs to have permission to create folders and files in the client.

Before going into the actual SQL Server test, we want to introduce a bit of platform and build directories, libraries, .

At the end of this series, there will be a Powershell library with many functions used as a source for any PowerShell script. One, many or all of these functions can be called from any PowerShell script when the library is used as the source.

Step 1

Launch Windows PowerShell by executing the command below (Figure 1.0)

 % SystemRoot% system32WindowsPowerShellv1.0powershell.exe 

Test SQL Server with Windows PowerShell - Part 1 Picture 1
Figure 1.0: Launch PowerShell

Step 2

Create a directory using the PowerShell command below. This directory will be used exclusively for PowerShell scripts, libraries, and functions that are related to testing SQL Server. Execute the command given below (see Figure 1.1)

 New-Item -Path C: -Name CheckSQLServer -Type directory 

You can see the result of executing the command below. Basically, the CheckSQLServer folder has been successfully created.

Test SQL Server with Windows PowerShell - Part 1 Picture 2
Figure 1.1 Create new folder

Step 3

Navigate to the CheckSQLServer folder and then create other files using the following PowerShell command (see Figure 1.3).

 set-location C: CheckSQLServer 
Notepad CheckSQL_Lib.ps1
Notepad CheckSQLServer.ps1
Notepad Pinghost.ps1

You can see that the location is changed to C: CheckSQLServer and also opened three notepad windows to be able to edit the files CheckSQL_Lib.sql, CheckSQLServer.Ps1 and PingHost.ps1.

Test SQL Server with Windows PowerShell - Part 1 Picture 3
Figure 1.2: Create a PowerShell script and folder

Note: Notepad is an editor that we use for editing PowerShell scripts. You can completely customize other editors for you personally.

Step 4

Type or copy / paste the following code into the notepad editor that opened PingHost.ps1 as shown below (see Figure 1.3)

 Function Pinghost ([string] $ Hostname) 
{
$ status = get-wmiobject win32_pingstatus -Filter "Address = '$ Hostname'" | Select-Object statuscode
if ($ status.statuscode -eq 0)
{write-host $ Hostname is REACHABLE -background "GREEN" -foreground "BLACk"}
else
{write-host $ Hostname is NOT reachable -background "RED" -foreground "BLACk"}
}

Test SQL Server with Windows PowerShell - Part 1 Picture 4
Figure 1.3: PingHost.ps1 script file

Save the Pinghost.ps1 file and exit notepad.

Step 5

Type or copy / paste the following code into the notepad editor that opened CheckSQL_Lib.ps1 as shown below (see Figure 1.4).

 #Source all the relate to functions CheckSQL 
. ./PingHost.ps1

Test SQL Server with Windows PowerShell - Part 1 Picture 5
Figure 1.4: CheckSQL_Lib.ps1

Save the file CheckSQL_Lib.ps1 and exit notepad.

Note : This CheckSQL_Lib.ps1 will be updated with sources of new scripts like PingHost.PS1

Basically the source will load the functions listed in the script file and make it available throughout the PowerShell session.

Step 6

Type or copy / paste the following code into the notepad editor that opened CheckSQLServer.ps1 as shown below (see Figure 1.5).

 #Objective: To check various status of SQL Server 
#Host, instances and databases.
#Author: MAK
#Date Written: June 5, 2008
param (
[string] $ Hostname
)
. ./CheckSQL_Lib.ps1
PingHost $ Hostname

Test SQL Server with Windows PowerShell - Part 1 Picture 6
Figure 1.5: CheckSQLServer.ps1

Save the file CheckSQLServer.ps1 and exit notepad

Note : This CheckSQLServer.ps1 file will be updated with new conditions and parameters in later sections of this series.

The source will load the functions listed in the script file and make it available to the entire PowerShell session. In this case, we will source a script that uses multiple sources from other scenarios.

Step 7

Execute CheckSQLServer.ps1 as shown below (see Figure 1.6).

 ./CheckSQLServer.ps1 PowerMachine 
./CheckSQLServer.ps1 TestServer

We will see the results, based on which you will know if the computer has ping capability. If the machine can be probed by ping, the message will be marked in green and it will be marked in red.

Test SQL Server with Windows PowerShell - Part 1 Picture 7
Figure 1.6: Pinging the server

By default, PowerShell scripts cannot be executed on the machine if you use it for the first time. If you encounter the following error message as shown in Figure 1.7, execute the command to enable unrestricted execution of PowerShell script.

Test SQL Server with Windows PowerShell - Part 1 Picture 8
Figure 1.7: Error in executing PowerShell script

 set-executionpolicy unrestricted 

Note: You do not have to execute the above command multiple times but only execute it once.
You can check the execution policy of the current PowerShell configuration by executing the following command (shown in Figure 1.8).

 get-executionpolicy 

Test SQL Server with Windows PowerShell - Part 1 Picture 9
Figure 1.8: Enforcement policy

Conclude

This is the first part of this series. This first part has shown you how to create a PowerShell script to ping a server. It also introduces how to use the PowerShell function source and how to call the function. In the later part of this series, we will continue to dive deeper into other Windows PowerShell features in checking the status of SQL Server.

5 ★ | 1 Vote

May be interested

  • Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 3Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 3
    part i and part ii of this series showed powershell and smo settings, simple wmi cmdlets. this part 3 will cover how to write code for the powershell cmdlet and execute them. script code is essential for automated operations and
  • Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 11Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 11
    part 10 of this article series showed how to use powershell scripts in conjunction with smo and parameters to create sql server scripts. in this section, we will show you how to use powershell cmdlets in conjunction with the sql server client and output saving to export to a text file or xml file.
  • 10 tips with PowerShell in Windows Server 2008 - Part 110 tips with PowerShell in Windows Server 2008 - Part 1
    in fact, there are a lot of windows server 2008 tasks that we can do a lot faster with powershell than the gui-based application or tool. in the following article, we will introduce you some basic and most frequently used operations with powershell ...
  • Microsoft Windows Power Shell and SQL Server 2005 SMO - Part 4Microsoft Windows Power Shell and SQL Server 2005 SMO - Part 4
    part i and part ii of this series showed simple power shell settings, smo and wmi cmdlets. part iii instructs writing powershell and connecting to sql server. part 4 will show you how to use powershell code to iterate file content and connect to other servers.
  • Instructions on how to use PowerShell in Windows Server 2012Instructions on how to use PowerShell in Windows Server 2012
    what is powershell? windows powershell is a command-line shell language interpreter and scripting language specifically designed for system administrators. built on the .net framework, windows powershell helps it professionals control and automate windows operating system administration tasks as well as applications running on windows server environments.
  • Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 9Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 9
    in part 9 of this series, i will show you how to use powershell in conjunction with smo to create a sql server script. creating sql server scripts is an important task for administrators and sql server database development professionals.
  • Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 10Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 10
    in part 10, i will show you how to use powershell scripts in conjunction with smo and parameters to create sql server scripts. creating sql server scripts is an important task for administrators and sql server database development professionals.
  • Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 8Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 8
    in this article, i will show you how to use powershell in conjunction with smo to display object properties for all sql server objects.
  • Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 1Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 1
    as you probably know, windows powershell is a new command-line utility and a scripting language that provides a command-line environment for computer interaction and administration surveys. in addition, it provides a scripting capability for fish
  • How to install PowerShell 7.0 in Windows 10/8/7How to install PowerShell 7.0 in Windows 10/8/7
    powershell 7 is the latest major update for powershell. powershell includes command line shells, object-oriented programming languages, along with a set of tools for script / cmdlet execution and module management.