添加成员的奇怪行为。 Get-Member不返回任何值

时间:2014-04-14 10:54:45

标签: powershell

如果我编写以下代码,正如您所见,我使用Get-Member我得到一个null:

$UserToAdd = New-Object psobject
$UserToAdd | Add-Member -membertype NoteProperty -Name "Name" -Value ([adsi]$_.path).name[0]
$UserToAdd | Add-Member -membertype NoteProperty -Name "Email" -Value ([adsi]$_.path).mail[0]
$UserToAdd | Add-Member -membertype NoteProperty -Name "DaysLeft" -Value $DaysTillExpiring
$UserToAdd | Add-Member -membertype NoteProperty -Name "ExpirationTimestamp" -Value $ExpirationTimestamp.ToString("dd/MM/yyyy alle ore HH:mm:ss ")
$UserToAdd | Add-Member -membertype NoteProperty -Name "SamAccountName" -Value ([adsi]$_.path).SamAccountName[0]
$UserToAdd | get-member -Verbose
get-content $UserToAdd

但变量确实有一个值,因为当我使用像get-content这样的东西时,我得到以下错误,包含正确的值:

  

get-content:找不到驱动器。一个名为'@ {Name = XXXXXXXXX;电子邮件= XXXXXXXXXX; DaysLeft = 13; ExpirationTimestamp = 27/04 / 2014 alle ore 10'不存在。

1 个答案:

答案 0 :(得分:1)

我无法在PS 4.0上重现您的问题。删除Get-Content部分之后,一切都运行正常。

$UserToAdd = New-Object psobject
$UserToAdd | Add-Member -membertype NoteProperty -Name "Name" -Value "hei"
$UserToAdd | Add-Member -membertype NoteProperty -Name "Email" -Value "epost"
$UserToAdd | Add-Member -membertype NoteProperty -Name "DaysLeft" -Value 3
$UserToAdd | Add-Member -membertype NoteProperty -Name "ExpirationTimestamp" -Value ((date).ToString("dd/MM/yyyy alle ore HH:mm:ss "))
$UserToAdd | Add-Member -membertype NoteProperty -Name "SamAccountName" -Value "ops"
$UserToAdd | get-member -Verbose


   TypeName: System.Management.Automation.PSCustomObject

Name                MemberType   Definition                                                     
----                ----------   ----------                                                     
Equals              Method       bool Equals(System.Object obj)                                 
GetHashCode         Method       int GetHashCode()                                              
GetType             Method       type GetType()                                                 
ToString            Method       string ToString()                                              
DaysLeft            NoteProperty System.Int32 DaysLeft=3                                        
Email               NoteProperty System.String Email=epost                                      
ExpirationTimestamp NoteProperty System.String ExpirationTimestamp=14.04.2014 alle ore 16:40:48 
Name                NoteProperty System.String Name=hei                                         
SamAccountName      NoteProperty System.String SamAccountName=ops  

如果Get-Member返回null,那么您从未在第一时间创建该对象,因为即使没有Add-Member行,Get-Member仍会显示:

$UserToAdd = New-Object psobject
$UserToAdd | get-member -Verbose


   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType Definition                    
----        ---------- ----------                    
Equals      Method     bool Equals(System.Object obj)
GetHashCode Method     int GetHashCode()             
GetType     Method     type GetType()                
ToString    Method     string ToString()