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

Mar*_*Gel 1 powershell active-directory export-to-csv

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

选择:E 键没有值。在 G:\MarcG\Scripts\AD-Extract.ps1:19 char:17 + $User | 选择@{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=\00))” -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 

    }
    }
Run Code Online (Sandbox Code Playgroud)

Mat*_*sen 6

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

错误的:

"abc" | Select-Object @{Name="Length";Expression=$_.Length}
Run Code Online (Sandbox Code Playgroud)

正确的:

"abc" | Select-Object @{Name="Length";Expression={$_.Length}}
Run Code Online (Sandbox Code Playgroud)

注意{}周围$_.Length

使用您的速记键名称,即:

Select-Object @{N="Name";E={$_.Property}}
Run Code Online (Sandbox Code Playgroud)