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.