1

I know this question has already been asked but the answers don't quite fit the constrains i have So here we go again (sorry for that) :

I have this line in my PowerShell script :

$WITHOUTCLIENT = Invoke-Sqlcmd -h -1 -Database CM_00A -Query "Select Members.Name From CollectionMembers Members Join Collections Coll on Members.SiteID = Coll.SiteID Where  Coll.CollectionName = '_SCCM-Machine Sans Client'"

The problem being that although when i count the number of items in this collection, the result is accurate (i.e. $WITHOUTCLIENT.count ) but when i try to display any item in this collection, it keeps on displaying a bloody header (i.e. $WITHOUTCLIENT[0] )

Note that i DON'T want to use an external file to store the result and that i should use Invoke-Sqlcmd, not Sqlcmd

i'm begging for help Thank you

Zedude
  • 11
  • 1
  • 3
  • `$WITHOUTCLIENT` is a proper object not raw text output. When Powershell is displaying that data you see the property names to give the data context. What problem is this causing for you? – Matt Sep 24 '18 at 13:52
  • Just noticed that you only have the one property...`$WITHOUTCLIENT[0].Name`? You are dealing with an object array when I suppose you just wanted a string array. That should expand just the value from the object. – Matt Sep 24 '18 at 13:57

1 Answers1

1

If you execute query:

$WITHOUTCLIENT = Invoke-Sqlcmd -h -1 -Database CM_00A -Query "Select Members.Name From CollectionMembers Members Join Collections Coll on Members.SiteID = Coll.SiteID Where  Coll.CollectionName = '_SCCM-Machine Sans Client'"

You are receiving list of objects. Each object has one property, but it's still an object. To get 'flat' collection, use select with `-ExpandProperty' argument:

$WITHOUTCLIENT = $WITHOUTCLIENT | select -ExpandProperty Name
Paweł Dyl
  • 8,888
  • 1
  • 11
  • 27