Test SQL Server with Windows PowerShell - Part 3
Test SQL Server with Windows PowerShell - Part 1
Test SQL Server with Windows PowerShell - Part 2
Muthusamy
Network Administration - Part 1 of this series showed you the first check on SQL Server — how to ping a host. In Part 2, I will show you how to check all Windows services related to SQL Server. In Part 3, I will show you how to find some hardware and operating system information from the host machine.
Step 1
Type or Copy and paste the code below into the C: CheckSQLServerCheckhardware.ps1 file.
#Function để kiểm tra thông tin cứng từ một máy phục vụ
Function checkhardware ([string] $ Hostname)
{
$ computer = get-wmiobject -class win32_computersystem -computername
$ hostname -errorvariable errorvar
$ errorvar.size
if (-not $ errorvar)
{
$ message = "Host =" + $ Hostname
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "Description =" + $ computer.Description
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "NumberOfLogicalProcessors =" +
$ computer.NumberOfLogicalProcessors
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "NumberOfProcessors =" + $ computer.NumberOfProcessors
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "TotalPhysicalMemory =" + $ computer.TotalPhysicalMemory
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "Model =" + $ computer.Model
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "Manufacturer =" + $ computer.Manufacturer
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "PartOfDomain =" + $ computer.PartOfDomain
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "CurrentTimeZone =" + $ computer.CurrentTimeZone
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "DaylightInEffect =" + $ computer.DaylightInEffect
write-host $ message -background "GREEN" -foreground "BLACk"
}
}
Step 2
Type or Copy and paste the code below into file C: CheckSQLServerCheckOS.ps1.
#Function để kiểm tra các thông tin thông tin về máy host
CheckOS function ([string] $ Hostname)
{
$ os = get-wmiobject -class win32_operatingsystem -computername $ hostname -errorvariable errorvar
if (-not $ errorvar)
{
$ message = "OSArchitecture =" + $ os.OSArchitecture
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "OSLanguage =" + $ os.OSLanguage
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "OSProductSuite =" + $ os.OSProductSuite
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "OSType =" + $ os.OSType
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "BuildNumber =" + $ os.BuildNumber
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "BuildType =" + $ os.BuildType
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "Version =" + $ os.Version
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "WindowsDirectory =" + $ os.WindowsDirectory
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "PlusVersionNumber =" + $ os.PlusVersionNumber
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "FreePhysicalMemory =" + $ os.FreePhysicalMemory
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "FreeSpaceInPagingFiles =" + $ os.FreeSpaceInPagingFiles
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "FreeVirtualMemory =" + $ os.FreeVirtualMemory
write-host $ message -background "GREEN" -foreground "BLACk"
$ message = "PAEEnabled =" + $ os.PAEEnabled
write-host $ message -background "GREEN" -foreground "BLACk"
}
}
Step 3
Append to file C: CheckSQLServerCheckSQL_Lib.ps1 code below.
. ./checkhardware.ps1
. ./checkOS.ps1
Now the file C: CheckSQLServerCheckSQL_Lib.ps1 will have pinghost, checkservices, checkhardware and checkOS as shown below.
#Source all the relate to functions CheckSQL
. ./PingHost.ps1
. ./checkservices.ps1
. ./checkhardware.ps1
. ./checkOS.ps1
Note: This CheckSQL_Lib.ps1 file will be updated from new scripts such as checkhardware.ps1 and checkOS.ps1
Step 4
Append to the file C: CheckSQLServerCheckSQLServer.ps1 code below.
checkhardware $ Hostname
checkOS $ Hostname
Now file C: CheckSQLServerCheckSQLServer.ps1 will have both checkhardware and checkOS as below, We have added a write-host statement to show the whole process.
#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
Write-host "Checking SQL Server ."
Write-host "...."
"Write-host"
Write-host "Arguments accepted: $ Hostname"
write-host "...."
Write-host "Pinging the host machine"
write-host "...."
PingHost $ Hostname
Write-host "Check windows services on the host related to SQL Server"
write-host "........ ... "
checkservices $ Hostname
Write-host "Checking hardware Information ."
Write-host "...."
checkhardware $ Hostname
Write-host "Checking OS Information ."
Write-host "...."
checkOS $ Hostname
Note: This CheckSQLServer.ps1 file will be updated with new conditions and parameters in later sections of the series.
The source will load the functions listed in the script file and make it available during the entire PowerShell session. In this case, we source a scenario, which will be sourced from many other scenarios.
Step 5
Now let's execute the script, CheckSQLServer.ps1 by passing 'Powerpc' host as an argument as shown below.
./CheckSQLServer.ps1 PowerServer2
Then the result will be as follows (refer to Figure 1.0)
Checking SQL Server .
....
Arguments accepted: PowerServer2
....
Pinging the host machine
....
PowerServer2 is REACHABLE
Check windows services on the host related to SQL Server
......... ..
Host = PowerServer2 MSSQLSERVER Running OK True .Administrator
Host = PowerServer2 MSSQLServerADHelper100 Stopped OK False NT AUTHORITYNETWORK SERVICE
Host = PowerServer2 MSSQLServerOLAPService Stopped OK False .Administrator
Host = PowerServer2 SQLBrowser Stopped OK False NT AUTHORITYLOCAL SERVICE
Host = PowerServer2 SQLSERVERAGENT Stopped OK False .Administrator
Host = PowerServer2 SQLWriter Stopped OK False LocalSystem
Checking hardware Information .
....
Host = PowerServer2
Description = AT / AT COMPATIBLE
NumberOfLogicalProcessors = 2
NumberOfProcessors = 1
TotalPhysicalMemory = 2145738752
Model = OptiPlex GX270
Manufacturer = Dell Computer Corporation
PartOfDomain = True
CurrentTimeZone = -240
DaylightInEffect = True
Checking OS Information .
....
OSArchitecture = 32-bit
OSLanguage = 1033
OSProductSuite = 274
OSType = 18
BuildNumber = 6001
BuildType = Multiprocessor Free
Version = 6.0.6001
WindowsDirectory = C: Windows
PlusVersionNumber =
FreePhysicalMemory = 1511144
FreeSpaceInPagingFiles = 2402648
FreeVirtualMemory = 3966452
PAEEnabled = False
Figure 1.0
From the results, you can see hardware and operating system information.
Step 6
Please execute the script on the machine that does not exist as shown below.
./CheckSQLServer.ps1 TestMachine
Then the results will be as follows (see Figure 1.1)
Result
Checking SQL Server .
....
Arguments accepted: TestMachine
....
Pinging the host machine
....
TestMachine is NOT reachable
Check windows services on the host related to SQL Server
......... ..
Get-WmiObject: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C: checksqlservercheckservices.ps1: 5 char: 24
+ $ Services = get-wmiobject <<<< -class win32_service -computername $ hostname | where {$ _. name -like '* SQL *'} | select-obj
ect Name, state, status, Started, Startname, Description
Host = TestMachine
Checking hardware Information .
....
Get-WmiObject: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C: checksqlservercheckhardware.ps1: 5 char: 24
+ $ computer = get-wmiobject <<<< -class win32_computersystem -computername $ hostname -errorvariable errorvar
Checking OS Information .
....
Get-WmiObject: The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C: checksqlservercheckOS.ps1: 5 char: 18
+ $ os = get-wmiobject <<<< -class win32_operatingsystem -computername $ hostname -errorvariable errorvar
Figure 1.1
Step 7
We do not have to continue with checkservices, checkhardware and checkos if ping fails. So update pinghost.ps1 as shown below.
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
{
$ global: errorvar = "host not reachable"
write-host $ Hostname is NOT reachable -background "RED" -foreground "BLACk"
}
}
Update the checksqlserver.ps1 as shown below.
#Objective: To check various status of SQL Server
#Host, instances and databases.
#Author: MAK
#Date Written: June 5, 2008
param (
[string] $ Hostname
)
$ global: errorvar = 0
. ./CheckSQL_Lib.ps1
Write-host "Checking SQL Server ."
Write-host "...."
"Write-host"
Write-host "Arguments accepted: $ Hostname"
write-host "...."
Write-host "Pinging the host machine"
write-host "...."
pinghost $ Hostname
if ($ global: errorvar -ne "host not reachable")
{
Write-host "Check windows services on the host related to SQL Server"
write-host "........ ... "
checkservices $ Hostname
Write-host "Checking hardware Information ."
Write-host "...."
checkhardware $ Hostname
Write-host "Checking OS Information ."
Write-host "...."
checkOS $ Hostname
}
Now execute the script by passing the name 'testmachine', the name does not really exist as an argument.
./CheckSQLServer.ps1 TestMachine
Result
Checking SQL Server .
....
Arguments accepted: TestMachine
....
Pinging the host machine
....
TestMachine is NOT reachable
Note that you can download the latest code in the third section here.
Conclude
This is the third part of this series. In this third part, I have shown you how to access hardware and operating system information using Windows PowerShell and WMI-Object. In the next section, we will add some other checks and introduce how to capture some hardware and operating system information.
You should read it
- Test SQL Server with Windows PowerShell - Part 7
- Test SQL Server with Windows PowerShell - Part 2
- Test SQL Server with Windows PowerShell - Part 5
- Test SQL Server with Windows PowerShell - Part 6
- 10 tips with PowerShell in Windows Server 2008 - Part 1
- How to create and run a PowerShell script file on Windows 10
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 5
- 10 tips with PowerShell in Windows Server 2008 - Part 2
May be interested
- Microsoft Windows PowerShell and SQL Server 2005 SMO - Part 3part 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 11part 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 1in 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 4part 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 2012what 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 9in 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 10in 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 8in 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 1as 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/7powershell 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.