'How to get PropertyNames of Invoke-Sqlcmd command
Normally I can get the PropertyNames of a PSObject like this:
$xy.PSObject.Properties | Select-Object -Expand Name
However, I get a PSObject from a Invoke-Sqlcmd like this:
$GetQueryNormeinsatz = "SELECT ARTIKEL_NR as ArtNo, SMW1 as NameDE, SMN2 as BreiteVon FROM SOMEWHERE
$GetDataNormeinsatz = Invoke-Sqlcmd -ConnectionString $ConnectionLogik -Query $GetQueryNormeinsatz
Now when I do this:
$GetDataNormeinsatz.PSObject.Properties | select -expand Name
I would expect it to return
ArtNo
NameDE
BreiteVon
However, it returns this:
Count
Length
LongLength
Rank
SyncRoot
IsReadOnly
IsFixedSize
IsSynchronized
Why? And how can I get my desired ouput? Thx!
Solution 1:[1]
The property listing suggests that $GetDataNormeinsatz is an array, not a single custom object.
Taking advantage of member-access enumeration you could therefore simply write:
$GetDataNormeinsatz.Name
This will return the .Name property values of all the elements in your $GetDataNormeinsatz array.
If (some of) these elements do not have a .Name property, you'll get a $null value for each element that doesn't, by default; if Set-StrictMode -Version 2 or higher is in effect, a statement-terminating error occurs ("The property '...'' cannot be found on this object").
To inspect the types of your array elements, use the following:
$GetDataNormeinsatz | % GetType
To see the properties of these types too, use:
$GetDataNormeinsatz | Get-Member -Type Properties
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
