invoke-sqlcmd output - store and call property values
-
Wednesday, May 09, 2012 6:07 PM
invoke-sqlcmd -query "sp_spaceused" -database master -serverinstance SQLDB\INST1
database_name : master
database_size : 1573.44 MB
unallocated space : 225.14 MB
RowError :
RowState : Detached
Table : {}
ItemArray : {master, 1573.44 MB, 225.14 MB}
HasErrors : FalseI saved the output of the above command to a variable, but $VAR.database_name does not work.
The only way I could get it to work was to use select for each property; for example:
$dbname = invoke-sqlcmd -query "sp_spaceused" -database master -serverinstance SQLDB\INST1 | select database_name
Since I want to get more than one property this is not efficient; is there a better way?
database_name NoteProperty System.String database_name=master
All Replies
-
Wednesday, May 09, 2012 6:48 PM
Do it like this:
$var | select -expand database_name $var | select -expand database_size # etc...
Grant Ward, a.k.a. Bigteddy
- Proposed As Answer by Yan Li_Microsoft Contingent Staff, Moderator Thursday, May 10, 2012 9:05 AM
-
Thursday, May 10, 2012 11:32 AM
Your suggestion gives me what I'm looking for, but an error is displayed on the console.
$TEST = invoke-sqlcmd -query "sp_spaceused" -database master -serverinstance SQLDB\INST
$DBNAME = $TEST | select -expand database_name
$DBSIZE = $TEST | select -expand database_size
$DBNAME
$DBSIZE--------------------------------------
Select-Object : Property "database_name" cannot be found.
At C:\users\anthonys\desktop\t5.ps1:3 char:25
+ $DBNAME = $TEST | select <<<< -expand database_name
+ CategoryInfo : InvalidArgument: (System.Data.DataRow:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommandSelect-Object : Property "database_size" cannot be found.
At C:\users\anthonys\desktop\t5.ps1:4 char:25
+ $DBSIZE = $TEST | select <<<< -expand database_size
+ CategoryInfo : InvalidArgument: (System.Data.DataRow:PSObject) [Select-Object], PSArgumentException
+ FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommandmaster
1573.44 MBHow can I clear the errors?
- Edited by wallst360 Thursday, May 10, 2012 12:00 PM
-
Thursday, May 10, 2012 11:55 AM
I have found the cause and solution to your problem. $TEST is actually an array, and to access it, you can use array notation:
$TEST = invoke-sqlcmd -query "sp_spaceused" -database master -serverinstance grant-pc $TEST[0].database_name $TEST[0].database_size
Grant Ward, a.k.a. Bigteddy
- Proposed As Answer by BigteddyMicrosoft Community Contributor Thursday, May 10, 2012 11:57 AM
- Marked As Answer by wallst360 Thursday, May 10, 2012 12:03 PM
-
Thursday, May 10, 2012 12:03 PMWorks great; thanks!
-
Thursday, May 10, 2012 12:12 PMNote that more data is stored in $TEST[1] too.
Grant Ward, a.k.a. Bigteddy
What's new in Powershell 3.0 (Technet Wiki)
- Edited by BigteddyMicrosoft Community Contributor Thursday, May 10, 2012 12:12 PM

