MarcGel
MarcGel

Reputation: 301

Powershell Select-Object not getting any property

I had this script working fine until the end user wanted the headers formatted differently. This is a simple dump from AD using the ActiveDirectory module, but now when I try to run this I get errors. Looks like I'm not using correct format for Select-Object? Thx

Select : The E key has no value. At G:\MarcG\Scripts\AD-Extract.ps1:19 char:17 + $User | 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=\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 

    }
    }

Upvotes: 1

Views: 4991

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

Unless you're referencing an existing property name, the Expression value of a calculated property is supposed to be a [ScriptBlock], not a naked statement:

Wrong:

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

Correct:

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

Notice the {} around $_.Length

With your shorthand key names that is then:

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

Upvotes: 6

Related Questions