My issue is that I need to create CSV file which will print values in separate columns, next to each other. For now I have separate columns, but with empty cells.
For example: Column B has values starting from cell 1 until cell 8. Column C will start printing values starting from cell 9, but should start from cell 1. It contains values under values of column B, but I need them next to each other.
I have such code
$allsites = @(
'newone'
'new'
'one'
)
# Set the path to save the CSV file
$csvFilePath = "C:\PATH"
$parameters = @(
)
# Create an empty array to store the parameter values
# $parameters = @()
foreach ($site in $allsites) {
$xmlFilePath = "C:\PATH"
$xmlContent = [xml](Get-Content -Path $xmlFilePath)
$allparameters = @(
'parameter1'
'Parameter2'
'Parameter3'
'Parameter4'
)
foreach ($parameter in $allparameters) {
$parameterNode = $xmlContent.SelectSingleNode("//add[@key='$parameter']")
if ($parameterNode -eq $null) {
$parameterValue = "Parameter not found or commented out."
} else {
$parameterValue = $parameterNode.getAttribute("value")
}
Write-Host $parameterValue
$paramObj = [PSCustomObject]@{
A = ''
B = ''
C = ''
D = ''
}
if ($site -eq "newone") {
$paramObj.B = $parameterValue
}
elseif ($site -eq "new") {
$paramObj.C = $parameterValue
}
elseif ($site -eq "one") {
$paramObj.D = $parameterValue
}
$parameters += $paramObj
}
# Export the parameter values to the CSV file
if ($parameters.Count -gt 0) {
$parameters | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | Out-File -FilePath $csvFilePath -Encoding UTF8 -Append
# Clear the $parameters array for the next iteration
$parameters = @()
}
timeout /t 20
}
Write-Host "CSV file created successfully at $csvFilePath"
timeout /t 10
Here is a piece of output
"A","B","C","D"
"","JIRA","",""
"","false","",""
"","1200","",""
"","0","",""
"","true","",""
"","Parameter not found or commented out.","",""
"A","B","C","D"
"","","JIRA",""
"","","false",""
"","","1200",""
"","","0",""
"","","yesyoucan",""
"","","Parameter not found or commented out.",""
"A","B","C","D"
"","","","JIRA"
"","","","false"
"","","","1200"
"","","","0"
"","","","dzialaj"
"","","","Parameter not found or commented out."
What I need is:
print values in separate columns next to each other, without empty cells. Each column should start from cell 1.
Can you help? I can't find solution. I think it should be something with this line
Clear the $parameters array for the next iteration $parameters = @() It should contain only parameters for A, B, C without any empty values.
I tried add-member and tried to write some conditions to not add empty values without luck.