Sign in
|
Join PowerShell.com!
|
Help
Home
PowerTips
Ask the Experts
Forums
Webcasts
Blogs
eBookV2
Script Library
Twitter Grid
Modules
QuickClick
Scripts
Snippets
Videos
Library
»
Script Library
»
SQL Server
»
Get-MSSQL-DB-UsingADO
Get-MSSQL-DB-UsingADO
Share
|
Browse Library
Module Library
QuickClick Library
Script Library
Active Directory
BizTalk
Citrix
Clustering
Desktop
Exchange Server 2003
Exchange Server 2007
File System
Group Policy
Internet Information Server (IIS)
Local Accounts
Logs
Microsoft Office
Microsoft Team Foundation Server
MySQL
Networking
Registry
Remote Desktop Services
Remoting
Security
SharePoint
SQL Server
System Center Virtual Machine Manager
System Center Configuration Manager
System Center Operations Manager
Tutorial
Terminal Server
Using .Net
Virtual Server
VMware
Windows 7
Windows HPC
Windows Server 2000
Windows Server 2003
Windows Server 2008
Windows XP
WMI
Misc
Snippet Library
Video Library
Members Only
Tags
Access
AD
ADO
Analysis Server
Attributes
Backup
CheckDB
Connect
Connection String
Create database
Create table
CSV
Database
Port
PowerShell
Properties
Service
SMO
SQL
SQL Authentication
SQL Server
stop jobs
test-connection
Windows Authentication
WMI
View more
Previous
|
Next
|
View all files
|
View Slideshow
Download
posted by
Richard Giles
10-06-2008
Downloads: 607
File size: 2kB
Views: 2,221
Embed
Get-MSSQL-DB-UsingADO
## =====================================================================
## Title
: Get-MSSQL-DB-UsingADO
## Description : Show all databases for a given server instance
## Author
: Idera
## Date
: 9/1/2008
## Input
: -serverInstance <server\instance>
##
-verbose
##
-debug
## Output
: List database ids and names
## Usage
: PS> . Get-MSSQL-DB-UsingADO -serverInstance MyServer -v -d
## Notes
: Adapted from Jakob Bindslet script
## Tag
: SQL Server, SMO, List databases
## Change log
:
## =====================================================================
param
(
[
string
]
$serverInstance
=
"."
,
[
switch
]
$verbose
,
[
switch
]
$debug
)
function
main()
{
if
(
$verbose
) {
$VerbosePreference
=
"Continue"
}
if
(
$debug
) {
$DebugPreference
=
"Continue"
}
Get-MSSQL
-
DB-UsingADO
$serverInstance
}
function
Get-MSSQL
-
DB-UsingADO
(
$serverInstance
)
{
# TIP: using PowerShell to create an exception handler
trap
[
Exception
]
{
write-error
$
(
"TRAPPED: "
+
$_
.
Exception.Message
);
continue
;
}
$adoOpenStatic
=
3
$adoLockOptimistic
=
3
# Create ADO connection and recordset objects
$adoConnection
=
New-Object
-comobject
ADODB.Connection
$adoRecordset
=
New-Object
-comobject
ADODB.Recordset
Write-Debug
"Opening connection..."
$adoConnection
.
Open
(
"Provider=SQLOLEDB;Data Source=$serverInstance;Initial Catalog=master;Integrated Security=SSPI"
)
# Run query to retrieve database ids and names
$query
=
"SELECT dbid, name FROM master.dbo.sysdatabases ORDER BY name"
$adoRecordset
.
Open
(
$query
,
$adoConnection
,
$adoOpenStatic
,
$adoLockOptimistic
)
$adoRecordset
.
MoveFirst
()
Write-Debug
"Retrieving results..."
do
{
$dbID
=
$adoRecordset
.
Fields.Item
(
"dbid"
).
Value
$dbName
=
$adoRecordset
.
Fields.Item
(
"name"
).
Value
Write-Output
"$dbID : $dbName"
$adoRecordset
.
MoveNext
()
}
until
(
$adoRecordset
.
EOF
-eq
$TRUE
)
$adoRecordset
.
Close
()
$adoConnection
.
Close
()
}
main
Filed under:
ADO
,
SQL Server
,
Database
Show all SQL Server databases for a given server instance.
Copyright 2012 PowerShell.com. All rights reserved.