Me again, I've just update the code with errors outfiles Still have two issues, the bad shape of my xml output (looks like crap) And also in my xml output if You execute the code you'll see that it generates files in the wrong way . Let me explain, if there is an error in the second line for example the xml will contain line 1 & 3, like he's skipping line 2 (which is normal because line 2 is in error) but not including line 4. At the end I want 3 elements per XML!!! and not 1 or 2 in case of error. If you do not understand just play the code.
@Theo : you were right about calling the counter variables in the for each loop
Here is the csv model (including errors for tests)
UCB63_DATENUM;U6618_FILENAME;UF6E8_CANAL;U65B8_IDRP
9/11/19 23:03;457E6659_ZN_LIQRLVPR_A_V_ML.pdf;ML;1
9/11/19 23:03;49453878_ZN_LIQRLVPR_A_V_ML.pdf;ML;2
9/11/19 23:03;497E585B_ZN_LIQRLVPR_A_V_CS.pdf;CS;3
2/12/20 22:12;58453B75_ZN_LIQRLVPR_A_V_ML.pdf;ML;4
7/8/19 22:27;457E6659_ZN_LIQRLVPR_A_V_ML.pdf;ML;5
;49453878_ZN_LIQRLVPR_A_V_ML.pdf;ML;6
9/11/19 23:03;497E585B_ZN_LIQRLVPR_A_V_CS.pdf;CS;7
2/12/20 22:12;58453B75_ZN_LIQRLVPR_A_V_ML.pdf;ML;8
2/12/20 22:12;58453B75_ZN_LIQRLVPR_A_V_ML.pdf;ML;
#vARIABLES EN DUR
$FREQUENCE_DECOMPTE = 'Nom="FREQUENCE_DECOMPTE" Valeur="MENS"'
$LIBELLE_ORGANISME = 'Nom="LIBELLE_ORGANISME" Valeur="HUMANIS CCN OG"'
$MONTANT_TOTAL = 'Nom="MONTANT_TOTAL" Valeur="0"'
$POLE = 'Nom="POLE" Valeur="1ADP"'
$CODE_ORGANISME = 'Nom="CODE_ORGANISME" Valeur="1ADP"'
# Paramètre nombre item par xml VALEUR A MODIFIER A 5000
$TailleLot = 3
# Nombre de process restants
$CountItems = 0
$countXml = 1
# Tests ligne OK/KO
$ligneOK = 0
$ligneKO = 0
$TotalLigne = 0
#Sortie ok/ko CSV
$FichierSortie = [pscustomobject]@{
"NumAssure" = $item.U65B8_IDRP
"Date" = $item.UCB63_DATENUM
"NomFichier" = $item.U6618_FILENAME
"Canal" = $item.UF6E8_CANAL
"Etat" = ""}
#Import du csv et création des différentes collections
$liste = Import-Csv -path 'c:\temp\testH.csv' -Delimiter ';'
[System.Collections.ArrayList] $DateErrors = @()
[System.Collections.ArrayList] $FileNameErrors = @()
[System.Collections.ArrayList] $CanalErrors = @()
[System.Collections.ArrayList] $NumAssureErrors = @()
#création fichier
$xmlFile = "C:\Temp\MIG_ERELEVE_MM_$(get-date -f dd-MM-yyyy)_$countXml.xml"
#En-tête
$output = @"
<?xml version="1.0" encoding="utf-8"?>
<Documents Origine="ERELEVE_HUM">
"@
foreach($item in $liste)
{
$FichierSortie = [pscustomobject]@{
"NumAssure" = $item.U65B8_IDRP
"Date" = $item.UCB63_DATENUM
"NomFichier" = $item.U6618_FILENAME
"Canal" = $item.UF6E8_CANAL
"Etat" = ""}
Write-Output "test for each"
#Initiation variables booléennes
$MyDateIsCorrect = $true
$MyFileNameIsCorrect = $true
$MyCanalIsCorrect = $true
$MyNumAssureIsCorrect = $true
$CountItems ++
$TotalLigne ++
#Transformations données
$date = $($item.UCB63_DATENUM -split " ")[0]
$renommage = [System.IO.Path]::GetFileNameWithoutExtension($item.U6618_FILENAME)
if([System.String]::IsNullOrEmpty($item.UCB63_DATENUM))
{
$MyDateIsCorrect = $false
$DateErrors.Add($item)
$ligneKO ++
$FichierSortie.Etat = "KO"
}
else {
if($date -notmatch "[0-9]{1,2}[\/][0-9]{1,2}[\/][0-9]{1,2}")
{
$MyDateIsCorrect = $false
$DateErrors.Add($item)
$ligneKO ++
$FichierSortie.Etat = "KO"
}
}
if([System.String]::IsNullOrEmpty($item.U6618_FILENAME))
{
$MyFileNameIsCorrect = $false
$FileNameErrors.Add($item)
$ligneKO ++
$FichierSortie.Etat = "KO"
}
else {
if($item.U6618_FILENAME -notmatch ".+[\.]pdf")
{
$MyFileNameIsCorrect = $false
$FileNameErrors.Add($item)
$ligneKO ++
$FichierSortie.Etat = "KO"
}
}
if([System.String]::IsNullOrEmpty($item.UF6E8_CANAL))
{
$MyCanalIsCorrect = $false
$CanalErrors.Add($item)
$ligneKO ++
$FichierSortie.Etat = "KO"
}
if([System.String]::IsNullOrEmpty($item.U65B8_IDRP))
{
$MyNumAssureIsCorrect = $false
$NumAssureErrors.Add($item)
$ligneKO ++
$FichierSortie.Etat = "KO"
}
#Génération output XML
if($MyDateIsCorrect -and $MyFileNameIsCorrect -and $MyCanalIsCorrect -and $MyNumAssureIsCorrect)
{
$ligneOK ++
$FichierSortie.Etat = "OK"
$output += @"
<Document>
<Index Nom="TITLE" Valeur="$renommage"/>
<Index Nom="NO_ASSURE" Valeur="$($item.U65B8_IDRP)"/>
<Index Nom="DEBUT_PERIODE" Valeur="$RecupDateFinTraitement"/>
<Index Nom="FIN_PERIODE" Valeur="$RecupDateFin30"/>
<Index $FREQUENCE_DECOMPTE/>
<Index $LIBELLE_ORGANISME/>
<Index $MONTANT_TOTAL/>
<Index Nom="DATE_GENERATION_DECOMPTE"$RecupDateFinTraitement/>
<Index $POLE/>
<Index $CODE_ORGANISME/>
<Index Nom="ALERTE_MAIL" Valeur="$fin"/>
<Fichier Nom="$($item.U6618_FILENAME)"/>
</Document>`r`n
"@
}
if ($CountItems -eq $TailleLot)
{
Write-Output "test premier if"
#Pied de page
$output += "`r`n </Documents>"
$output | Set-Content -Path $xmlFile -Encoding UTF8
$CountItems = 0
$countXml += 1
$xmlFile = "C:\Temp\MIG_ERELEVE_MH_$(get-date -f dd-MM-yyyy)_$countXml.xml"
#En-tête
$output =
@"
<?xml version="1.0" encoding="utf-8"?>
<Documents Origine="ERELEVE_HUM">
"@
}
$FichierSortie | export-csv -Path C:\Temp\SortieGlobaleOK_KO.csv -NoTypeInformation -Encoding UTF8 -Delimiter ';' -Append
}
$FichierSortieNbreLignes = [pscustomobject]@{
"LignesOK" = $ligneOK
"LignesKO" = $ligneKO
"TotalLignes" = $TotalLigne}
Write-Output "ligne OK $ligneOK"
Write-Output "ligne en erreur $ligneKO"
Write-Output "Total ligne $TotalLigne"
#Fichiers de sortie d'erreur et de comptage
$FichierSortieNbreLignes | Export-Csv -NoTypeInformation -Path c:\temp\ComptageLigneOK_KO.csv -Delimiter ';' -Encoding UTF8
$DateErrors.ToArray() | Export-Csv -Path c:\temp\DateErrors.csv -NoTypeInformation
$FileNameErrors.ToArray() | Export-Csv -Path c:\temp\FileNameErrors.csv -NoTypeInformation
$CanalErrors.ToArray() | Export-Csv -Path c:\temp\CanalErrors.csv -NoTypeInformation
$NumAssureErrors.ToArray() | Export-Csv -Path c:\temp\NumAssureErrors.csv -NoTypeInformation