Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 8
Ngu
ồn : quantrimang.com
The MA
K
Phần 1 và phần 2 của loạt bài này chúng tôi đã giới thiệu về cài đặt
PowerShell và SMO, WMI cmdlets đơn giản. Trong phần 3 chúng tôi đã giới
thiệu về cách lập kịch bản cho PowerShell và kết nối SQL Server.
Phần 4 đã giới thiệu cách sử dụng kịch bản PowerShell để lặp trong nội dung file
và kết nối các máy chủ khác nhau. Phần 5 là giới thiệu về cách tạo cơ sở dữ liệu
SQL Server bằng PowerShell và SMO. Phần 6 là nghiên cứu v
ấn đề backup một
cơ sở dữ liệu SQL Server bằng PowerShell và SMO và phần 7 là cách tạo một
danh sách các đối tượng trong một cơ sở dữ liệu.
Trong phần 8 này chúng tôi sẽ giới thiệu cho các bạn cách sử dụng PowerShell
kết hợp với SMO để hiển thị các thuộc tính đối tượng cho tất cả các đối tượng
SQL Server.
Phương pháp 1
Chúng ta hãy giả dụ rằng sẽ hiển thị đặ
c tính bảng của tất cả các bảng trong cơ
sở dữ liệu AdventureWorks từ máy chủ “HOME\SQLEXPRESS”. Thực thi
cmdlets dưới đây như thể hiện trong hình 1.1 bên dưới.
[System.Reflection.Assembly]::LoadWithPartialName
("Microsoft.SqlServer.Smo") | out-null
$srv=New-Object
"Microsoft.SqlServer.Management.Smo.Server" "HOME\SQLEXPRES
S"
RowCount : 701
TextFileGroup : PRIMARY
Events :
Microsoft.SqlServer.Management.Smo.TableEvents
Checks : {}
ForeignKeys :
{FK_Store_Customer_CustomerID,FK_Store_SalesPerson_SalesPer
sonID}
PartitionSchemeParameters : {}
RowCountAsDouble : 701
Triggers : {iStore}
Indexes : {AK_Store_rowguid,
IX_Store_SalesPersonID,PK_Store_CustomerID,PXML_Store_Demog
raphics}
Statistics : {AK_Store_rowguid,
IX_Store_SalesPersonID,PK_Store_CustomerID}
ExtendedProperties : {MS_Description}
Columns : {CustomerID, Name,
SalesPersonID,Demographics...}
FullTextIndex :
Schema : Sales
Name : Store
Urn :
Server[@Name='HOME\SQLEXPRESS']/Database[@Name='adventurewo
rks']
/Table[@Name='Store' and
@Schema='Sales']
Properties : {CreateDate, DataSpaceUsed,
FakeSystemTable,FileGroup...}
UserData :
{FK_StoreContact_Contact_ContactID,
FK_StoreContact_ContactType_ContactTypeID, FK_StoreCont
act_Store_CustomerID}
PartitionSchemeParameters : {}
RowCountAsDouble : 753
Triggers : {}
Indexes : {AK_StoreContact_rowguid,
IX_StoreContact_ContactID, IX_StoreContact_ContactTypeID,
PK_Stor
eContact_CustomerID_ContactID}
Statistics : {AK_StoreContact_rowguid,
IX_StoreContact_ContactID, IX_StoreContact_ContactTypeID,
PK_Stor
eContact_CustomerID_ContactID}
ExtendedProperties : {MS_Description}
Columns : {CustomerID, ContactID,
ContactTypeID, rowguid...}
FullTextIndex :
Schema : Sales
Name : StoreContact
Urn :
Server[@Name='HOME\SQLEXPRESS']/Database[@Name='adventurewo
rks']/Table[@Name='StoreContact'
and @Schema='Sales']
Properties : {CreateDate, DataSpaceUsed,
FakeSystemTable, FileGroup...}
UserData :
State : Existing
Hình 1.2
Phương pháp 2
echo "------"
foreach ($ExtendedStoredProcedures in $db)
{$ExtendedStoredProcedures}
Hình 1.3
Bằng cách thực thi cmdlets ở trên, bạn có thể thấy đặc tính đối tượng của tất cả
các đối tượng, kiểu đối tượng khác nhau trong cơ sở dữ liệu AdventureWorks
trên máy chủ “HOME\SQLEXPRESS” như thể hiện bên dưới. Xem hình 1.4.
Kết quả
ExtendedProperties : {MS_Description}
DatabaseOptions :
Microsoft.SqlServer.Management.Smo.DatabaseOptions
Synonyms : {}
Tables : {AWBuildVersion,
DatabaseLog, ErrorLog, Department...}
StoredProcedures :
{uspGetBillOfMaterials, uspGetEmployeeManagers,
uspGetManagerEmployees, uspGetWher
eUsedProductID...}
Assemblies : {}
UserDefinedTypes : {}
UserDefinedAggregates : {}
FullTextCatalogs : {}
Certificates : {}
SymmetricKeys : {}
AsymmetricKeys : {}
ExtendedStoredProcedures :
{sp_AddFunctionalUnitToComponent, sp_batch_params,
sp_bindsession, sp_control_dbma