-
-
Save irwins/e1ea07d9369eacced449 to your computer and use it in GitHub Desktop.
PowerShell script demonstrating the difference between Csv, Xml & Json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Author: I.C.A Strachan | |
Version: | |
Version History: | |
Purpose: Demo exporting/importing to/from csv, xml and json format | |
#> | |
[CmdletBinding()] | |
#region Nested Object for Demo purposes | |
$Win32_ComputerSystem = @( | |
'DNSHostName' | |
'Model' | |
'Manufacturer' | |
'Domain' | |
'DomainRole' | |
'PartOfDomain' | |
'SystemType' | |
) | |
$Win32_OperatingSystem = @( | |
'Version' | |
'BuildNumber' | |
'BuildType' | |
'OSLanguage' | |
'OSType' | |
'OSArchitecture' | |
'MUILanguages' | |
'OperatingSystemSKU' | |
'Organization' | |
'ProductType' | |
'ServicePackMajorVersion' | |
'ServicePackMinorVersion' | |
'SizeStoredInPagingFiles' | |
'SystemDevice' | |
'SystemDirectory' | |
'WindowsDirectory' | |
) | |
$Win32_BIOS = @( | |
'SMBIOSBIOSVersion' | |
'Manufacturer' | |
'Name' | |
'SerialNumber' | |
'Version' | |
'ReleaseDate' | |
) | |
function Get-WMIData { | |
[cmdletbinding()] | |
param( | |
[string] | |
[ValidateNotNullOrEmpty()] | |
$WMIClass, | |
[string[]] | |
[ValidateNotNullOrEmpty()] | |
$ComputerName, | |
[string[]] | |
[ValidateNotNullOrEmpty()] | |
$WMIProperties | |
) | |
$queryProperties ='' | |
foreach ($property in $WMIProperties){ | |
$queryProperties += "$property," | |
} | |
#Remove last character from $queryProperties | |
$queryProperties = $queryProperties -replace ".$" | |
Get-wmiObject -Query "SELECT $queryProperties FROM $WMIClass" -ComputerName $ComputerName | | |
Select-Object $WMIProperties | |
} | |
$Inventory = @{} | |
$Inventory.ComputerSystem = Get-WMIData -WMIClass Win32_ComputerSystem -ComputerName LocalHost -WMIProperties $Win32_ComputerSystem | |
$Inventory.OperatingSystem = Get-WMIData -WMIClass Win32_OperatingSystem -ComputerName LocalHost -WMIProperties $Win32_OperatingSystem | |
$Inventory.BIOS = Get-WMIData -WMIClass Win32_BIOS -ComputerName LocalHost -WMIProperties $Win32_BIOS | |
#endregion | |
#region Export-Csv | |
$exportSplat = @{ | |
NoTypeInformation = $true | |
Encoding = 'UTF8' | |
Delimiter = ';' | |
} | |
#Export each key to seperate csv File | |
Foreach ($key in $Inventory.Keys) { | |
if ( $Inventory.$key.Count -ne 0 ){ | |
$exportSplat.Path = "$PSSCriptRoot\$($key).csv" | |
$Inventory.$key | | |
Export-Csv @exportSplat | |
} | |
} | |
#Import Csv for verfication | |
$importCsv = @{} | |
#Get csv Files | |
$csvFiles = Get-ChildItem -Filter *.csv -Path $PSSCriptRoot -File | |
$importSplat =@{ | |
Delimiter = ';' | |
Encoding = 'UTF8' | |
} | |
Foreach ($file in $csvFiles) { | |
$importCsv.$($file.BaseName) = Import-Csv -Path $file.FullName @importSplat | |
} | |
#endregion | |
#region Export-Clixml | |
$Inventory | | |
Export-Clixml -Path "$PSSCriptRoot\Inventory.xml" -Encoding UTF8 | |
#Import Xml for verfication | |
$importXml = Import-Clixml -Path "$PSSCriptRoot\Inventory.xml" | |
#endregion | |
#region Export to Json Format using ConvertTo-Json | |
$Inventory | | |
ConvertTo-Json | | |
Out-File "$PSSCriptRoot\Inventory.json" -Encoding utf8 | |
#Import Json for verification | |
$importJson = Get-Content -Path "$PSSCriptRoot\Inventory.json" | | |
ConvertFrom-Json | |
#endregion | |
#region Compare the membertypes of Csv, Xml & Json | |
$importCsv.ComputerSystem | Get-Member #Everything is a string | |
$importXml.ComputerSystem | Get-Member #Typecast is preserved | |
$importJson.ComputerSystem | Get-Member #Typecast is preserved | |
if ($psISE) { | |
psedit "$PSSCriptRoot\Inventory.json" | |
psedit "$PSSCriptRoot\Inventory.xml" | |
} | |
else { | |
notepad.exe "$PSSCriptRoot\Inventory.json" | |
notepad.exe "$PSSCriptRoot\Inventory.xml" | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment