header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Appending Objects to single CSV
Last Post 23 Apr 2008 09:54 PM by smurawski. 4 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
SoCalDaveLUser is Offline
New Member
New Member
Posts:19
Avatar

--
23 Apr 2008 07:18 PM  

I know there has to be an easy answer to this, but I can 't seem to find it anywhere.

For simplicity... I have a basic script that's going to run against a list of servers and, for each server, gather some data.  I'm using a foreach loop to run the queries against each server and each server will be captured as an Object. 

What I'd like to do is export each object into a CSV file (ultimately I'd like to send the results to a SQL server, but I need to walk before I can run)...  What I'm finding is that each time I step through the script I get all the appropriate data for each server, but it OVERWRITES the CSV each time only displaying the last server info.

I don't want to write it to a different csv for each server (which I can do), just append the existing one so I have a full list of servernames and the appropriate fields?  Make sense?

 

$report = "C:\Logs\apps_status.csv"
$serverlist = "C:\temp\servers.txt"

function MakeObject {
 $svrObj = New-Object PSObject
 $svrObj | Add-Member NoteProperty "Server" "$servername"
 $svrObj | Add-Member NoteProperty "Status" "$app_status"
##more fields here..
 Write-Output $svrObj
 }

foreach ( $servername in $serverlist)
    {
    $app_status =   #get status of application
    #do some other stuff
    }

MakeObject | Export-csv -Path $report

 

What am I missing or doing wrong? 

PS. - I tried searching the forum to no avail, so if it has been answered already then a link to that post would suffice.

PSS - the only other option I see for export-csv is "NoClobber" which just prevents the overwriting of the file itself, not allowing it to append.

^^^^^^^^^^^^^^^^^^^^
"Sometimes I feel like I'm full of I.T."
Twitter = SoCalDaveL
SoCalDaveLUser is Offline
New Member
New Member
Posts:19
Avatar

--
23 Apr 2008 07:41 PM  

OR...

Would I be better suited replacing

foreach ( $servername in $serverlist)
    {
    $app_status =   #get status of application
    #do some other stuff
    }

MakeObject | Export-csv -Path $report

 

with

foreach ( $servername in $serverlist)
    {
    $app_status =   #get status of application
    #do some other stuff

    "$servername,$app_status" | out-file -filePath $report -Append
    }

The difference being using out-file to append instead os export-csv (which I thought would be a cleaner solution).

The latter appears to work, although I have to be sure that I have the headers setup ahead of time (which can be added to the script too)

^^^^^^^^^^^^^^^^^^^^
"Sometimes I feel like I'm full of I.T."
Twitter = SoCalDaveL
smurawskiUser is Offline
New Member
New Member
Posts:46

--
23 Apr 2008 07:45 PM  
Dave,

You could try this.. If you would like multiple runs of the same script to log to one CSV file, then use the -Append switch. Otherwise, you can change the report file for concurrent runs.


param(
    $report = "C:\Logs\apps_status.csv" ,
    $serverlist = "C:\temp\servers.txt",
    [switch]$append
)

function MakeObject {
 $svrObj = New-Object PSObject
 $svrObj | Add-Member NoteProperty "Server" "$servername"
 $svrObj | Add-Member NoteProperty "Status" "$app_status"
##more fields here..
 Write-Output $svrObj
 }

if ($append)
{
    $servers = Import-Csv $report
}
else 
{
    $servers = @()
}

foreach ( $servername in $serverlist)
    {
        $server = MakeObject
        $server.app_status =   #get status of application
        #do some other stuff
        $servers += $server
    }


$servers | Export-csv -Path $report


Good Luck!
Steven Murawski
Co-Host - Mind of Root (www.mindofroot.com)
Host - PowerShell Basics (powershell-basics.com)
SoCalDaveLUser is Offline
New Member
New Member
Posts:19
Avatar

--
23 Apr 2008 09:17 PM  

Interesting....  yet the $servername variable keeps coming up as "C:\temp\servers.txt"

^^^^^^^^^^^^^^^^^^^^
"Sometimes I feel like I'm full of I.T."
Twitter = SoCalDaveL
smurawskiUser is Offline
New Member
New Member
Posts:46

--
23 Apr 2008 09:54 PM  
Sorry Dave,

It should have been

foreach ( $servername in get-content $serverlist)
Steven Murawski
Co-Host - Mind of Root (www.mindofroot.com)
Host - PowerShell Basics (powershell-basics.com)
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer