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
}
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
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.
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
I tried you report but i get this output:
IsReadOnly | IsFixedSize | IsSynchronized | Keys | Values | SyncRoot | Count |
FALSE | FALSE | FALSE | System.Collections.Hashtable+KeyCollection | System.Collections.Hashtable+ValueCollection | System.Object | 3 |
FALSE | FALSE | FALSE | System.Collections.Hashtable+KeyCollection | System.Collections.Hashtable+ValueCollection | System.Object | 3 |
FALSE | FALSE | FALSE | System.Collections.Hashtable+KeyCollection | System.Collections.Hashtable+ValueCollection | System.Object | 3 |
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.com | vmnic0 | ixgbe | 3.7.13.7.14iov-NAPI | |
prod3-01.test.com | vmnic1 | ixgbe | 3.7.13.7.14iov-NAPI | |
prod3-01.test.com | vmnic2 | igb | 5.0.5.1 | |
prod3-01.test.com | vmnic3 | igb | 5.0.5.1 | |
prod3-01.test.com | vmnic4 | ixgbe | 3.7.13.7.14iov-NAPI | |
prod3-01.test.com | vmnic5 | ixgbe | 3.7.13.7.14iov-NAPI |
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
Only thing it is missing is the ESXi host. I get a list of VMnics but have no idea which host they belong to.
Name | Version | ID |
vmnic0 | 3.7.13.7.14iov-NAPI | VMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585 |
vmnic1 | 3.7.13.7.14iov-NAPI | VMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585 |
vmnic2 | 5.0.5.1 | VMware_bootbank_net-igb_5.0.5.1.1-5vmw.600.0.0.2494585 |
vmnic3 | 5.0.5.1 | VMware_bootbank_net-igb_5.0.5.1.1-5vmw.600.0.0.2494585 |
vmnic4 | 3.7.13.7.14iov-NAPI | VMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585 |
vmnic5 | 3.7.13.7.14iov-NAPI | VMware_bootbank_net-ixgbe_3.7.13.7.14iov-20vmw.600.0.0.2494585 |
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