VMware Cloud Community
PorzioM
Contributor
Contributor
Jump to solution

Can someone help me with formatting the output of this script into an easily readable CSV?

Can someone help me with formatting the output of this script into an easily readable CSV?

Import-Module VMware.VimAutomation.Core

Import-Module VMware.VimAutomation.VDS

Connect-VIServer -Server X.X.X.X -Protocol https -User administrator@vsphere.local -Password password

$VMhosts = Get-VMHost

$hosts = $VMhosts.Name

foreach($i in $hosts){

echo $i

$esxcli = Get-EsxCli -VMHost $i

$x=$Esxcli.network.nic.list() | Select-Object *, @{N="FirmwareVersion"; E={$Esxcli.network.nic.get($_.name).driverinfo.firmwareVersion}}, @{N="Version"; E={$Esxcli.network.nic.get($_.name).driverinfo.Version}}

$y = $esxcli.software.vib.list() | Where-Object {$_.ID -Like "*ixgbe*"}

echo $y | Select-Object -Property ID

echo $x | Select-Object -Property Name,Version

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Here you go

$report = foreach($esx in Get-VMHost){

    $esxcli = Get-EsxCli -VMHost $esx

    foreach($nic in $esxcli.network.nic.list()){

        $nicDetail = $esxcli.network.nic.get($nic.Name)

        $nicVib = $esxcli.software.vib.list() | where{$_.Name -match "net-$($nicDetail.DriverINfo.Driver)$"}

        New-Object PSOBject -Property @{

            VMHost = $esx.Name

            Name = $nic.Name

            Version = $nicDetail.DriverInfo.Version

            ID = $nicVib.ID

        }

    }

}

$report |

Select VMHost,Name,Version,ID |

Export-Csv .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
sjesse
Leadership
Leadership
Jump to solution

look at this example


foreach
($domain in $domains) {

  
Get-QADComputer -Service $domain -SizeLimit 0 | Foreach-Object {

  $reachable
= Test-Connection -ComputerName $_.Name -count 1 -Quiet

  
if($reachable)
  
{
  $IPAddress
= [System.Net.Dns]::GetHostAddresses($_.Name)|select-object IPAddressToString -expandproperty IPAddressToString
  
}
  
else
  
{
  $IPAddress
= $null
  
}


  
New-Object -TypeName PSObject -Property @{
  
SystemName = $_.Name.ToLower()
  
Reachable = $reachable
  
Domain = $_.Domain
  
IPAddress = $IPAddress
  
} | Select-Object SystemName,Domain,IPAddress

  
} |

Export-Csv -Path export.csv -Append 

from powershell - Gather info through several foreach and then export-csv at the end of script - Stack Ov...     .The parts where you do the Select-Object is where you would want to create a new psobject.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

$report = foreach($esx in Get-VMHost){

    $esxcli = Get-EsxCli -VMHost $esx

    foreach($nic in $esxcli.network.nic.list()){

        $nicDetail = $esxcli.network.nic.get($nic.Name)

        $nicVib = $esxcli.software.vib.list() | where{$_.Name -eq $nicDetail.DriverINfo.Driver}

        New-Object PSOBject @{

            Name = $nic.Name

            Version = $nicDetail.DriverInfo.Version

            ID = $nicVib.ID

        }

    }

}

$report | Export-Csv .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
PorzioM
Contributor
Contributor
Jump to solution

I tried you report but i get this output:

 

IsReadOnlyIsFixedSizeIsSynchronizedKeysValuesSyncRootCount
FALSEFALSEFALSESystem.Collections.Hashtable+KeyCollectionSystem.Collections.Hashtable+ValueCollectionSystem.Object3
FALSEFALSEFALSESystem.Collections.Hashtable+KeyCollectionSystem.Collections.Hashtable+ValueCollectionSystem.Object3
FALSEFALSEFALSESystem.Collections.Hashtable+KeyCollectionSystem.Collections.Hashtable+ValueCollectionSystem.Object3

Ended up doing this:

Get-Module -ListAvailable PowerCLI* | Import-Module;

Connect-VIServer -Server <x.x.x.x> -Protocol https -User administrator@vsphere.local -Password <password>

$hosts = Get-VMHost

$getlisthost = $hosts.Name

Add-Content -Path c:\vmincsFirmware.csv -Value '"Host Name", "vmnic", "Driver", "Version"'

foreach($line in $getlisthost){

  $esxcli = Get-EsxCli -VMHost $line -V2

  $niclist=$esxcli.network.nic.list.Invoke()

  foreach ($nic in $niclist) {

    $args = $esxcli.network.nic.get.createargs()

    $args.nicname = $nic.Name

    $nicdetail = $esxcli.network.nic.get.Invoke($args)

    $newline = "{0},{1},{2},{3}" -f $line, $nicdetail.Name, $nicdetail.driverinfo.Driver, $nicdetail.driverinfo.Version

    Add-Content -Path c:\vmincsFirmware.csv -Value $newline

   }

   }

with this we get:

 

Host Name "vmnic" "Driver" "Version"
prod3-01.test.comvmnic0ixgbe3.7.13.7.14iov-NAPI
prod3-01.test.comvmnic1ixgbe3.7.13.7.14iov-NAPI
prod3-01.test.comvmnic2igb5.0.5.1
prod3-01.test.comvmnic3igb5.0.5.1
prod3-01.test.comvmnic4ixgbe3.7.13.7.14iov-NAPI
prod3-01.test.comvmnic5ixgbe3.7.13.7.14iov-NAPI
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Forgot the Property parameter on the New-Object.

This should work better, and will also give you the Vib ID

$report = foreach($esx in Get-VMHost){

    $esxcli = Get-EsxCli -VMHost $esx

    foreach($nic in $esxcli.network.nic.list()){

        $nicDetail = $esxcli.network.nic.get($nic.Name)

        $nicVib = $esxcli.software.vib.list() | where{$_.Name -match "net-$($nicDetail.DriverINfo.Driver)$"}

        New-Object PSOBject -Property @{

            Name = $nic.Name

            Version = $nicDetail.DriverInfo.Version

            ID = $nicVib.ID

        }

    }

}

$report | Export-Csv .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
PorzioM
Contributor
Contributor
Jump to solution

Only thing it is missing is the ESXi host. I get a list of VMnics but have no idea which host they belong to.

   

NameVersionID
vmnic03.7.13.7.14iov-NAPIVMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585
vmnic13.7.13.7.14iov-NAPIVMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585
vmnic25.0.5.1VMware_bootbank_net-igb_5.0.5.1.1-5vmw.600.0.0.2494585
vmnic35.0.5.1VMware_bootbank_net-igb_5.0.5.1.1-5vmw.600.0.0.2494585
vmnic43.7.13.7.14iov-NAPIVMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585
vmnic53.7.13.7.14iov-NAPIVMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Here you go

$report = foreach($esx in Get-VMHost){

    $esxcli = Get-EsxCli -VMHost $esx

    foreach($nic in $esxcli.network.nic.list()){

        $nicDetail = $esxcli.network.nic.get($nic.Name)

        $nicVib = $esxcli.software.vib.list() | where{$_.Name -match "net-$($nicDetail.DriverINfo.Driver)$"}

        New-Object PSOBject -Property @{

            VMHost = $esx.Name

            Name = $nic.Name

            Version = $nicDetail.DriverInfo.Version

            ID = $nicVib.ID

        }

    }

}

$report |

Select VMHost,Name,Version,ID |

Export-Csv .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos