1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
$file ='c:\temp\datas.txt'
@'
[10]
nom=mon_nom
prenom=mon_prenom
code=1111
adresse=mon_adresse
postal=12345
sexe=m
tel=1234567890
etage=1/2
dpt=bloc <30>
[12]
nom=mon_nom1
prenom=mon_prenom1
code=2222
adresse=mon_adresse1
postal=12345
sexe=f
tel=1234567891
etage=1/2
dpt=bloc <30>
[12]
nom=mon_nom1
prenom=mon_prenom1
code=2222
adresse=mon_adresse1
postal=12345
sexe=f
tel=1234567891
'@ > $File
Function Split-Bloc {
$NombreDeLigneParBloc=11
$NombreDePropriétés=10
$Elements=Get-Content $File -ReadCount $NombreDeLigneParBloc
#tableau de tableau
Foreach( $Lignes in $Elements)
{
#Transforme un bloc en un objet Powershell
$CurrentObject= New-Object PSObject
#Ligne d'un bloc
Foreach( $Ligne in $Lignes)
{
#Write-debug "Traite la ligne : $Ligne"
switch -regex ($Ligne)
{
#séparateur + valeur sur quatre chiffres + séparateur
'^\[(?<Number>\d{1,4})\]$' {
Write-debug ("`tAjoute la propriété {0}"-F $Matches.0)
$CurrentObject.psObject.Properties.Add(
(New-Object Management.Automation.PSNoteProperty('Number',$Matches.Number)))
Continue
}
#Nom + séparateur + valeur
'^(?<Name>.*?)=(?<Value>.*)$' {
Write-debug ("`tAjoute la propriété {0}"-F $Matches.0)
$CurrentObject.psObject.Properties.Add(
(New-Object Management.Automation.PSNoteProperty($Matches.Name, $Matches.Value)))
Continue
}
'' { Write-Debug "`tLigne vide";Continue }
default {Write-Error 'Cas inconnu : $Element' }
}
} #lignes
#Todo liste des noms de propriété, car ETS peut ajouter des membres
$nbProperties=0;$CurrentObject.psobject.Properties.GetEnumerator()|% {$nbProperties++}
Write-Debug "nbProperties=$nbProperties"
if ($nbProperties -ne $NombreDePropriétés)
{ Write-Error "Object incomplet : $CurrentObject" }
#Controles d'autres régles, possible...
Write-Output $CurrentObject
}#Elements
} #Split-Bloc
$Objets=Split-Bloc
$Objets.Count |
Partager