Kim traSQLServer bng Windows
PowerShell – Phn 3
Phn 1 ca lot bài này ã gii thiu cho các bn kim tra u tiên v SQL Server—cách ping
mt host. Trong phn 2 là gii thiu v cách kim tra tt c các dch v ca Windows có liên
quan vi SQL Server. Trong phn 3 này chúng tôi s gii thiu v cách tìm mt s thông tin v
phn cng và h iu hành t máy host.
Bc 1
ánh hoc Copy và paste on mã bên di vào file C:\CheckSQLServer\Checkhardware.ps1.
#Function to check Hardware information from a host
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"
}
}
Bc 2
ánh hoc Copy và paste on mã bên di vào file C:\CheckSQLServer\CheckOS.ps1.
#Function to check the OS information on the host machine
Function checkOS([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"
}
}
Bc 3
Gn thêm vào file C:\CheckSQLServer\CheckSQL_Lib.ps1 on mã bên di.
. ./checkhardware.ps1
. ./checkOS.ps1
Lúc này file C:\CheckSQLServer\CheckSQL_Lib.ps1 s có pinghost, checkservices,
checkhardware và checkOS nh th hin bên di.
#Source all the functions relate to CheckSQL
. ./PingHost.ps1
. ./checkservices.ps1
. ./checkhardware.ps1
. ./checkOS.ps1
Lu ý: File CheckSQL_Lib.ps1 này s c cp nht t các kch bn mi nh
checkhardware.ps1 và checkOS.ps1
Bc 4
Gn thêm vào file C:\CheckSQLServer\CheckSQLServer.ps1 on mã di ây.
checkhardware $Hostname
checkOS $Hostname
Lúc này file C:\CheckSQLServer\CheckSQLServer.ps1 s có c checkhardware và checkOS nh
bên di, Chúng tôi ã thêm môt tuyên b write-host hin toàn b quá trình.
#Objective: To check various status of SQLServer
#Host, instances and databases.
#Author: MAK
#Date Written: June 5, 2008
param (
[string] $Hostname
)
. ./CheckSQL_Lib.ps1
Write-host "Checking SQLServer "
Write-host " "
Write-host " "
Write-host "Arguments accepted : $Hostname"
write-host " "
Write-host "Pinging the host machine"
write-host " "
PingHost $Hostname
Write-host "Checking 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
Lu ý: File CheckSQLServer.ps1 này s c cp nht các iu kin và các tham s mi trong
các phn sau ca lot bài.
Ngun dn s load các hàm c lit kê trong file kch bn và làm cho nó hin hu trong toàn b
PowerShell session. Trong trng hp này, chúng tôi dn ngun mt kch bn, kch bn này s
li ly ngun t nhiu kch bn khác.
Bc 5
Lúc này chúng ta hãy thc thi kch bn, CheckSQLServer.ps1 bng cách passing “Powerpc” host
vi t cách mt i s nh th hin bên di.
./CheckSQLServer.ps1 PowerServer2
Khi ó kt qu cho ra s nh sau (tham kho hình 1.0)
Checking SQLServer
Arguments accepted : PowerServer2
Pinging the host machine
PowerServer2 is REACHABLE
Checking windows services on the host related to SQLServer
Host=PowerServer2 MSSQLSERVER Running OK True .\Administrator
Host=PowerServer2 MSSQLServerADHelper100 Stopped OK False NT
AUTHORITY\NETWORK SERVICE
Host=PowerServer2 MSSQLServerOLAPService Stopped OK False .\Administrator
Host=PowerServer2 SQLBrowser Stopped OK False NT AUTHORITY\LOCAL 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
Hình 1.0
T kt qu nhn c, bn có th thy c các thông tin v phn cng và h iu hành.
Bc 6
Hãy thc thi kch bn trên máy không tn ti nh di ây.
./CheckSQLServer.ps1 TestMachine
Khi ó kt qu nhn c s nh sau (xem hình 1.1)
Kt qu
Checking SQLServer
Arguments accepted : TestMachine
Pinging the host machine
TestMachine is NOT reachable
Checking windows services on the host related to SQLServer
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\checksqlserver\checkservices.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:\checksqlserver\checkhardware.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:\checksqlserver\checkOS.ps1:5 char:18
+ $os=get-wmiobject <<<< -class win32_operatingsystem -computername $hostname -
errorvariable errorvar
Hình 1.1
Bc 7
Chúng ta không phi tip tc vi checkservices, checkhardware và checkos nu ping b tht bi.
Chính vì vy hãy cp nht pinghost.ps1 nh th hin bên di.
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"
}
}
Hãy cp nht checksqlserver.ps1 nh th hin bên di.
#Objective: To check various status of SQLServer
#Host, instances and databases.
#Author: MAK
#Date Written: June 5, 2008
param (
[string] $Hostname
)
$global:errorvar=0
. ./CheckSQL_Lib.ps1
Write-host "Checking SQLServer "
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 "Checking 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
}
Lúc này hãy thc thi kch bn bng cách passing tên máy “testmachine”, tên thc s không tn
ti vi t cách mt i s.
./CheckSQLServer.ps1 TestMachine
Kt qu
Checking SQLServer
Arguments accepted : TestMachine
Pinging the host machine
TestMachine is NOT reachable
Lu ý bn có th download các an mã mi nht trong phn ba ti ây.
Kt lun
ây là phn th ba ca lot bài này. Trong phn th ba này chúng tôi ã gii thiu c cho các
bn cách truy cp các thông tin phn cng và h iu hành bng WindowsPowerShell và WMI-
Object. Trong phn tip theo, chúng tôi s b sung thêm mt s kim tra khác và gii thiu cách
capture mt s thông tin phn cng và h iu hành
.
Kim tra SQL Server bng Windows
PowerShell – Phn 3
Phn 1 ca lot bài này ã gii thiu cho các bn kim tra u tiên v SQL Server cách ping. services on the host related to SQL Server
Host=PowerServer2 MSSQLSERVER Running OK True .Administrator
Host=PowerServer2 MSSQLServerADHelper100 Stopped