Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 1

The MAK

Như bạn có lẽ đã biết, Windows PowerShell là tiện ích dòng lệnh mới và là ngôn ngữ kịch bản cung cấp một môi trường dòng lệnh cho việc khảo sát tương tác và quản trị của máy tính. Ngoài ra, nó cung cấp một khả năng viết kịch bản cho các lệnh này vì thế mà chúng tôi có thể hoạch định và chạy chúng nhiều lần.

Windows PowerShell phụ thuộc vào .NET framework 2.0.

SQL Server Management Objects, được biết đến như SMO, là một mô hình đối tượng cho SQL Server và thiết lập cấu hình của nó. Ứng dụng nền tảng SMO sử dụng các ngôn ngữ .NET Framework để lập trình chống lại mô hình đối tượng bộ nhớ.

Trong loạt bài này, tôi sẽ chứng minh sức mạnh của Windows PowerShell phối hợp với SQL Server 2005. Cụ thể là trong phần I này, chúng tôi sẽ chỉ dẫn cách thiết lập và sử dụng lệnh một lệnh PowerShell và SMO đơn giản.

Giả thiết

a. Máy tính đã cài đặt .NET 2.0

b. Máy tính đã cài đặt phiên bản gói dịch vụ SQL Server 2005 mới nhất

Tải về và cài đặt Microsoft PowerShell

a. Tải về file cài đặt Microsoft PowerShell “WindowsXP-KB926139-x86-ENU.exe” từ địa chỉ http://download.microsoft.com

b. Cài đặt PowerShell lên máy tính

Bước 1: Kích đúp vào file chạy “WindowsXP-KB926139-x86-ENU.exe”. [Hình 1.0]


Hình 1

Bước 2: Kích “Run”. [Hình 1.1]


Hình 2

Bước 3: Kích “Next”. [Hình 1.2]


Hình 3

Bước 4: Chọn tùy chọn “I agree”. [Hình 1.3]


Hình 4

Bước 5: Chờ quá trình cài đặt. [Hình 1.4]


Hình 5

Bước 6: Kích Finish. [Hình 1.5]


Hình 6

Chạy PowerShell

Có một số cách để chạy PowerShell. Một phương pháp là tìm tới dấu nhắc lệnh và nhập lệnh PowerShell. [Hình 1.6]

PowerShell


Hình 7

Lệnh PowerShell xuất hiện. [Hình 1.7]


Hình 8

Cách khác, bạn có thể chạy PowerShell bằng cách chọn Programs-Windows PowerShell 1.0-Windows PowerShell. [Hình 1.8]


Hình 9

Lệnh Help

Trong các cửa sổ PowerShell, bạn có thể truy cập vào danh sách các lệnh bằng cách nhập lệnh

Get-command

Lệnh này sẽ hiển thị toàn bộ các lệnh có sẵn trong PowerShell. [Hình 1.9]

Add-Content
Add-History
Add-Member
Add-PSSnapin
Clear-Content
Clear-Item
Clear-ItemProperty
Clear-Variable
Compare-Object
ConvertFrom-SecureString
Convert-Path
ConvertTo-Html
ConvertTo-SecureString
Copy-Item
Copy-ItemProperty
Export-Alias
Export-Clixml
Export-Console
Export-Csv
ForEach-Object
Format-Custom
Format-List
Format-Table
Format-Wide
Get-Acl
Get-Alias
Get-AuthenticodeSignature
Get-ChildItem
Get-Command
Get-Content
Get-Credential
Get-Culture
Get-Date
Get-EventLog
Get-ExecutionPolicy
Get-Help
Get-History
Get-Host
Get-Item
Get-ItemProperty
Get-Location
Get-Member
Get-PfxCertificate
Get-Process
Get-PSDrive
Get-PSProvider
Get-PSSnapin
Get-Service
Get-TraceSource
Get-UICulture
Get-Unique
Get-Variable
Get-WmiObject
Group-Object
Import-Alias
Import-Clixml
Import-Csv
Invoke-Expression
Invoke-History
Invoke-Item
Join-Path
Measure-Command
Measure-Object
Move-Item
Move-ItemProperty
New-Alias
New-Item
New-ItemProperty
New-Object
New-PSDrive
New-Service
New-TimeSpan
New-Variable
Out-Default
Out-File
Out-Host
Out-Null
Out-Printer
Out-String
Pop-Location
Push-Location
Read-Host
Remove-Item
Remove-ItemProperty
Remove-PSDrive
Remove-PSSnapin
Remove-Variable
Rename-Item
Rename-ItemProperty
Resolve-Path
Restart-Service
Resume-Service
Select-Object
Select-String
Set-Acl
Set-Alias
Set-AuthenticodeSignature
Set-Content
Set-Date
Set-ExecutionPolicy
Set-Item
Set-ItemProperty
Set-Location
Set-PSDebug
Set-Service
Set-TraceSource
Set-Variable
Sort-Object
Split-Path
Start-Service
Start-Sleep
Start-Transcript
Stop-Process
Stop-Service
Stop-Transcript
Suspend-Service
Tee-Object
Test-Path
Trace-Command
Update-FormatData
Update-TypeData
Where-Object
Write-Debug
Write-Error
Write-Host
Write-Output
Write-Progress
Write-Verbose
Write-Warning


Hình 10

Một SMO đơn giản trong PowerShell

Hãy truy vấn sys.sydatabases của SQL Server trường hợp “SQLEXPRESS” từ máy chủ “HOME” sử dụng PowerShell và SMO.

Bước 1: Vào cửa sổ lệnh: Startruncmd

Bước 2: Bắt đầu PowerShell bằng cách nhập lệnh

PowerShell

Bước 3: Thực hiện các lệnh, từng bước một, như sau

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") "HOME\SQLEXPRESS"
foreach($database in $Server.databases) {$database.name}

Bạn sẽ thu được kết quả như hình dưới. [Hình 2.0]


Hình 11

Các thành phần SMO

Để hiển thị tất cả các thành phần liên quan đến đối tượng $Server, thực hiện lệnh dưới đây.

Bước 1: Vào cửa sổ lệnh: Startruncmd

Bước 2: Bắt đầu PowerShell bằng cách nhập lệnh

PowerShell

Bước 3: Thực hiện các lệnh, từng bước một, như sau

[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
$Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") "HOME\SQLEXPRESS"
$server | get-member

Bạn sẽ thu được kết quả như hình dưới. [Hình 2.1]

Alter
AttachDatabase
CompareUrn
DeleteBackupHistory
Deny
DetachDatabase
DetachedDatabaseInfo
EnumAvailableMedia
EnumCollations
EnumDatabaseMirrorWitnessRoles
EnumDetachedDatabaseFiles
EnumDetachedLogFiles
EnumDirectories
EnumErrorLogs
EnumLocks
EnumMembers
EnumObjectPermissions
EnumPerformanceCounters
EnumProcesses
EnumServerAttributes
EnumServerPermissions
EnumStartupProcedures
EnumWindowsDomainGroups
EnumWindowsGroupInfo
EnumWindowsUserInfo
Equals
GetActiveDBConnectionCount
GetDefaultInitFields
GetHashCode
GetPropertyNames
GetSmoObject
GetType
get_ActiveDirectory
get_BackupDevices
get_Configuration
get_ConnectionContext
get_Credentials
get_Databases
get_DefaultTextMode
get_Endpoints
get_Events
get_FullTextService
get_Information
get_InstanceName
get_JobServer
get_Languages
get_LinkedServers
get_Logins
get_Mail
get_Name
get_NotificationServices
get_Properties
get_ProxyAccount
get_ReplicationServer
get_Roles
get_ServiceMasterKey
get_Settings
get_State
get_SystemDataTypes
get_SystemMessages
get_Triggers
get_Urn
get_UserData
get_UserDefinedMessages
get_UserOptions
Grant
Initialize
IsDetachedPrimaryFile
IsWindowsGroupMember
KillAllProcesses
KillDatabase
KillProcess
PingSqlServerVersion
ReadErrorLog
Refresh
Revoke
SetDefaultInitFields
set_DefaultTextMode
set_UserData
ToString
ActiveDirectory
BackupDevices
Configuration
ConnectionContext
Credentials
Databases
DefaultTextMode
Endpoints
Events
FullTextService
Information
InstanceName
JobServer
Languages
LinkedServers
Logins
Mail
Name
NotificationServices
Properties
ProxyAccount
ReplicationServer
Roles
ServiceMasterKey
Settings
State
SystemDataTypes
SystemMessages
Triggers
Urn
UserData
UserDefinedMessages
UserOptions


Hình 12

Kết luận

Như đã giải thích từ đầu bài viết, loạt bài này sẽ minh họa sức mạnh Windows PowerShell phối hợp với SQL Server 2005. Trong loạt bài này, tôi sẽ chứng minh sức mạnh của Windows PowerShell phối hợp với SQL Server 2005. Phần I của loạt này đã chỉ dẫn cách thiết lập và sử dụng một lệnh PowerShell và SMO đơn giản.

Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 2
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 3
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 4
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 5
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 6
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 7
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 8
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 9
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 10
Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 11

Thứ Ba, 04/12/2007 09:31
53 👨 4.319
0 Bình luận
Sắp xếp theo
    ❖ Tổng hợp