views:

241

answers:

4

Hi, I'm new to PowerShell and am trying to query against my SQL server. I get the idea of creating a new-psdrive and then navigating to databases etc and have a line of code as

$dbs = (get-childitem sqlserver:\sql\SERVER\INSTANCE\databases)

when I pipe the $dbs to a foreach, how would I get results of a collection of the database object? I am trying to read the extendedproperties of my test database.

This single query gives the results I want repeated for each database.

set-location DRIVENAME:\databases\beagle_test\extendedproperties get-childitem | select displayname, value

any help very much appreciated.

+1  A: 

I dont have SQL server handy to try this. Let me know the result

Set-Location DRIVENAME:\Databases

Get-ChildItem | % { Get-ChildItem $("$_.Name\extendedproperties") | Select DisplayName, Value }

ravikanth
Pretty close, this error for each database: Get-ChildItem : Length cannot be less than zero.Parameter name: lengthAt line:6 char:34+ Get-ChildItem | % { Get-ChildItem <<<< $("$_.Name\extendedproperties") | Select DisplayName, Value } + CategoryInfo : NotSpecified: (:) [Get-ChildItem], ArgumentOutOf RangeException + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.Pow erShell.Commands.GetChildItemCommand
Fatherjack
can you explain the | % please? My help file doesnt return any results when I search for it...
Fatherjack
Aah...so, the $_.Name isn't there. What do u see when you run Get-ChildItem at DRIVENAME:\Databases ? what is the name of the column that shows your database name?BTW, % is foreach-object. It is just a shorthand notation
ravikanth
and | is a pipeline...
ravikanth
yeah, got the | on its own, whats the % that follows it?Not all db's will have extended properties, I think thats where the problem is ...
Fatherjack
gci after sl gives every property for every database. gci | select name gives name of every database
Fatherjack
that explains then. So, we need to first find out if extendedproperties exist and then do something. Like I said % is a shorthand for ForEach-Object
ravikanth
sorry, just re-read your 1st comment and saw that at the end. Yes, we need to test the extendedproperties collection for each database. IsEmpty? IsNull? only have VB and SQL experience to draw on :-/
Fatherjack
Sorry..added another answer by mistake..check that
ravikanth
+1  A: 

Try this

Set-Location DRIVENAME:\Databases

Get-ChildItem | foreach-object { if (Test-Path $("$.Name\extendedproperties")) { Get-ChildItem $("$.Name\extendedproperties") | Select DisplayName, Value } }

The second line here is a single statement. What I am doing is to check if Extendedproperties exist and then get child item.

ravikanth
At line:6 char:47+ Get-ChildItem | foreach-object { if (Test-Path <<<< -path $("$.Name\extendedproperties\displayname")) { Get-ChildItem $("$.Name\extendedproperties") | Select DisplayName, Value } } + CategoryInfo : InvalidData: (SQLSERVER:\sql\...ies\displayname:SqlPath) [Test-Path], GenericProviderException + FullyQualifiedErrorId : KeysNotMatching,Microsoft.PowerShell.Commands.TestPathCommand
Fatherjack
tried adding "displayname" (see above) but original gives same error for each database
Fatherjack
I am lost too..I need to look at the properties at this point..Is it possible to paste the output of Get-ChildItem DRIVERNAME:\Databases?
ravikanth
is there a way to only get one set, rather than for each db?
Fatherjack
I am sorry I did not understand your last comment
ravikanth
gci output only fits in new answer !
Fatherjack
ExtendedProperties : {} seems to null. Do u have a DB for which this property shows something? I want to find out what type of data is this? Looks like a hash to me
ravikanth
yes there is database that I have added them specifically to test this
Fatherjack
A: 

How about just:

dir SQLSERVER:\SQL\Server\Instance\databases\*\extendedproperties\* | % {select $_.displayname, $_.value}
Rob Farley
Hi Rob. Not all db's have extendedproperties :: Get-ChildItem : Length cannot be less than zero.Parameter name: lengthAt line:12 char:4+ dir <<<< SQLSERVER:\SQL\Server\Instance\databases\*\extendedproperties\* | % {select $_.displayname, $_.value} + CategoryInfo : NotSpecified: (:) [Get-ChildItem], ArgumentOutOfRangeException + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.GetChildItemCommand
Fatherjack
A: 

How about:

dir sqlserver:\sql\ServerName\InstanceName\Databases\*\ExtendedProperties\* | 
  select @{Name="Database";Expression={$_.Parent.Name}}, Name, Value
Rob Farley
(I should mention... I tested this on a system on which some, but not all, databases had extended properties)
Rob Farley
This is crazy, I still get an error ::>> Get-ChildItem : Length cannot be less than zero.Parameter name: lengthAt line:5 char:4+ dir <<<< sqlserver:\sql\myservername\myinstancename\databases\*\ExtendedProperties\* | select @{Name="Database";Expression={$_.Parent.Name}}, Name, Value + CategoryInfo : NotSpecified: (:) [Get-ChildItem], ArgumentOutOfRangeException + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.GetChildItemCommand
Fatherjack
You've missed a slash. Databases\*\ExtendedProperties - with a slash after Databases, before the star.
Rob Farley
Oh... SO is removing it maybe. It dropped it from my comment too.
Rob Farley
not noticed that before. I had the slashes as per your answer (copy/paste). My error had same slashes but SO has dropped them as escape chars I think
Fatherjack