Powershell Select-Object 没有得到任何 属性

Powershell Select-Object not getting any property

我让这个脚本运行良好,直到最终用户想要 headers 不同的格式。这是使用 ActiveDirectory 模块从 AD 进行的简单转储,但现在当我尝试 运行 时出现错误。看起来我没有为 Select-Object 使用正确的格式?谢谢

Select : E键没有值。 在 G:\MarcG\Scripts\AD-Extract.ps1:19 char:17 + $用户 | Select@{N='Source KEY'; E=$_.SamAccountName}, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupportedException + FullyQualifiedErrorId : DictionaryKeyMissingValue,Microsoft.PowerShell.Commands.SelectObjectCommand

$users = (get-aduser -LDAPFilter "(&(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2))(&(objectCategory=person)(objectClass=user)(!objectClass=inetOrgPerson))(sAMAccountName=s0*)(!sAMAccountName=*-d)(!sAMAccountName=*-e)(!sAMAccountName=*-a)(!Name=Test*)(!Name=v-*)(!employeeID=[=12=]))” -Properties SamAccountName,givenname,sn,enabled,accountexpires,employeeID,employeeNumber,title,company,costcenterID,costcenter,uHALocationName,streetAddress,st,postalcode,mail,xorgid)

ForEach ($User in $Users) {

    If ($User.xorgid -ne $Null){

        $bytes = $User.xOrgID

        $newguid = new-object -TypeName System.Guid -ArgumentList (,$Bytes)
        $newguid.ToString()

        $User | Select @{N='Source KEY'; E=$_.SamAccountName},
                         @{N='First Name'; E=$_.givenname},
                         @{N='Last Name'; E=$_.sn},
                         @{N='Employee ID'; E=$_.employeeID},
                         @{N='Job Title'; E=$_.title},
                         company,
                         @{N='Cost Center Number'; E=$_.costcenterID},
                         @{N='Cost Center Name'; E=$_.costcenter},
                         @{N='Work Facility Location'; E=$_.uHALocationName},
                         @{N='Work Address 1'; E=$_.streetAddress},
                         @{N='State'; E=$_.st},
                         @{N='Work  Zip'; E=$_.postalcode},
                         @{N='Work Email'; E=$_.Mail},
                         @{N='xOrgID';E={($newguid.ToString()) }} | Export-csv -Path $Out_file -NoTypeInformation -Append


}
    Else {$User | Select @{N='Source KEY'; E=$_.SamAccountName},
                         @{N='First Name'; E=$_.givenname},
                         @{N='Last Name'; E=$_.sn},
                         @{N='Employee ID'; E=$_.employeeID},
                         @{N='Job Title'; E=$_.title},
                         company,
                         @{N='Cost Center Number'; E=$_.costcenterID},
                         @{N='Cost Center Name'; E=$_.costcenter},
                         @{N='Work Facility Location'; E=$_.uHALocationName},
                         @{N='Work Address 1'; E=$_.streetAddress},
                         @{N='State'; E=$_.st},
                         @{N='Work  Zip'; E=$_.postalcode},
                         @{N='Work Email'; E=$_.Mail} | 
                         Export-csv -Path $Out_file -NoTypeInformation -Append 

    }
    }

除非您引用现有的 属性 名称,否则计算出的 属性 的 Expression 值应该是 [ScriptBlock],而不是裸语句:

错误:

"abc" | Select-Object @{Name="Length";Expression=$_.Length}

正确:

"abc" | Select-Object @{Name="Length";Expression={$_.Length}}

注意 $_.Length

周围的 {}

你的 shorthand 键名是:

Select-Object @{N="Name";E={$_.Property}}