Solved

How to retrieve all user databases using powershell

Posted on 2013-11-15
6
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 70

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 70

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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.

752 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