常规讨论 Export results to CSV

  • 23. dubna 2012 17:51
     
     
    can anyone help me export results to CSV
    $searchRoot = 'cd.ds.bsu.edu/Academic Affairs/AG/Computers' 
    $age = 120 # in days
     
    $accounts = Get-QADComputer -IncludedProperties LastLogonTimeStamp -SearchRoot $searchRoot | Sort-Object -Property LastLogonTimeStamp -Descending
    $today = Get-Date
     
    foreach( $account in $accounts )
    {
        if( $account.LastLogonTimeStamp -eq $null )
        {
            Write-Host $account.Name "last logon: Never"
        }
        else
        {
            $res = $today - $account.LastLogonTimeStamp
            $days = $res.Days
     
            if( $days -gt $age )
            {
                Write-Host $account.Name "last logon:" $days "days ago"
            }
        }
    }

    bradley Wyatt

Všechny reakce

  • 23. dubna 2012 17:59
     
     
    Unless you pressed a wrong button, you are messing this forum around.  You have already asked this question.  Why are you posting it again?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)


  • 23. dubna 2012 18:01
     
     

    The last question I had posted resulted it an entirely different script tat would not export. I was seeing if anyone else had any ideas on how to do it. I wanted it to be something like the following:
    | Select-Object Name, SAMAccountName, LastLogon|Export-CSV C:\csv\file.csv

    Just a csv with what it throws me on PowerShell


    bradley Wyatt

    I took your new advice on the previous post and it compiled a blank csv as well. Just looking for other input as well
  • 23. dubna 2012 18:19
     
      Obsahuje kód

    This works.  If you wrote that script, you will know how to fit the code into this structure:

    $numbers = 0..9
    $results = @()
    foreach ($number in $numbers) {
        $tobj = New-Object psobject
        $tobj | Add-Member -MemberType noteproperty -Name 'Number' -Value $number
        if ($number % 2 -eq 0) {$tobj | Add-Member -MemberType noteproperty -Name 'Type' -Value 'Even'}
        else {$tobj | Add-Member -MemberType noteproperty -Name 'Type' -Value 'Odd' }
        $results += $tobj
        }
    $results | Export-Csv me.csv -Force
    Import-Csv me.csv | Format-Table -AutoSize


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

  • 23. dubna 2012 18:49
     
     
    I wrote the script but never implemented $results, $tobj within the script. That is why I am having troubles. 

    bradley Wyatt

  • 23. dubna 2012 19:28
     
     
    I found out how to do it the right way. Thanks for the help anyways

    bradley Wyatt

  • 24. dubna 2012 2:39
    Moderátor
     
     

    Hi,

    Glad to hear that, why not share out the method you found? So that others would be helped here.

    Best Regards,

    Yan Li


    Yan Li

    TechNet Community Support

  • 3. května 2012 21:30
     
     
    $results | Export-Csv me.csv -Force

    bradley Wyatt