none
Format-Table not displaying a columns data

    Question

  • I am using the powershell command below to read a csv file and match the computer name to the employee name where the last login date is 181 days old. For some reason the Employee_Name column in the output is only displaying {} on each row. Any idea why its not returning the employee name?

        Import-Module ActiveDirectory
        $Days = (Get-Date).AddDays(-181) 
    
    
        $Computers = @{}
        Import-CSV -Path c:\PS\ComputerNames.CSV | % { $Computers[$_.Computer_Name] = $_.Employee_Name }
        Get-ADComputer -Property Name,lastLogonDate -Filter  {lastLogonDate -lt $Days} -Server servername -Searchbase "OU=US,DC=Domain,DC=net" | ? { $Computers.Keys -contains $_.Computer_Name } | select Name,lastLogonDate,@{n='Employee_Name';e={$Computers[$_.Computer_Name]}} | ft

    Friday, August 23, 2013 2:02 PM

Answers

  • mkozz,

    Here's one way it can be done in 2.0.  We put the AD results in an array, adding a blank property for the employee name, because it's a convenient time to do so.  We then use this syntax for ForEach so that we have a solid variable name we can use for referencing object from the outer pipeline (the ForEach statement) from within the nested pipeline (the Where clause).

    Import-Module ActiveDirectory
    $Days = (Get-Date).AddDays(-181) 
    
    $ComputerUsers = Import-CSV -Path c:\PS\ComputerNames.CSV
    $UsedComputers = $ComputerUsers | % { $_.Computer_Name }
    $Computers = Get-ADComputer -Property Name, lastLogonDate -Filter { lastLogonDate -lt $Days } -Server servername -Searchbase "OU=US,DC=Domain,DC=net" `
      | Where { $UsedComputers -Contains $_.Name } `
      | Select Name,  lastLogonDate, @{ Name = "Employee"; Expression = { "" } }
    
    ForEach ( $Computer in $Computers )
      {
      $Computer.Employee = ( $ComputerUsers | Where { $_.Computer_Name -eq $Computer.Name } ).Employee_Name
      }
    
    $Computers | Format-Table

    Friday, August 23, 2013 10:28 PM

All replies

  • Can you paste the content of your csv

    Thanks Azam When you see answers please Mark as Answer if Helpful..vote as helpful.

    Friday, August 23, 2013 2:19 PM
  • Here are a few rows with the header on the first row

    Computer_Name Employee_Name
    US0001W John Doe
    US0002W Jane Doe
    US0003W Bill Smith
    US0004W John Smith
    Friday, August 23, 2013 2:23 PM
  • mkozz,

    I would leave the imported data as an array.

    Import-Module ActiveDirectory
    $Days = (Get-Date).AddDays(-181) 
    
    $Computers = Import-CSV -Path c:\PS\ComputerNames.CSV
    Get-ADComputer -Property Name, lastLogonDate -Filter  { lastLogonDate -lt $Days } -Server servername -Searchbase "OU=US,DC=Domain,DC=net" `
      | ? { $Computers.Computer_Name -contains $_.Name } `
      | select Name, `
               lastLogonDate, `
               @{ n = 'Employee_Name'; e = { ( $Computers | ? Computer_Name -eq $_.Name ).Employee_Name } } `
      | ft

    • Proposed as answer by Mike Laughlin Saturday, August 24, 2013 3:20 AM
    Friday, August 23, 2013 2:55 PM
  • When I run this it is not returning anything?
    Friday, August 23, 2013 3:06 PM
  • mkozz,

    It works for me.

    Did you change the 'servername' and 'DC=Domain' back to whatever you need them to be?

    I do rely on PowerShell 3.0 syntax in the where clause in the expression defining Employee_Name.  If you are using PowerShell 2.0, we can't just convert the Where syntax, because the $_ would become ambiguous, and we'd have to find a better way.

    Friday, August 23, 2013 5:50 PM
  • Thanks, I am using powershell built into windows 7
    Friday, August 23, 2013 7:26 PM
  • mkozz,

    Here's one way it can be done in 2.0.  We put the AD results in an array, adding a blank property for the employee name, because it's a convenient time to do so.  We then use this syntax for ForEach so that we have a solid variable name we can use for referencing object from the outer pipeline (the ForEach statement) from within the nested pipeline (the Where clause).

    Import-Module ActiveDirectory
    $Days = (Get-Date).AddDays(-181) 
    
    $ComputerUsers = Import-CSV -Path c:\PS\ComputerNames.CSV
    $UsedComputers = $ComputerUsers | % { $_.Computer_Name }
    $Computers = Get-ADComputer -Property Name, lastLogonDate -Filter { lastLogonDate -lt $Days } -Server servername -Searchbase "OU=US,DC=Domain,DC=net" `
      | Where { $UsedComputers -Contains $_.Name } `
      | Select Name,  lastLogonDate, @{ Name = "Employee"; Expression = { "" } }
    
    ForEach ( $Computer in $Computers )
      {
      $Computer.Employee = ( $ComputerUsers | Where { $_.Computer_Name -eq $Computer.Name } ).Employee_Name
      }
    
    $Computers | Format-Table

    Friday, August 23, 2013 10:28 PM
  • Thank you so much! works
    Monday, August 26, 2013 12:59 PM