是否可以创建一个自定义对象(PSObject)并在程序执行之前和之后定义其属性,我们不断向该对象添加值数组。
For e.g;
$c = @()
$c = New-Object PSObject
$c | Add-Member -type NoteProperty -name Name
$c | Add-Member -type NoteProperty -name Gender
$c | Add-Member -type NoteProperty -name Age
$c | Add-Member -type NoteProperty -name Name -value "John"
$c | Add-Member -type NoteProperty -name Gender -value "Male"
$c | Add-Member -type NoteProperty -name Age -value "30"
预先感谢您的任何线索或建议。
我不确定我是否遵循。您想要一个具有指定属性的对象数组吗?因为您的示例首先创建一个数组,然后将其覆盖为单个对象。所以你失去了你的阵列。
您可以使用创建对象new-object
并将属性值指定为哈希表-Property
范围。像这样:
$c = New-Object psobject -Property @{
Name = "John"
Gender = "Male"
Age = 30
}
要创建它们的数组,您可以使用:
$myarray = @()
$myarray += New-Object psobject -Property @{
Name = "John"
Gender = "Male"
Age = 30
}
如果您有多个测试需要一一运行,则可以在测试并创建“结果对象”的函数中运行测试,然后收集它:
$myresults = @()
function mytests($computer) {
#Test connection
$online = Test-Connection $computer
#Get buildnumber
$build = (Get-WmiObject win32_operatingsystem -ComputerName $computer).buildnumber
#other tests
#output results
New-Object psobject -Property @{
Online = $online
WinBuild = $build
}
}
$myresults += mytests -computer "mycomputername"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)