• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

Append to a CSV file in powershell from a function

Function CSVFile($ProcessID,$Settings,$Identity,$CurrentSetting,$ChangeRequired,[Switch]$New){

	If($New -eq $True){
	Remove-Item ".\CasChkReport.csv" -Force -Confirm:$false -ErrorAction SilentlyContinue
	$CSV = New-Object System.Object 
	$CSV | Add-Member -type NoteProperty -Name ProcessID -Value $ProcessID
	$CSV | Add-Member -type NoteProperty -Name Settings -Value $Settings
	$CSV | Add-Member -type NoteProperty -Name Identity -Value $Identity
	$CSV | Add-Member -type NoteProperty -Name CurrentSetting -Value $CurrentSetting
	$CSV | Add-Member -type NoteProperty -Name ChangeRequired -Value $ChangeRequired
	$CSV | Export-Csv "CasChkReport.csv" -NoTypeInformation
	}

	Else{
	$CSV = New-Object System.Object 
	$Existing = Import-Csv .\CasChkReport.csv -header "ProcessID","Settings","Identity","CurrentSetting","ChangeRequired"
	#$newRow = New-Object PsObject -Property @{ProcessID=$ProcessID;Settings=$Settings;Identity=$Identity;CurrentSetting=$CurrentSetting;ChangeRequired=$ChangeRequired}
	#$CSV += $newRow
	#$CSV | Export-Csv "CasChkReport.csv" -NoTypeInformation
	
	$CSV | Add-Member -type NoteProperty -Name ProcessID -Value $ProcessID
	$CSV | Add-Member -type NoteProperty -Name Settings -Value $Settings
	$CSV | Add-Member -type NoteProperty -Name Identity -Value $Identity
	$CSV | Add-Member -type NoteProperty -Name CurrentSetting -Value $CurrentSetting
	$CSV | Add-Member -type NoteProperty -Name ChangeRequired -Value $ChangeRequired
	$CSV += $Existing
	$CSV | Export-Csv "CasChkReport.csv" -NoTypeInformation
	}
}


csvfile -processID "1" -Settings "1" -Identity "2" -CurrentSetting "1" -ChangeRequired "1" -new
csvfile -processid "2" -Settings "2" -Identity "1" -CurrentSetting "2" -ChangeRequired "2" 

Open in new window

How do I make this work. I have tried many things and I know I am close.
0
PeterSinger
Asked:
PeterSinger
1 Solution
 
slidingfoxCommented:
Function CSVFile($ProcessID,$Settings,$Identity,$CurrentSetting,$ChangeRequired,[Switch]$New){

     If($New -eq $True){

    	Remove-Item ".\CasChkReport.csv" -Force -Confirm:$false -ErrorAction SilentlyContinue
    	$CSV = New-Object System.Object 
    	$CSV | Add-Member -type NoteProperty -Name ProcessID -Value $ProcessID
    	$CSV | Add-Member -type NoteProperty -Name Settings -Value $Settings
    	$CSV | Add-Member -type NoteProperty -Name Identity -Value $Identity
    	$CSV | Add-Member -type NoteProperty -Name CurrentSetting -Value $CurrentSetting
    	$CSV | Add-Member -type NoteProperty -Name ChangeRequired -Value $ChangeRequired
    	$CSV | Export-Csv "CasChkReport.csv" -NoTypeInformation
	} else {

        Add-Content ".\CasChkReport.csv" "$ProcessID, $Settings, $Identity, $CurrentSetting, $ChangeRequired"

     }
}

csvfile -processID "1" -Settings "1" -Identity "2" -CurrentSetting "1" -ChangeRequired "1" -new
csvfile -processid "2" -Settings "2" -Identity "1" -CurrentSetting "2" -ChangeRequired "2" 

Open in new window

0
 
AkulshCommented:
In line 12, rather than exporting $CSV, first add all $CSV items to a new variable , say, $Table:
$Table += $CSV
Then export this $Table variable (outside ForEach loop, if you are going to use such a loop)
$Table| Export-Csv "CasChkReport.csv" -NoTypeInformation

In lines 26 and 27, your have variables in reverse order:
$Existing += $CSV
$Existing | Export-Csv "CasChkReport.csv" -NoTypeInformation


Permit me to add, your $CSV-named variable to add fields/arrays is rather confusing since exporting this variable would not generate a CSV file.  It would be better if you call it something like $array
0
 
PeterSingerAuthor Commented:
Thanks, thats what I am looking for.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now