Quantcast
Channel: Windows PowerShell Forum
Viewing all articles
Browse latest Browse all 2562

Appending to pre-existing csv via Export-csv switch

$
0
0

How do you append to a CSV file which already exists without overwriting the initial data?  Essentially, I want to write PowerShell code to append data to an existing CSV file or a building a new CSV file without overwriting the existing data when an error is encountered.  The code below continues to overwrite itself.

$Credentials = Get-Credential DOMAIN\USER_ID
$Servers = Get-Content .\Servers.txt

foreach ($ServerOccurrence in $Servers)
{
   Try{
   Get-WmiObject -Class Win32_Service -computername $ServerOccurrence -Credential $Credentials -ErrorAction Stop|Where-object {$_.name -like "Sym*"}|select-object -Property __server, name, startmode, startname, state, pathname | Export-Csv DRFT11-28-2012-V.Csv -NotypeInformation
   }
   Catch{
      Write-Host "Couldn't contact server " $ServerOccurrence -foregroundcolor green
      Continue
   }
}

However, if I run GET-WMIOBject command by itself, it seems to works fine.  Let  me know your thoughts

Get-WmiObject -Class Win32_Service -computername (Get-Content .\Servers.txt) -Credential DOMAIN\USER_ID -ErrorAction Stop|Where-object {$_.name -like "Sym*"}|select-object -Property __server, name, startmode, startname, state, pathname | Export-Csv DRFT11-28-2012-V.Csv -NotypeInformation



Viewing all articles
Browse latest Browse all 2562

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>