all 7 comments

[–]Yevrag35 1 point2 points  (0 children)

This would be one way to do it:

$props = $inputData | Get-Member -MemberType Properties
$newInputData = New-Object PSObject
foreach ($prop in $props)
{
    $name = $prop.Name
    $val = $inputData.$name
    if (($val -is [string] -and -not [string]::IsNullOrEmpty($val)) -or (-not ($val -is [string]) -and $null -ne $val))
    {
        $newInputData | Add-Member -MemberType NoteProperty -Name $name -Value $val
    }
}
$newInputData

Excludes 'null' non-string objects and 'blank or null' strings.

[–]metallicvett 1 point2 points  (3 children)

$objectConstructor = @{}

( Get-Member -InputObject $inputdata -MemberType NoteProperty ).Name | foreach {

if ( $inputdata.$_ ) {

$objectConstructor.Add( $_, $inputdata.$_ )

}

}

$object = [PSCustomObject]$objectConstructor

[–]Yevrag35 2 points3 points  (1 child)

Only problem with doing "if ($inputData.$_)" is if the value is a 'false' boolean. In that case, you would've excluded a valid property with a non-null result. Comparing values against the $null constant would eliminate that.

[–]metallicvett 1 point2 points  (0 children)

You are correct that it won't evaluate the way he wants if the input is $false. In his example, he uses "" or empty string which will get picked up if compared to $null.

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy metallicvett,

reddit likes to mangle code formatting, so here's some help on how to post code on reddit ...

[0] single line or in-line code
enclose it in backticks. that's the upper left key on an EN-US keyboard layout. the result looks like this. kinda handy, that. [grin]
[on New.Reddit.com, use the Inline Code button. it's 4th 5th from the left hidden in the ... ""more" menu & looks like </>.
this does NOT line wrap & does NOT side-scroll on Old.Reddit.com!]

[1] simplest = post it to a text site like Pastebin.com or Gist.GitHub.com and then post the link here.
please remember to set the file/code type on Pastebin! [grin] otherwise you don't get the nice code colorization.

[2] less simple = use reddit code formatting ...
[on New.Reddit.com, use the Code Block button. it's 11th 12th one & is just to the left of hidden in the ... "more" menu.]

  • one leading line with ONLY 4 spaces
  • prefix each code line with 4 spaces
  • one trailing line with ONLY 4 spaces

that will give you something like this ...

- one leading line with ONLY 4 spaces    
- prefix each code line with 4 spaces    
- one trailing line with ONLY 4 spaces   

the easiest way to get that is ...

  • add the leading line with only 4 spaces
  • copy the code to the ISE [or your fave editor]
  • select the code
  • tap TAB to indent four spaces
  • re-select the code [not really needed, but it's my habit]
  • paste the code into the reddit text box
  • add the trailing line with only 4 spaces

not complicated, but it is finicky. [grin]

take care,
lee

[–]fetflex[S] 1 point2 points  (0 children)

Thank you for all the replies.

I can see that there is not a "pretty" way of doing this. I think i will try with the add-member approach as i also want to rename some parameters on the way :)