LE LANGAGE POWERSHELL SISR4 : Scripting
Un fichier CSV se présente comme une liste d'éléments séparés par un caractère qui peut être un point virgule (;), une virgule (,) ou tout autre caractère. Le plus utilisé est le pointvirgule.
Chaque ligne contient donc un ensemble d'éléments qui ont la même signification. C'est souvent sur la première ligne du fichier (mais ce n'est pas toujours le cas) qu'on trouve la signification de chacun des éléments constituants une ligne.
Les éléments constituants chaque ligne sont souvent appelés des «champs» ou encore des «zones». Vous pouvez facilement visualiser un fichier.csv avec un utilitaire de type notepad ou notepad++.
Créer un fichier CSV en Powershell Export-Csv
Get-EventLog system -Newest 10 Index Time EntryType Source InstanceID Message ----- ---- --------- ------ ---------- ------- 3128 sept. 22 1... Information Service Control M... 1073748860 Le service Carte de performance WMI... 3127 sept. 22 1... Information Service Control M... 1073748860 Le service Programme d installation... 3126 sept. 22 1... Information Service Control M... 1073748864 Le type de démarrage du service Pro... 3125 sept. 22 1... Information Service Control M... 1073748864 Le type de démarrage du service Pro... 3124 sept. 22 1... Information Service Control M... 1073748860 Le service Fournisseur de cliché in...
Get-EventLog system -Newest 10 Select-Object TimeGenerated,EntryType,Source,InstanceID TimeGenerated EntryType Source InstanceId ------------- --------- ------ ---------- 22/09/2014 17:46:26 1073748860 Information Service Control Manager 22/09/2014 17:46:26 1073748860 Information Service Control Manager 22/09/2014 17:44:22 1073748860 Information Service Control Manager 22/09/2014 16:12:59 1073748860 Information Service Control Manager 22/09/2014 16:12:59 1073748864 Information Service Control Manager
Get-EventLog system -Newest 10 Select-Object TimeGenerated,EntryType,Source,InstanceID Export-Csv c:\essai.csv Get-Content c:\essai.csv #TYPE Selected.System.Diagnostics.EventLogEntry "TimeGenerated","EntryType","Source","InstanceId" "22/09/2014 17:46:26","Information","Service Control Manager","1073748860«
Le même avec un point virgule comme délimiteur Get-EventLog system -Newest 10 Select-Object TimeGenerated,EntryType,Source,InstanceID Export-Csv c:\essai.csv -Delimiter ";«Get-Content c:\essai.csv #TYPE Selected.System.Diagnostics.EventLogEntry "TimeGenerated";"EntryType";"Source";"InstanceId" "22/09/2014 17:52:26";"Information";"Service Control Manager";"1073748860«
Lire un fichier Csv en Powershell Import-Csv
Import-Csv c:\essai.csv -Delimiter ";" Format-Table TimeGenerated EntryType Source InstanceId ------------- --------- ------ ---------- 22/09/2014 17:52:26 Information Service Control Manager 1073748860 22/09/2014 17:46:26 Information Service Control Manager 1073748860 22/09/2014 17:46:26 Information Service Control Manager 1073748860 22/09/2014 17:44:22 Information Service Control Manager 1073748860 22/09/2014 16:12:59 Information Service Control Manager 1073748860 22/09/2014 16:12:59 Information Service Control Manager 1073748864 22/09/2014 16:12:59 Information Service Control Manager 1073748864
$log = Import-Csv c:\essai.csv -Delimiter ";" $log Get-Member Name MemberType Definition ---- ---------- ---------- Equals Method bool Equals(System.Object obj) GetHashCode Method int GetHashCode() GetType Method type GetType() ToString Method string ToString() EntryType NoteProperty System.String EntryType=Information InstanceId NoteProperty System.String InstanceId=1073748860 Source NoteProperty System.String Source=Service Control Manager TimeGenerated 17:52:26 NoteProperty System.String TimeGenerated=22/09/2014
$log[0] TimeGenerated EntryType Source InstanceId ------------- --------- ------ ---------- 22/09/2014 17:52:26 Information Service Control Manager 1073748860 $log[0].source Service Control Manager