Microsoft Windows PowerShell và SQL Server 2005 SMO – Phần 7
Phn 1 và phần 2 ca loạt bài này chúng tôi đã gii thiu về cài đặt PowerShell và
SMO đơn giản, WMI cmdlet. Trong phần 3 chúng tôi giới thiu về cách lập kịch
bản PowerShell để kết nối đến máy ch SQL Server.
Phn 4 gii thiuch sử dụng kịch bản PowerShell để thực hin hành động lặp
một file và kết nối các máy chủ khác. Phần 5 gii thiu cho các bạn cách tạo một
sở dữ liệu SQL Server bằng PowerShell và SMO. Phn 6 giới thiệu về việc
backup cơ sở dữ liệu SQL Server bằng PowerShell và SMO.
Mỗi một phần trong loạt bài này đều minh chứng cách sử dụng PowerShell kết hợp
với SMO để trìnhy các đối tượng của SQL Server.
Phương pháp 1: Hiển thị các tên bng
Chúng ta hãy giả dụ rằng mình muốn tìm tt cả các bảng đã có trong cơ sở dữ liệu
AdventureWorks”, trên máy chHOME\SQLEXPRESS”. Thực thi lệnh dưới
đây, tham khảo hình 1.1.
[reflection.assembly]:: LoadWithPartialName("Microsoft.SqlServer.Smo") | out-
null
$srv=New-Object "Microsoft.SqlServer.Management.Smo.Server"
"HOME\SQLEXPRESS"
$db = $srv.Databases["adventureworks"]
foreach ($tbl in $db.tables) {$tbl.name}
Hình 1.1
Cmdlets ở trên s hiển thị các tên bng trong cơ sở dữ liu AdventureWorks trên
máy chHOME\SQLEXPRESS” (xemnh 1.2)
Kết quả
AWBuildVersion
DatabaseLog
ErrorLog
Department
Employee
EmployeeAddress
EmployeeDepartmentHistory
EmployeePayHistory
JobCandidate
Shift
Address
AddressType
Contact
ContactType
CountryRegion
StateProvince
BillOfMaterials
Culture
Document
Illustration
Location
Product
ProductCategory
ProductCostHistory
ProductDescription
ProductDocument
ProductInventory
ProductListPriceHistory
ProductModel
ProductModelIllustration
ProductModelProductDescriptionCulture
ProductPhoto
ProductProductPhoto
ProductReview
ProductSubcategory
ScrapReason
TransactionHistory
TransactionHistoryArchive
UnitMeasure
WorkOrder
WorkOrderRouting
ProductVendor
PurchaseOrderDetail
PurchaseOrderHeader
ShipMethod
Vendor
VendorAddress
VendorContact
ContactCreditCard
CountryRegionCurrency
CreditCard
Currency
CurrencyRate
Customer
CustomerAddress
Individual
SalesOrderDetail
SalesOrderHeader
SalesOrderHeaderSalesReason
SalesPerson
SalesPersonQuotaHistory
SalesReason
SalesTaxRate
SalesTerritory
SalesTerritoryHistory
ShoppingCartItem
SpecialOffer
SpecialOfferProduct
Store
StoreContact
Hình 1.2
Phương pháp 2
Giả dụ rằng bạn muốn tìm tt cả các đốiợng đã có trong cơ sở dữ liệu
AdventureWorks”, trên máy chHOME\SQLEXPRESS”. Thực thi lệnh dưới
đây, tham khảo hình 1.3.
[reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo") | out-
null
$srv=New-Object "Microsoft.SqlServer.Management.Smo.Server"
"HOME\SQLEXPRESS"
$db = $srv.Databases["adventureworks"]
echo "Tables"
echo "------"
foreach ($tbl in $db.Tables) {$tbl.name}
echo "Synonyms"
echo "------"
foreach ($Synonyms in $db.Synonyms) {$Synonyms.name}
echo "Stored Procedures"
echo "------"
foreach ($StoredProcedures in $db.StoredProcedures) {$StoredProcedures.name}
echo "Assemblies"
echo "------"
foreach ($Assemblies in $db.Assemblies) {$Assemblies.name}
echo "UserDefined Functions"
echo "------"
foreach ($UserDefinedFunctions in $db.UserDefinedFunctions)
{$UserDefinedFunctions.name}
echo "Views"
echo "------"
foreach ($Views in $db.Views) {$Views.name}
echo "ExtendedStoredProcedures"
echo "------"
foreach ($ExtendedStoredProcedures in $db.ExtendedStoredProcedures)
{$ExtendedStoredProcedures.name}
Hình 1.3
Cmdlets ở trên shiển thị các tên đối tượng trong cơ sở dữ liu AdventureWorks
trên máy chủ “HOME\SQLEXPRESS” (xem hình 1.4)
Kết quả