locked
CSV output issues RRS feed

  • Question

  • Hello everyone, I'm a PowerShell newbie. I'm trying to query remote a computer's  Users folder, sort it by the most recent logins, return the last 3 usernames in one column and their last login times in a second column in a csv.  When I run the following code, I'm getting the headers for each column, but just says "System.Object[]" after. I'm sure I'm inputting the values wrong into the $obj_list variable, but even when I make it just one value, it returns empty.  Here is what I have:

    function last3  
       {
        [CmdletBinding()]
          param(
          [Parameter( Position=0,Mandatory=$true)]
          [string]$ComputerName
          )
        $last3name1 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select Name | Select-Object -First 1 | Format-Table | Out-String
        $last3name2 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select Name | Select-Object -skip 1 | Select-Object -First 1 | Format-Table | Out-String
        $last3name3 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select Name | Select-Object -skip 2 | Select-Object -First 1 | Format-Table | Out-String
        $last3write = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select LastWriteTime | Select -First 1 | Format-Table | Out-String
        $last3write = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select LastWriteTime | Select-Object -skip 1 | Select -First 1 | Format-Table | Out-String
        $last3write = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select LastWriteTime | Select-Object -skip 2 | Select -First 1 | Format-Table | Out-String
    
        $obj_list = New-Object PSObject
        $obj_list | Add-Member -Type NoteProperty -Name "Name" -Value $last3name1, $last3name2, $last3name3
        $obj_list | Add-Member -Type NoteProperty -Name "Write Time" -Value $last3write1, $last3write2, $last3write3
        write-host $obj_list
        return $obj_list
        }
    
    $list = Get-Content "*filepath*"
    
    $data = @()
    foreach ($item in $list) {
        $data += last3 -ComputerName $item
        }
    
    $data | Export-Csv .\Results.csv -NoTypeInformation
    .\Results.csv
    
    
    Thank you for any insight!


    • Edited by Codyyoung37 Friday, January 5, 2018 10:03 PM
    Friday, January 5, 2018 10:03 PM

Answers

  • You probably want to do select-expand and not use format-table and out-string at all.  Then output 3 pscustomobject's, one for each.

    • Marked as answer by Codyyoung37 Friday, January 5, 2018 11:11 PM
    Friday, January 5, 2018 11:00 PM

All replies

  • Here is how to create an object and set its values.

    [pscustomobject]@{
    ComputerName = $ComputerName Name = $last3name1, $last3name2, $last3name3 WriteTime = $last3write1, $last3write2, $last3write3 }

    Arrays and collections embedded in an object cannot be exported to a CSV without first being converted.


    \_(ツ)_/


    • Edited by jrv Friday, January 5, 2018 10:15 PM
    Friday, January 5, 2018 10:14 PM
  • Thanks for the response!

    That's getting me closer, but I'm still getting the following output:

    FYI, this is how I have the code set up now:

    function last3 
        {
        [CmdletBinding()]
          param(
          [Parameter( Position=0,Mandatory=$true)]
          [string]$ComputerName
          )
        $last3name1 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select Name | Select-Object -First 1 | Format-Table | Out-String
        $last3name2 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select Name | Select-Object -skip 1 | Select-Object -First 1 | Format-Table | Out-String
        $last3name3 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select Name | Select-Object -skip 2 | Select-Object -First 1 | Format-Table | Out-String
        $last3write1 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select LastWriteTime | Select -First 1 | Format-Table | Out-String
        $last3write2 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select LastWriteTime | Select-Object -skip 1 | Select -First 1 | Format-Table | Out-String
        $last3write3 = Get-Childitem "\\$ComputerName\c$\Users" | sort-object -Descending LastWriteTime | Select LastWriteTime | Select-Object -skip 2 | Select -First 1 | Format-Table | Out-String
    
        $obj_list = [pscustomobject]@{
        ComputerName = $ComputerName
        Name = $last3name1, $last3name2, $last3name3
        WriteTime = $last3write1, $last3write2, $last3write3
        }
        return $obj_list
        }
    
    $list = Get-Content "*filepath*"
    
    $data = @()
    foreach ($item in $list) {
        $data += last3 -ComputerName $item
        }
    
    $data | Export-Csv .\Results.csv -NoTypeInformation
    .\Results.csv
    
    

    The only thing I can think of is I don't have the right object type that I'm trying to export into the file, but I assume the out-string on each line should fix it. Obviously, its not. Any ideas on that?


    • Edited by Codyyoung37 Friday, January 5, 2018 10:38 PM
    Friday, January 5, 2018 10:23 PM
  • Yes.  You have to convert those to strings.  Objects will not convert.


    \_(ツ)_/

    Friday, January 5, 2018 10:26 PM
  • Is piping the variable into Out-String enough then? That's what is currently in my code as the final thing to be piped into for each variable, and it still isn't returning correctly. It seems to me like I must be retrieving the information incorrectly somehow and it isn't going into the custom object properly.

    The other odd thing is that I experimented a little and when I just put one variable into a column, it returns empty. Multiple variables returns System.Object[]. I'm not sure what it means exactly, maybe nothing.

    If I do a write-host on the individual variables they return with the correct values. It's when they go into the custom object that things won't work.

    Thanks again for your help!



    • Edited by Codyyoung37 Friday, January 5, 2018 10:40 PM
    Friday, January 5, 2018 10:35 PM
  • You probably want to do select-expand and not use format-table and out-string at all.  Then output 3 pscustomobject's, one for each.

    • Marked as answer by Codyyoung37 Friday, January 5, 2018 11:11 PM
    Friday, January 5, 2018 11:00 PM
  • That did it, thanks a ton!
    • Marked as answer by Codyyoung37 Friday, January 5, 2018 11:11 PM
    • Unmarked as answer by Codyyoung37 Friday, January 5, 2018 11:11 PM
    Friday, January 5, 2018 11:11 PM