Solved

How to retrieve all user databases using powershell

Posted on 2013-11-15
6
301 Views
Last Modified: 2013-11-20
Given a server name I need to retrieve all the user databases on that sql server. The code below:

$srv=New-Object "Microsoft.SqlServer.Management.Smo.Server" "$ServerName"
$svr.Databases

Open in new window


only retrieves 5 system databases but none of the user ones. Can anyne help?
0
Comment
Question by:YZlat
6 Comments
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39652496
Hi,
which Powershell version and which SQL Server version and edition?

Can you confirm that if you logon to SQL Server (e.g. using SQL Server Management Studio) with the same account with whom you run the Powershell script, that you cannot see the user databases as well?

So I guess either permission issue, wrong version of SMO (not the same as the version of SQL Server) or an issue with the script.
Could you post your whole script?

Thanks and HTH
Rainer
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39653006
this will get all DB names from mssql

select name from sys.databases

Open in new window


with powershell


[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "LOCALHOST\SQLSERVER"

$dbs=$s.Databases
$dbs | Get-Member -MemberType Property 

Open in new window

0
 
LVL 69

Accepted Solution

by:
Qlemo earned 250 total points
ID: 39653793
I agree, it should work if you've got the proper privileges for the Windows account used. I've tested with this code and an admin account (LoadWithPartialName is sooo old-style):
Add-Type -Assembly Microsoft.SqlServer.SMO
$srv = New-Object Microsoft.Sqlserver.Management.SMO.Server "$server"
$srv.Databases | select Owner, Name, IsSystemObject

Open in new window

and got all info as expected.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 35

Author Comment

by:YZlat
ID: 39662535
RainerJ, I login to SQL Server using my windows account and I run powershell as myself.

QuinnDex, that's exactly what I use but only gets me system databases

Qlemothat does not help - same code I use and only system dbs come up. Perhaps it is something with permissions. But I do run powershell with my windows account. I will try runing it as admin
0
 
LVL 35

Author Comment

by:YZlat
ID: 39663082
OK, so I just tried and

$srv.Databases | select Owner, Name, IsSystemObject

worked for me. The thing I really wanted is to put all the database names into an array.

I tried this to just print out db names to the screen so I can later manipulate them but it didn't work:

$svr.Databases |% {
     Write-Host $_.Name
}

I also tried

$svr.Databases | Select Name |% {
     Write-Host $_.Name
}

but neither printed anything to the screen
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39663222
Pitfall! You do not have strict variable checking switched on (Set-StrictMode -Version latest), and so the typo you made was not obvious. Use $srv, not $svr!
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A procedure for exporting installed hotfix details of remote computers using powershell
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question