我有一个使用自定义对象的脚本。我用这样的伪构造函数创建它们:
function New-TestResult
{
$trProps = @{
name = "";
repo = @{};
vcs = $Skipped;
clean = New-StageResult; # This is another pseudo-constructor
build = New-StageResult; # for another custom object.
test = New-StageResult; # - Micah
start = get-date;
finish = get-date;
}
$testResult = New-Object PSObject -Property $trProps
return $testResult
}
这些很有用,因为它们可以传递给类似的东西ConvertTo-Csv
or ConvertTo-Html
(不像哈希表,否则它可以实现我的目标)。它们的类型为PSCustomObject
对象。这段代码:
$tr = new-testresult
$tr.gettype()
返回这个:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSCustomObject System.Object
我可以更改Name
从那里返回的字段PSCustomObject
到别的东西?
稍后,当我整理测试结果时,我将传递给另一个函数,有时是单个结果,有时是一组结果。我需要能够根据我得到的内容做不同的事情。
任何帮助表示赞赏。
当然,创建 $testResult 后尝试一下:
$testResult.psobject.TypeNames.Insert(0, "MyType")
PowerShell 扩展类型系统的核心是 psobject 包装器(至少在 V1 和 V2 中)。该包装器允许您添加属性和方法、修改类型名称列表并获取底层 .NET 对象,例如:
C:\PS > $obj = new-object psobject
C:\PS > $obj.psobject
BaseObject :
Members : {string ToString(), bool Equals(System.Object obj), int GetHashCode(), type GetType()}
Properties : {}
Methods : {string ToString(), bool Equals(System.Object obj), int GetHashCode(), type GetType()}
ImmediateBaseObject :
TypeNames : {System.Management.Automation.PSCustomObject, System.Object}
或者根据提示尝试以下操作:
C:\PS> $d = [DateTime]::Now
C:\PS> $d.psobject
...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)