Pouvons-nous convertir ci-dessous fichier txt au format CSV ajouté à la fin?Besoin de convertir le fichier txt en CSV (avec en-têtes) en utilisant PowerShell
fichier texte:
IP Address: 192.168.1.1 Hostname: 01-any1TEST Event Message: Ping Alert Status: Down at least 3 min Event Time: 17:25:14 Alert Type: :Windows 2012 Server ------------------------------------------------------------------------------------- IP Address: 192.168.1.2 Hostname: 02-any2TEST Event Message: Ping Alert Status: Down at least 4 min Event Time: 17:25:40 Alert Type: :Unix Server ------------------------------------------------------------------------------------- IP Address: 192.168.1.3 Hostname: 03-any3TEST Event Message: Ping Alert Status: Down at least 3 min Event Time: 17:26:21 Alert Type: :windows host -------------------------------------------------------------------------------------
sortie fichier CSV requis comme ci-dessous:
'IP Address','Hostname','Event Message','Alert Status','Event Time','Alert Type' '192.168.1.1','01-any1TEST','Ping','Down at least 3 min','17:26:21','Windows 2012 Server ' '192.168.1.2','02-any2TEST','Ping','Down at least 3 min','17:26:21','unix host ' '192.168.1.3','03-any3TEST','Ping','Down at least 3 min','17:26:21','windows host '
C'est possible, l'avez-vous essayé? – arco444
oui. $ input = Get-Content -path "chemin \ Bureau \ in.txt" | Select-String -List 'Type d'alerte', 'Adresse IP', 'Nom d'hôte', 'Message d'événement', 'Statut d'alerte', 'Heure d'événement' $ data = $ input [1 .. ($ input.Length - 1)] $ maxLength = 0 –
$ objects = ForEach ($ enregistrement dans $ data) { $ split = $ record -split "\ s {2,} | \ t +" If ($ split.Length -gt $ maxLength) { $ maxLength = $ split.Longueur } $ props = @ {} Pour ($ i = 0; $ i -lt $ split.Longueur; $ i ++) { $ props.Add ([Chaîne]) ($ i + 1), $ split [$ i]) } Nouveau-Objet -TypeName PSObject -Property $ props } $ headers = [Chaîne []] (0 .. $ longueur max) $ heade rs1 = [Chaîne []] ('MachineType', 'EventTime', 'HostName', 'IPAddress', 'AlertMessage', 'Gravité', 'AlertType', 'AlertStatus') $ objets | Sélectionner-Objet $ en-têtes | Export-Csv -NoTypeInformation -Path "path \ out.csv" –