skip to Main Content

i imported from file IDs and base on this ID i am trying to get information from AD…and insert this information as columns in original file…this is the code:

$import = Import-Csv C:Tempcoputerstatus.csv
Foreach ($item in $import) {
$user = Get-ADUSer -Identity $item.ID
#$item = New-Object psobject
$item | Add-Member -MemberType NoteProperty -Name "office" -Value $user.office
$item | Add-Member -MemberType NoteProperty -Name "title" -Value $user.title
$item | Add-Member -MemberType NoteProperty -Name "displayname" -Value $user.name
} #foreach
$import | Export-Csv C:tempmy.csv

but it is no working very well..i success to insert the headers but the value is empty(,,,)

i would like to same help

thanks

3

Answers


  1. Get-ADUser and other active directory cmdlets operate a little funny. When you ask for an ADUser object Get-ADuser only returns a small subset of the properties available. Office and title are not available in this default set. To get what you want add the -Property parameter to Get-ADUser and ask for title and office in addition to the defaults it normally returns

    $user = Get-ADUSer -Identity $item.ID -Property Title, Office
    

    if you want to retrieve all possible properties use an asterisk

    $user = Get-ADUSer -Identity $item.ID -Property *
    
    Login or Signup to reply.
  2. It actually sounds like you may not be getting any user object back from Get-ADUser. Have a look at what is in the ID column in your csv files. Manually check what you are getting back from the import-csv command

    $import = Import-Csv C:Tempcoputerstatus.csv
    $import | gm
    

    Make sure that you have an ID property in that list. If so go ahead and check if your objects have ID values that you expect

    $import.ID
    

    If everything looks fine there try taking one of those IDs and manually running the Get-ADuser command replacing $item.ID with one of the actual IDs.

    Get-ADUSer -Identity TESTID -Property Title, Office
    

    Do you get anything back?

    Login or Signup to reply.
  3. Two ways…you can use splatting to dump all of the properties into the CSV for each like this…

    $import = Import-Csv C:Tempcoputerstatus.csv
    $myUsers = @()
    Foreach ($item in $import) {
        $user = Get-ADUSer -Identity $item.ID -Properties *
        [pscustomobject]$MyItem = @{
            "userProps" = $user
        }
        $myUsers += $MyItem.Values
    } #foreach
    $myUsers | Export-Csv 'C:tempmy.csv' -NoTypeInformation
    

    Or you can select specific properties that you care about, like this…

    $import = Import-Csv C:Tempcoputerstatus.csv
    $myUsers = @()
    Foreach ($item in $import) {
        $user = Get-ADUSer -Identity $item.ID -Properties *
        [pscustomobject]$MyItem = @{
            "DisplayName" = $user.Name
            "Office" = $user.office
            "Title" = $user.title
        }
        $myUsers += $MyItem.Values
    } #foreach
    $myUsers | Export-Csv 'C:tempmy.csv' -NoTypeInformation
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search