Solved

How to retrieve all user databases using powershell

Posted on 2013-11-15
6
293 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 68

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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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 68

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

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 brief introduction to what I consider to be the best editor for PowerShell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

861 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now