Score:1

how export get-vm info to csv file in PowerShell?

in flag

How can I export this output to csv file ?

I have this output from below get-vm command:

Get-VM | Select Name, PowerState, GuestId, @{N="VM Config File";E={$_.extensiondata.config.files.vmpathname}}

Name           : test-vm
PowerState     : PoweredOff
GuestId        : rhel7_64Guest
VM Config File : [DS_001] test-vm/test-vm.vmx

Name           : test-vm2
PowerState     : PoweredOn
GuestId        : sles12_64Guest
VM Config File : [DS_002] test-vm2/test-vm2.vmx

I would like to add also VLAN info get-vm | Get-NetworkAdapter |Select NetworkName

Final Output should be this:

NAME, POWERSTATE, GuestiD, VM Config File, NetworkName
test-vm, PoweredOff,rhel7_64Guest, [DS_001] test-vm/test-vm.vmx, VLAN-100
test-vm2, PoweredOn,sles12_64Guest, [DS_002] test-vm2/test-vm2.vmx, VLAN-200
Score:0
ch flag

A way to get all the VMware system results into one csv file.

This essentially...

  1. Exports the VM data excluding the network name into a csv1.csv file.
  2. Exports the VM data containing the network name into a csv2.csv file.
  3. Uses Write-Output to explicitly define the header columns into a Merged.csv file.
  4. Uses Import-Csv against the csv1.csv file and loops that over a For-EachObject.
  5. Within the For-EachObject loop it:
    • defines a $name variable with the value of $_.Name (from csv1)
    • defines an $oName variable for the NetworkName (from csv2) using Import-Csv against the csv2.csv where its Name value is the same as the current iterated Name [$name] in csv1
    • uses Write-Output to specify all the properties and variables in order matching the column headers
      • uses PowerShell subexpression operators enclosing them in $() and separates each by a comma
        • encloses the entire group of subexpression operators with double quotes then pipes that over to Out-File using the -Append parameter and points to the the final Merged.csv file for the preferred output.

PowerShell

Get-VM  | 
Select Name, PowerState, GuestId, 
    @{N="VM Config File";E={$_.extensiondata.config.files.vmpathname}} |
        Export-Csv "C:\Temp\csv1.csv" -NoTypeInformation;

Get-VM | Get-NetworkAdapter | Select @{n="Name";e={$_.Parent}}, 
     NetworkName | Export-Csv "C:\Temp\csv2.csv" -NoTypeInformation;

Write-Output 'Name, Powerstate, GuestId, VM Config File, NetworkName' | Out-File "C:\Temp\Merged.csv";
Import-Csv "C:\Temp\csv1.csv" | % {
    $name = $_.Name;
    $oName = (Import-Csv C:\Temp\csv2.csv | ? {$_.Name -eq $name} | Select NetworkName).NetworkName;
    Write-Output "$($_.Name), $($_.PowerState), $($_.GuestId), $($_.'VM Config File'), $($oName)" | Out-File "C:\Temp\Merged.csv" -Append;
    };

Supporting Resources

  • Export-Csv

    -NoTypeInformation

    Remove the #TYPE information header from the output. This parameter became the default in PowerShell 6.0 and is included for backwards compatibility.

  • ForEach-Object

    Standard Aliases for Foreach-Object: the '%' symbol, ForEach

  • Where-Object

    The '?' symbol and Where are both aliases for Where-Object. If you explicitly want to run the Where-Object command, run Where-object or '?'

  • PowerShell Operators $( ) @( ) :: &

    $( ) Subexpression operator.

    While a simple ( ) grouping expression means 'execute this part first', a subexpression $( ) means 'execute this first and then treat the result like a variable'.

  • Out-File

    -append

    Add the output to the end of an existing file, instead of replacing the file contents. You need to ensure the -encoding matches any existing content in the target file

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.