如何用 PowerShell 把 JSON 檔案轉換成 CSV 檔案?
要將 JSON 轉換為 CSV 格式,我們需要首先使用 ConvertFrom-JSON 命令。例如,我們已經有了位於 C:\temp\VMinfo.JSON 位置的 JSON 檔案。我們將先匯入此檔案,然後按以下方式將其轉換為 CSV。
Get-Content C:\Temp\VMInfo.json | ConvertFrom-Json | Export-Csv C:\Temp\vminfo.csv -NoTypeInformation
假設您有如以下所示的 cmdlet,其輸出以雜湊表格式生成。
PS C:\> Get-AzVM -VMName TestMachine2k16 | Select -ExpandProperty Tags Key Value --- ----- For Ansible Patching_Day Sunday Application SecretTag Owner Chirag
因此,我們需要首先將雜湊輸出轉換為 JSON 格式,然後從 JSON 中,我們可以將輸出儲存到 excel。
PS C:\> Get-AzVM -VMName TestMachine2k16 | Select -ExpandProperty Tags | ConvertTo-Json | ConvertFrom-Json | Export-Csv C:\temp\Vmtags.csv -NoTypeIn formation
廣告