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

2
  • $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? Commented Sep 24, 2018 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. Commented Sep 24, 2018 at 13:57

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.