Je ne vais pas aborder le débat du choix entre CSV, JSON et XML, ce dernier étant déjà largement présent sur le net.

Le seul truc à retenir c’est que le JSON et le XML permettent des résultats sur plusieurs là le CSV ne peut pas.

Il faut savoir que Powershell gère nativement le JSON.

Par exemple, avec un Get-ChildItem:

Get-ChildItem  C:\Temp\Test\  | ConvertTo-Json

On obtient:

[
    {
        "Name":  "Sub1",
        "FullName":  "C:\\Temp\\Test\\Sub1",
        "Parent":  {
                       "Name":  "Test",
                       "FullName":  "C:\\Temp\\Test",
                       "Parent":  "Temp",
                       "Exists":  true,
                       "Root":  "C:\\",
                       "Extension":  "",
                       "CreationTime":  "\/Date(1523650597878)\/",
                       "CreationTimeUtc":  "\/Date(1523650597878)\/",
                       "LastAccessTime":  "\/Date(1523650688390)\/",
                       "LastAccessTimeUtc":  "\/Date(1523650688390)\/",
                       "LastWriteTime":  "\/Date(1523650688390)\/",
                       "LastWriteTimeUtc":  "\/Date(1523650688390)\/",
                       "Attributes":  16
                   },
        "Exists":  true,
        "Root":  {
                     "Name":  "C:\\",
                     "FullName":  "C:\\",
                     "Parent":  null,
                     "Exists":  true,
                     "Root":  "C:\\",
                     "Extension":  "",
                     "CreationTime":  "\/Date(1446186510642)\/",
                     "CreationTimeUtc":  "\/Date(1446186510642)\/",
                     "LastAccessTime":  "\/Date(1523889482812)\/",
                     "LastAccessTimeUtc":  "\/Date(1523889482812)\/",
                     "LastWriteTime":  "\/Date(1523889482812)\/",
                     "LastWriteTimeUtc":  "\/Date(1523889482812)\/",
                     "Attributes":  22
                 },
        "Extension":  "",
        "CreationTime":  "\/Date(1523650667572)\/",
        "CreationTimeUtc":  "\/Date(1523650667572)\/",
        "LastAccessTime":  "\/Date(1523650717234)\/",
        "LastAccessTimeUtc":  "\/Date(1523650717234)\/",
        "LastWriteTime":  "\/Date(1523650717234)\/",
        "LastWriteTimeUtc":  "\/Date(1523650717234)\/",
        "Attributes":  16,
        "PSPath":  "Microsoft.PowerShell.Core\\FileSystem::C:\\Temp\\Test\\Sub1",
        "PSParentPath":  "Microsoft.PowerShell.Core\\FileSystem::C:\\Temp\\Test",
        "PSChildName":  "Sub1",
        "PSDrive":  {
                        "CurrentLocation":  "Users\\rober",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "OS",
                        "MaximumSize":  null,
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\rober",
                           "Drives":  "C"
                       },
        "PSIsContainer":  true,
        "Mode":  "d-----",
        "BaseName":  "Sub1",
        "Target":  [

                   ],
        "LinkType":  null
    },
    {
        "Name":  "Sub2",
        "FullName":  "C:\\Temp\\Test\\Sub2",
        "Parent":  {
                       "Name":  "Test",
                       "FullName":  "C:\\Temp\\Test",
                       "Parent":  "Temp",
                       "Exists":  true,
                       "Root":  "C:\\",
                       "Extension":  "",
                       "CreationTime":  "\/Date(1523650597878)\/",
                       "CreationTimeUtc":  "\/Date(1523650597878)\/",
                       "LastAccessTime":  "\/Date(1523650688390)\/",
                       "LastAccessTimeUtc":  "\/Date(1523650688390)\/",
                       "LastWriteTime":  "\/Date(1523650688390)\/",
                       "LastWriteTimeUtc":  "\/Date(1523650688390)\/",
                       "Attributes":  16
                   },
        "Exists":  true,
        "Root":  {
                     "Name":  "C:\\",
                     "FullName":  "C:\\",
                     "Parent":  null,
                     "Exists":  true,
                     "Root":  "C:\\",
                     "Extension":  "",
                     "CreationTime":  "\/Date(1446186510642)\/",
                     "CreationTimeUtc":  "\/Date(1446186510642)\/",
                     "LastAccessTime":  "\/Date(1523889482812)\/",
                     "LastAccessTimeUtc":  "\/Date(1523889482812)\/",
                     "LastWriteTime":  "\/Date(1523889482812)\/",
                     "LastWriteTimeUtc":  "\/Date(1523889482812)\/",
                     "Attributes":  22
                 },
        "Extension":  "",
        "CreationTime":  "\/Date(1523650675524)\/",
        "CreationTimeUtc":  "\/Date(1523650675524)\/",
        "LastAccessTime":  "\/Date(1523650675524)\/",
        "LastAccessTimeUtc":  "\/Date(1523650675524)\/",
        "LastWriteTime":  "\/Date(1523650675524)\/",
        "LastWriteTimeUtc":  "\/Date(1523650675524)\/",
        "Attributes":  16,
        "PSPath":  "Microsoft.PowerShell.Core\\FileSystem::C:\\Temp\\Test\\Sub2",
        "PSParentPath":  "Microsoft.PowerShell.Core\\FileSystem::C:\\Temp\\Test",
        "PSChildName":  "Sub2",
        "PSDrive":  {
                        "CurrentLocation":  "Users\\rober",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "OS",
                        "MaximumSize":  null,
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\rober",
                           "Drives":  "C"
                       },
        "PSIsContainer":  true,
        "Mode":  "d-----",
        "BaseName":  "Sub2",
        "Target":  [

                   ],
        "LinkType":  null
    },
    {
        "Name":  "Sub3",
        "FullName":  "C:\\Temp\\Test\\Sub3",
        "Parent":  {
                       "Name":  "Test",
                       "FullName":  "C:\\Temp\\Test",
                       "Parent":  "Temp",
                       "Exists":  true,
                       "Root":  "C:\\",
                       "Extension":  "",
                       "CreationTime":  "\/Date(1523650597878)\/",
                       "CreationTimeUtc":  "\/Date(1523650597878)\/",
                       "LastAccessTime":  "\/Date(1523650688390)\/",
                       "LastAccessTimeUtc":  "\/Date(1523650688390)\/",
                       "LastWriteTime":  "\/Date(1523650688390)\/",
                       "LastWriteTimeUtc":  "\/Date(1523650688390)\/",
                       "Attributes":  16
                   },
        "Exists":  true,
        "Root":  {
                     "Name":  "C:\\",
                     "FullName":  "C:\\",
                     "Parent":  null,
                     "Exists":  true,
                     "Root":  "C:\\",
                     "Extension":  "",
                     "CreationTime":  "\/Date(1446186510642)\/",
                     "CreationTimeUtc":  "\/Date(1446186510642)\/",
                     "LastAccessTime":  "\/Date(1523889482812)\/",
                     "LastAccessTimeUtc":  "\/Date(1523889482812)\/",
                     "LastWriteTime":  "\/Date(1523889482812)\/",
                     "LastWriteTimeUtc":  "\/Date(1523889482812)\/",
                     "Attributes":  22
                 },
        "Extension":  "",
        "CreationTime":  "\/Date(1523650685768)\/",
        "CreationTimeUtc":  "\/Date(1523650685768)\/",
        "LastAccessTime":  "\/Date(1523650697916)\/",
        "LastAccessTimeUtc":  "\/Date(1523650697916)\/",
        "LastWriteTime":  "\/Date(1523650697916)\/",
        "LastWriteTimeUtc":  "\/Date(1523650697916)\/",
        "Attributes":  16,
        "PSPath":  "Microsoft.PowerShell.Core\\FileSystem::C:\\Temp\\Test\\Sub3",
        "PSParentPath":  "Microsoft.PowerShell.Core\\FileSystem::C:\\Temp\\Test",
        "PSChildName":  "Sub3",
        "PSDrive":  {
                        "CurrentLocation":  "Users\\rober",
                        "Name":  "C",
                        "Provider":  "Microsoft.PowerShell.Core\\FileSystem",
                        "Root":  "C:\\",
                        "Description":  "OS",
                        "MaximumSize":  null,
                        "Credential":  "System.Management.Automation.PSCredential",
                        "DisplayRoot":  null
                    },
        "PSProvider":  {
                           "ImplementingType":  "Microsoft.PowerShell.Commands.FileSystemProvider",
                           "HelpFile":  "System.Management.Automation.dll-Help.xml",
                           "Name":  "FileSystem",
                           "PSSnapIn":  "Microsoft.PowerShell.Core",
                           "ModuleName":  "Microsoft.PowerShell.Core",
                           "Module":  null,
                           "Description":  "",
                           "Capabilities":  52,
                           "Home":  "C:\\Users\\rober",
                           "Drives":  "C"
                       },
        "PSIsContainer":  true,
        "Mode":  "d-----",
        "BaseName":  "Sub3",
        "Target":  [

                   ],
        "LinkType":  null
    }
]

Bref un bon gros pavé bien complet en JSON 😀

Et comment on l’exporte ? Comme ça !

Get-ChildItem  C:\Temp\Test\  | ConvertTo-Json | Out-File .\MyFile.json -Encoding utf8 -Force

Voila, c’est tout simple !

 

 

Catégories : Astuces

0 commentaire

Laisser un commentaire

Emplacement de l’avatar

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *