0

If I have a custom PowerShell Object with int, boolean, and PSCredential, I need to be able to write those out to a location. The easiest solution is to do a foreach loop, but I need to treat PSCredential differently of course. How can I exclude PSCredential from the foreach when writing out all other values?

Custom object is initialized similar to:

$myObj = [PSCustomObject]@{
  Value1 = [string]
  Value2 = [int]
  Value3 = $null}

Further on in the processing of the script, Value3 is set to Get-Credential and the user is prompted for credentials.

3 Answers 3

2

Use select to restrict the properties appearing the output:

$myObj | select -Property * -ExcludeProperty Value3

Note that you must use -Property * here, otherwise -ExcludeProperty won't have any effect.

Sign up to request clarification or add additional context in comments.

Comments

0

For a location like a file try

"Value1 is {0}, Value2 is {1}" -f $myObj.Value1, $myObj.Value2 |out-file $someFileNameHere
Or did I misunderstand your question?

1 Comment

Yes, I could of course do that -- except when the object contains 20+ properties, that is convoluted. I have 5 PSCredentials and the remainder are other object types that I want to extract, without having to handle each property by hand.
0

Would something like this help?

$array = <array of objects>

$Props = $array[0].psobject.Properties |
Where { $_.TypeNameofValue -ne 'System.Management.Automation.PSCredential' } |
select -ExpandProperty Name

$array | select $Props

That should select out all the properties that aren't credentials

Comments

Your Answer

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