0

this post is supplementary to this thread here

Basically, I have 2 scripts

script1 has the following:

$exportObject = New-Object System.Collections.ArrayList
$exportObject | Select-Object

in script2, i am calling script1 to do something and piping the output to an -ov

& "script1.ps1" -ov $outputValue
$outputValue

this is what i get

op

i would like to covert $outValue to pscustomobject dynamically, because Theo's answer (in link above) requires pscustomobject, and my $outValue is an array list/select object...

basically the pscustomobject would hold values like this, but this is not dynamic and instead hardcoding the keys/values.

$outValue = @(
    [PSCustomObject]@{
        'Server' = 'Server1.com'
        'Cube' = 'Cube1'
        'Connection Details' = 'Connection changed!'
    }
)

i am looking for something like this:

$outValue = $outValue | foreach-object () { @(
    [PSCustomObject]@{
        $key = $value
    }
}
)
1
  • The answer from Theo is actually an array of pscustomobjects. I think I have a very, very stupid way of doing this but I will need to test first. Commented Jul 3, 2019 at 22:48

1 Answer 1

1

This will be highly volatile but is working on several machines I have tested.

This should get you going at creating "dynamic" Arrays with PSCustomObject members.

$array = @()
Foreach($Object in $exportObject ) {
    $i = -1
    $Goal = -($Object | gm | Where-Object {$_.MemberType -like "noteproperty"}).count

    $temp = New-Object pscustomobject
    Do {
        $temp | Add-Member -MemberType NoteProperty -Name (($Object | gm)[$($i)].Name) -Value ($Object."$(($Object | gm)[$($i)].Name)")
        $i--
    }While($i -ge $Goal)

    $array += $temp
}

This is obviously not the best practise but quick and dirty is how most things get done in my experience.

Then use $array in your other function provided by Theo, ConvertTo-HTMLTable $array

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

10 Comments

This is actually close to what I am looking for: ), but because there is -1, -2, -3 objects, it still is kinda hard coding, dont you think? It's great that no actual values are defined, but if its possible to condense it down for the pscustom object to automatically identify the number of objects/properties incoming fron $exportObject/$outputValue, and create objects as needed it would be perfect
Updated. I actually had a lot more fun making this one, I may even have use for it at some point.
Trying it right now :D, btw, offtopic, this is an array @(), but what is this called? @" "@?
hey Drew, i just tried this, and i tried outputting $array to test it, it is outputting the values, but the column/headers are missing (Server, Cube, Connection Details). is there a way to include those? because if i implement this ConvertTo-HTMLTable $array, wouldnt the colun names be missing?
i just applied this to Theo's function and it sent me the email with the nice table :D also, both column and data are there..though its weird how the column name wasnt printing on console
|

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.