本文介绍了如何从arraylist动态创建pscustomobject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章是对这个主题的补充

我想将 $outValue 转换为 pscustomobject 动态,因为 Theo 的回答(在上面的链接中)需要 pscustomobject,而我的 $outValue 是一个数组列表/选择对象...

基本上 pscustomobject 会保存这样的值,但这不是动态的,而是硬编码键/值.

$outValue = @([PSCustomObject]@{'服务器' = 'Server1.com''立方体' = '立方体1''连接详细信息' = '连接已更改!'})

我正在寻找这样的东西:

$outValue = $outValue |foreach-object () { @([PSCustomObject]@{$key = $value}})
解决方案

这将非常不稳定,但在我测试过的几台机器上都可以使用.

这应该会让您开始使用 PSCustomObject 成员创建动态"数组.

$array = @()Foreach($exportObject 中的 $Object ) {$i = -1$Goal = -($Object | gm | Where-Object {$_.MemberType -like "noteproperty"}).count$temp = 新对象 pscustomobject做 {$temp |Add-Member -MemberType NoteProperty -Name (($Object | gm)[$($i)].Name) -Value ($Object."$(($Object | gm)[$($i)].Name)")$i--}同时($i -ge $目标)$array += $temp}

这显然不是最佳做法,但根据我的经验,大多数事情都是快速而肮脏的.

然后在 Theo 提供的其他函数中使用 $arrayConvertTo-HTMLTable $array

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

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
    }
}
)
解决方案

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

这篇关于如何从arraylist动态创建pscustomobject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 03:47