Solved

How to retrieve all user databases using powershell

Posted on 2013-11-15
6
286 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A procedure for exporting installed hotfix details of remote computers using powershell
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

758 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

13 Experts available now in Live!

Get 1:1 Help Now