locked
Need Help With script, Cannot add Export-CSV Cmdlet. RRS feed

  • Question

  • Hi guys, I am trying to add Export-CSV cmdlet to this script

    But this not working,

    ### Get all AD Computers ###
    Get-ADComputer -Filter * | select name >> .\file.txt
    ### This command  clean white spaces / empty lines in the txt file ###
    (Get-Content .\file.txt) | Foreach {$_.TrimEnd()} | where {$_ -ne ""} | Set-Content file.txt    
    ### Get Time and date from computers in the txt file ###
    $servers = (Get-Content .\file.txt)
    ForEach ($server in $servers) {
        $time = ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem -computername $server).LocalDateTime)
        $server + '  ' + $time
    } | Eport-CSV -Path .\File.csv -NoType
     

    Zvi Alikyan



    • Edited by Tzvi Alikian Sunday, October 25, 2015 11:38 AM
    • Moved by Bill_Stewart Monday, November 23, 2015 10:03 PM Unanswerable drive-by question
    Sunday, October 25, 2015 11:36 AM

All replies

  • Hey Zvi,

    I just went through this same predicament last night!  Here's a very helpful post about the .CSV and powershell.

    http://community.spiceworks.com/scripts/show/3385-array-to-csv

    Let me see if I can put together (gotta go to work here in about 1/2 hour) a quick script that will do what you want.

    BRB

    Jeff


    Jeff Cummings - tech support and sharepoint project manager

    Sunday, October 25, 2015 1:25 PM
  • $servers = (Get-Content .\file.txt)
    
    $holdarr = @()
    $pNames=@("Server","Time")
    
    ForEach ($server in $servers) {
        $time = ([WMI]'').ConvertToDateTime((gwmi win32_operatingsystem -computername $server).LocalDateTime)
    # Don't need this anymore, but it will provide output during script execution...
        $server + '  ' + $time
        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name "Server" -value $server
        $obj | Add-Member -MemberType NoteProperty -Name "Time" -value $time
        $holdarr+=$obj
        $obj=$null
    }
    $holdarr | Export-CSV -Path .\File.csv -NoType
    

    # Thanks to this internet post, but I did modify the code to try out a jagged array for my own purposes....
    # http://sharepoint-community.net/profiles/blogs/powershell-from-an-array-to-comma-separated-file-csv-via-the

    Anyways, yeah, powershell and .csvs.  This is the actual link, but my code on spiceworks is a modification of his code on sharepoint-community.net.  And I created a modification that should work for you.....please let me know.

    Code on, my friend,

    Jeff


    Jeff Cummings - tech support and sharepoint project manager

    • Proposed as answer by OceanJeff40 Sunday, October 25, 2015 1:32 PM
    Sunday, October 25, 2015 1:32 PM
  • Too complicated:

    $props = @(
    	@{ N = 'Server'; E = { $_.__SERVER } },
    	@{ N = 'Time'; E = { $_.ConvertToDateTime($_.LocalDateTime) } }
    )
    Get-Content .\servers.txt |
    	ForEach{gwmi win32_operatingsystem -computername $_} |
    	select $props |
    	Export-CSV .\servertime.csv -NoType

    With PowerShell these things can be done very easily.

    This method can make all of you querying easier.


    \_(ツ)_/


    • Edited by jrv Sunday, October 25, 2015 2:19 PM
    Sunday, October 25, 2015 2:18 PM