Kiểm tra SQL Server bằng Windows PowerShell – Phần 4
Cập nhật lúc 10h57' ngày 06/10/2008

Kiểm tra SQL Server bằng Windows PowerShell – Phần 1
Kiểm tra SQL Server bằng Windows PowerShell – Phần 2
Kiểm tra SQL Server bằng Windows PowerShell – Phần 3

Muthusamy

Quản trị mạng - Phần 1 của loạt bài này đã giới thiệu về kiểm tra đầu tiên trên SQL Server – các ping một host. Phần 2 là giới thiệu về cách kiểm tra tất cả các dịch vụ của Windows có liên quan đến SQL Server và phần ba là cách kiểm tra các thông tin phần cứng và phần mềm. Trong phần tiếp theo này, chúng tôi sẽ giới thiệu cho các bạn các thu thập các thông tin về card mạng và ổ đĩa cứng từ máy chủ.

Bước 1

Đánh hoặc copy và paste đoạn mã dưới đây vào C:\CheckSQLServer\Checkhd.ps1.

#Function to check the HDD information on the host machine
Function checkHD([string] $Hostname )
{
$drives=get-wmiobject -class Win32_LogicalDisk 
  -computername $hostname -errorvariable errorvar
if (-not $errorvar)
{
foreach ($drive in $drives) 
{
if ($drive.drivetype -eq "3" )
{ 
$message= "DeviceID="+$drive.Deviceid+" Size="+ 
  $drive.size/1048576+"MB Free Space="+ 
  http://www.databasejournal.com/img/return.gif$drive.freespace/1048576 +"MB Percentage Used=" + 
  http://www.databasejournal.com/img/return.gif(($drive.Size/1048576)-($drive.freespace/1048576))/
  http://www.databasejournal.com/img/return.gif($drive.Size/1048576) *100+"% "
write-host $message -background "GREEN" -foreground "BLACk"
}
}
}
}

Bước 2

Đánh hoặc copy và paste đoạn mã dưới đây vào C:\CheckSQLServer\Checknet.ps1.

#Function to check the Network netadapter information on the 
  http://www.databasejournal.com/img/return.gifhost machine
Function checknet([string] $Hostname )
{
$netadapter=get-wmiobject -class win32_networkadapter 
  http://www.databasejournal.com/img/return.gif-computername $hostname -errorvariable errorvar
if (-not $errorvar)
{
foreach ($netadapter in $netadapter)
{
write-host "---------------------------------------------------" 
  http://www.databasejournal.com/img/return.gif-background "Blue" -foreground "BLACk"
#$message= "netadapter Enabled="+$netadapter.Enable
#write-host $message -background "GREEN" -foreground "BLACk"
$message= "netadapterType="+$netadapter.netadapterType
write-host $message -background "GREEN" -foreground "BLACk"
$message= "Description="+$netadapter.Description
write-host $message -background "GREEN" -foreground "BLACk"
$message= "Manufacturer="+$netadapter.Manufacturer
write-host $message -background "GREEN" -foreground "BLACk"
$message= "NetworkAddresses="+$netadapter.NetworkAddresses
write-host $message -background "GREEN" -foreground "BLACk"
$message= "PermanentAddress="+$netadapter.PermanentAddress
write-host $message -background "GREEN" -foreground "BLACk"
$message= "Physicalnetadapter="+$netadapter.Physicalnetadapter
write-host $message -background "GREEN" -foreground "BLACk"
$message= "ProductName="+$netadapter.ProductName
write-host $message -background "GREEN" -foreground "BLACk"
$message= "ServiceName="+$netadapter.ServiceName
write-host $message -background "GREEN" -foreground "BLACk"
$message= "StatusInfo="+$netadapter.StatusInfo
write-host $message -background "GREEN" -foreground "BLACk"
$message= "Speed="+$netadapter.Speed
write-host $message -background "GREEN" -foreground "BLACk"
$message= "Status="+$netadapter.Status
write-host $message -background "GREEN" -foreground "BLACk"
}
}
}

Bước 3

Gán vào file C:\CheckSQLServer\CheckSQL_Lib.ps1 đoạn mã dưới đây.

. ./checkhd.ps1
. ./checknet.ps1

Lúc này C:\CheckSQLServer\CheckSQL_Lib.ps1 sẽ có pinghost, checkservices, checkhardware, checkOS, checkHD và checknet như thể hiện bên dưới.

#Source all the functions relate to CheckSQL
. ./PingHost.ps1
. ./checkservices.ps1
. ./checkhardware.ps1
. ./checkOS.ps1
. ./checkHD.ps1
. ./checknet.ps1

Lưu ý: File CheckSQL_Lib.ps1 này sẽ được cập nhật với nguồn dẫn của các kịch bản mới như checkhd.ps1 và checknet.ps1.

Bước 4

Gắn thêm vào file C:\CheckSQLServer\CheckSQLServer.ps1 đoạn mã dưới đây.

Write-host "Checking HDD Information....."
Write-host "............................."
checkHD $Hostname
Write-host "Checking Network Adapter Information....."
Write-host "........................................."
checknet $Hostname

Lúc này C:\CheckSQLServer\CheckSQLServer.ps1 sẽ có cả checkhd và checknet như thể hiên dưới. Chúng tôi đã thêm một số tuyên bố write-host để hiện toàn bộ quá trình.

#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 "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
Write-host "Checking HDD Information....."
Write-host "............................."
checkHD $Hostname
Write-host "Checking Network Adapter Information....."
Write-host "........................................."
checknet $Hostname
 
}

Lưu ý: CheckSQLServer.ps1 sẽ được cập nhật các điều kiện mới và các tham số mới trong các phần sau của loạt bài này.

Nguồn dẫn cơ bản load các hàng được liệt kê trong file kịch bản và làm cho nó trở lên hiện hữu trong toàn bộ PowerShell session. Trong trường hợp này, chúng tôi dẫn nguồn một kịch bản, kịch bản này lại lấy nguồn từ nhiều kịch bản khác.

Bước 5

Chúng ta hãy thực thi kịch bản CheckSQLServer.ps1 bằng cách passing “Powerpc” host như một đối số, xem thể hiện dưới đây.

./CheckSQLServer.ps1 PowerServer2

Các kết quả bạn thu được như thể hiện bên dưới (tham khảo hình 1.0)

Checking SQL Server.....
........................
Arguments accepted : PowerServer2
........................
Pinging the host machine
........................
PowerServer2 is REACHABLE
Checking windows services on the host related to SQL Server
...........................................................
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=1492684
FreeSpaceInPagingFiles=2402648
FreeVirtualMemory=3948148
PAEEnabled=False
ServicePackMajorVersion=0
ServicePackMinorVersion=0
Checking HDD Information.....
.............................
DeviceID=C: Size=38143.99609375MB Free Space=23761.51953125MB 
Percentage Used=37.7057414937619
Checking Network Adapter Information.....
.........................................
---------------------------------------------------
netadapterType=
Description=WAN Miniport (SSTP)
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=WAN Miniport (SSTP)
ServiceName=RasSstp
StatusInfo=
Speed=
Status=
---------------------------------------------------
netadapterType=
Description=WAN Miniport (L2TP)
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=WAN Miniport (L2TP)
ServiceName=Rasl2tp
StatusInfo=
Speed=
Status=
---------------------------------------------------
netadapterType=
Description=WAN Miniport (PPTP)
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=WAN Miniport (PPTP)
ServiceName=PptpMiniport
StatusInfo=
Speed=
Status=
---------------------------------------------------
netadapterType=
Description=WAN Miniport (PPPOE)
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=WAN Miniport (PPPOE)
ServiceName=RasPppoe
StatusInfo=
Speed=
Status=
---------------------------------------------------
netadapterType=
Description=WAN Miniport (IPv6)
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=WAN Miniport (IPv6)
ServiceName=NdisWan
StatusInfo=
Speed=
Status=
---------------------------------------------------
netadapterType=
Description=WAN Miniport (Network Monitor)
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=WAN Miniport (Network Monitor)
ServiceName=NdisWan
StatusInfo=
Speed=
Status=
---------------------------------------------------
netadapterType=
Description=Intel(R) PRO/1000 MT Network Connection
Manufacturer=Intel
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=Intel(R) PRO/1000 MT Network Connection
ServiceName=E1G60
StatusInfo=
Speed=100000000
Status=
---------------------------------------------------
netadapterType=
Description=Microsoft ISATAP Adapter
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=Microsoft ISATAP Adapter
ServiceName=tunnel
StatusInfo=
Speed=100000
Status=
---------------------------------------------------
netadapterType=
Description=WAN Miniport (IP)
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=WAN Miniport (IP)
ServiceName=NdisWan
StatusInfo=
Speed=
Status=
---------------------------------------------------
netadapterType=
Description=Microsoft Tun Miniport Adapter
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=Microsoft Tun Miniport Adapter
ServiceName=tunmp
StatusInfo=
Speed=1073741824
Status=
---------------------------------------------------
netadapterType=
Description=RAS Async Adapter
Manufacturer=Microsoft
NetworkAddresses=
PermanentAddress=
Physicalnetadapter=
ProductName=RAS Async Adapter
ServiceName=AsyncMac
StatusInfo=
Speed=
Status=


Hình 1.0 

Từ các kết quả thu được, bạn có thể thấy được các thông tin về ổ đĩa cứng và card mạng.

Bước 6

Chúng ta hãy thực thhi kịch bản trên máy không có ổ đĩa cứng hay card mạng.

./CheckSQLServer.ps1 TestMachine

Kết quả mà bạn nhận được được thể hiện bên dưới (tham khảo hình 1.1)

Kết quả

Checking SQL Server.....
........................
Arguments accepted : TestMachine
........................
Pinging the host machine
........................
TestMachine is NOT reachable


Hình 1.1

Lưu ý: bạn có thể download mã cho phần 3 mới nhất ở đây.

Kết luận

Phần 4 này đã giới thiệu cho các bạn cách truy cập để thu thập các thông tin về ổ đĩa cứng và card mạng bằng Windows PowerShell và WMI-Object.

Văn Linh (Theo Databasejournal)
Đánh giá(?):
WinRAR 3.71
Phát hành: RARLAB
Download:301497
Dung lượng: 1.15 MB
Tìm thêm:winrar, rarlab, phần mềm nén, giải nén
 
Foxit Reader 2.3 Build 3309
Phát hành: Foxit Software
Download:34438
Dung lượng: 2.57 MB
Tìm thêm:foxit software, pdf, adobe acrobat reader, adobe, multimedia, miễn phí
 
WinZip 11.2
Phát hành: WinZip Computing
Download:26667
Dung lượng: 13.03 MB
Tìm thêm:winzip computing, trình nén, giải nén
 
WinRAR 3.80 beta 5
Phát hành: RARLAB
Download:25971
Dung lượng: 1.18 MB
Tìm thêm:winrar, rarlab, phần mềm nén, giải nén
 
Auslogics Disk Defrag 1.4.16
Phát hành: AusLogics
Download:13977
Dung lượng: 1.36 MB
Tìm thêm:auslogics, defrag, chống phân mảnh, ổ cứng, hard disk
Những chuyện kỳ bí