我试图创建一个自定义对象,但收到错误。有人对如何处理这个问题有想法吗?
$tempObject = New-Object System.Object
Add-Member -inputobject $tempObject -type NoteProperty -name Name -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name account status -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name OU -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name Last Logon -value ""
Add Member:SecondValue参数对于"类型的成员不是必需的;NoteProperty";,并且不应该被指定。在添加的成员时不要指定SecondValue参数这种类型。
如果字符串中有空格,请将它们放在引号中(单引号或双引号):
$tempObject = New-Object System.Object
Add-Member -inputobject $tempObject -type NoteProperty -name Name -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name 'account status' -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name OU -value ""
Add-Member -inputobject $tempObject -type NoteProperty -name 'Last Logon' -value ""
更新
根据@wannabeprogrammer在下面的评论,实现上述目标的一种简单方法是:
$tempObject2 = [PSCustomObject]@{
Name = "" ;
'Account Status' = "" ;
OU = "" ;
'Last Logon' = ""
}
或者获得完全相同(即,使得两种方法中的compare-object
在正在创建的对象之间没有差异)。
$tempObject3 = New-Object -TypeName PSObject -Property @{
Name = "" ;
'Account Status' = "" ;
OU = "" ;
'Last Logon' = ""
}
看看我运行上面的片段是什么意思,然后是下面的:
compare-object $tempObject $tempObject2
compare-object $tempObject $tempObject3
compare-object $tempObject2 $tempObject3