1

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!

SimonS
  • 1,891
  • 1
  • 29
  • 53
  • What do you get from just `$GetDataNormeinsatz`? – I.T Delinquent Dec 10 '19 at 14:39
  • Have you tried `$GetDataNormeinsatz | Get-member` – Sid Dec 10 '19 at 14:42
  • @I.TDelinquent I get the whole Object returned, with correct Propertynames and correct Values. Basically the SQL Values I'm after – SimonS Dec 10 '19 at 14:44
  • @Sid good call, `$GetDataNormeinsatz | gm | ? Membertype -eq Property | select -expand Name` returned my correct PropertyNames. Do you know why the other command I wrote in the question didn't work? – SimonS Dec 10 '19 at 14:46
  • I think you don't need the `.PSObject.Properties` part. What do you get with `$GetDataNormeinsatz | Select-Object -ExpandProperty Name` ? – I.T Delinquent Dec 10 '19 at 14:46
  • @I.TDelinquent a whole lot of errors :-). Because it can't find a Name Property – SimonS Dec 10 '19 at 14:47

1 Answers1

0

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
mklement0
  • 382,024
  • 64
  • 607
  • 775