Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Convertto-CSV can't handle doubles lines in json file

$
0
0

I'm working on a script that pull info from VM's and places it in a json file.
In case of multiple VHD files or NIC's it will put it as {<value>,<value>}.

When converting the above to CSV (either directly from the output or after being output to json) it shows it can't handle multiple values per line.
It will shows as "System.Object[]".

Oddly enough, if I use https://json-csv.com/ to convert the json output it will nicely place each extra value on a separate line.

Below is the code that I use, if someone wants to test it:

Get-VM -ComputerName <node-name> -VMName <vm-name> | ForEach-Object {
    $vmHdd = $_ | Get-VMHardDiskDrive
    $vmNic = $_ | Get-VMNetworkAdapter
    $vmVlan = $_ | Get-VMNetworkAdapterVlan

    [PSCustomObject] @{
        'Node' = $node
        'VM Name' = $_.Name
        'Processor' = $_.ProcessorCount
        'Memory' = '{0:N0} MB' -f ($_.MemoryAssigned / 1MB)
        'HDD Path' = $vmHdd.Path
        'NIC' = $vmNic.Name
        'MAC Address' = $vmNic.MacAddress
        'Dynamic MAC' = $vmNic.DynamicMacAddressEnabled
        'MAC Spoofing' = $vmNic.MacAddressSpoofing
        'VLAN ID' = $vmVlan.AccessVlanId
        # 'Uptime' = $_.Uptime
        # 'Notes' = $_.Notes
    }
} | ConvertTo-Json | Out-File C:\temp\${env:COMPUTERNAME}_$(Get-Date -Format yyyy-MM-dd)_output.json -Append

$content = Get-Content -Path C:\temp\${env:COMPUTERNAME}_$(Get-Date -Format yyyy-MM-dd)_output.json | ConvertFrom-Json
$content | Export-Csv -NoTypeInformation -Path C:\temp\${env:COMPUTERNAME}_$(Get-Date -Format yyyy-MM-dd)_output.csv

I know the last two lines can be done as a single line, but I keep getting an error, so this works for now.
Apart from the "System.Object[]" obviously.

Following is the CSV file output how the website converts it, and what I would to see using PowerShell.

"Node","VM Name","Processor","Memory","HDD Path","NIC","MAC","Dyn MAC","VLAN ID"
"node-name","vm-name","1","10 GB","C:\ClusterStorage\Volume3\vm-name\vm-name_disk_1.vhd","VM vm-name VLAN 84","mac-address","False","84"
"","","","","C:\ClusterStorage\Volume10\vm-name\vm-name_2.vhdx","VPC5000-vm-name","mac-address","False","2000"

Hopefully someone has an idea how to deal with this.



Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>