Solved

How to retrieve all user databases using powershell

Posted on 2013-11-15
6
311 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

617 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