VMware Cloud Community
selvanvm707
Contributor
Contributor

cmdlet to obtain CRC errors on hosts

i see in one of the vmtn posts, this script that can get the CRC errors for a host

$esxName = 'esx01'

$esxcli = Get-EsxCli -VMHost $esxName -V2
$esxcli.network.nic.list.Invoke() |
ForEach-Object -Process {
$esxcli.network.nic.stats.get.Invoke(@{nicname=$_.Name}) |
Select @{N='VMHost';E={$esxcli.VMHost.Name}},
NICName,PacketsReceived,PacketsSent,ReceiveCRCerrors
}

 

is there a way we can run a cmd like 
get-cluster | get-vmhost | xxxxxxxxx | export-csv -noinformation -path xxxx -useculture

basically, i am looking to get a dump into csv with these columns

cluster| vmhost | NIC | receive CRC errors

much appreciated

0 Kudos
10 Replies
LucD
Leadership
Leadership

You could do something like this

Get-Cluster -PipelineVariable cluster | 
Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
  $esxcli = Get-EsxCli -VMHost $esx -V2
  $esxcli.network.nic.list.Invoke() |
  ForEach-Object -Process {
    $esxcli.network.nic.stats.get.Invoke(@{nicname=$_.Name}) |
    Select @{N='Cluster';E={$cluster.Name}},
      @{N='VMHost';E={$esxcli.VMHost.Name}},
      NICName,PacketsReceived,PacketsSent,ReceiveCRCerrors
  }  
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

selvanvm707
Contributor
Contributor

Hello LucD

the above cmd runs for each esxi host i believe. would there be an amendment to fetch the cluster and run for all hosts?

correct me if i am wrong 

0 Kudos
LucD
Leadership
Leadership

The code runs for all ESXi nodes in all clusters


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

0 Kudos
selvanvm707
Contributor
Contributor

Hello LucD
can you help on this error?

 

PS C:\Users\xxx> Get-Cluster -PipelineVariable xxxx | Get-VMHost -PipelineVariable esx | ForEach-Object -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic.list.Invoke() | ForEach-Object -Process { $esxcli.network.nic.stats.get.Invoke(@{nicname=$_.Name}) | Select @{N='Cluster';E={$cluster.Name}}, @{N='VMHost';E={$esxcli.VMHost.Name}}, NICName,PacketsReceived,PacketsSent,ReceiveCRCerrors } } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
You cannot call a method on a null-valued expression.
At line:1 char:120
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:120
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

0 Kudos
LucD
Leadership
Leadership

Why did you change the PipelineVariable to 'xxx'?
The rest of the code still references $esx


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

0 Kudos
selvanvm707
Contributor
Contributor

LucD

PS C:\Users\xxx> Get-Cluster -PipelineVariable xxxx

=> in the above i just masked my clustername and folder path to dummy characters for privacy.

other than that there are no changes in the cmd i have made

so the cmd is being issued like this

 

Get-Cluster -PipelineVariable myclustername | Get-VMHost -PipelineVariable esx | and goes on with the rest .. 

0 Kudos
LucD
Leadership
Leadership

That will not work, the clustername should not be the argument for the PipelineVariable.

Get-Cluster -Name <yourcluster>  -PipelineVariable cluster | 
Get-VMHost -PipelineVariable esx | 

 


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

0 Kudos
selvanvm707
Contributor
Contributor

Hello LucD

let us know your comments

=> This is what i am getting while running the cmdlet as it is, just connecting to vcenter and not adding anything to your original cmd

PS C:\Users\txij\Downloads\Files> Get-Cluster -PipelineVariable cluster | Get-VMHost -PipelineVariable esx | ForEach-Object -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic.list.Invoke() | ForEach-Object -Process { $esxcli.network.nic.stats.get.Invoke(@{nicname=$_.Name}) | Select @{N='Cluster';E={$cluster.Name}}, @{N='VMHost';E={$esxcli.VMHost.Name}}, NICName,PacketsReceived,PacketsSent,ReceiveCRCerrors } } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
You cannot call a method on a null-valued expression.
At line:1 char:102
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:102
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:102
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:102
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException

 

 

=> this is what i am getting when i use your last tweak and enter the cluster name into the cmd

PS C:\Users\txij\Downloads\Files> Get-Cluster -name XYZ -PipelineVariable cluster | Get-VMHost -PipelineVariable esx | ForEach-Object -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic.list.Invoke() | ForEach-Object -Process { $esxcli.network.nic.stats.get.Invoke(@{nicname=$_.Name}) | Select @{N='Cluster';E={$cluster.Name}}, @{N='VMHost';E={$esxcli.VMHost.Name}}, NICName,PacketsReceived,PacketsSent,ReceiveCRCerrors } } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
You cannot call a method on a null-valued expression.
At line:1 char:134
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:134
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:134
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:1 char:134
+ ... -Process { $esxcli = Get-EsxCli -VMHost $esx -V2 $esxcli.network.nic ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

0 Kudos
LucD
Leadership
Leadership

You are ignoring the LF in my original code.
This for example are 2 lines

  $esxcli = Get-EsxCli -VMHost $esx -V2
  $esxcli.network.nic.list.Invoke() |

Are you just copy/paste my code at the prompt?
You are definitely loosing the LF and CR.

Place the code in a .ps1 file, and run that .ps1 file


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

0 Kudos
selvanvm707
Contributor
Contributor

yep that works, thanks a lot

0 Kudos