VMware Cloud Community
a2alpha
Expert
Expert
Jump to solution

Help with PowerShell Script to return physical NIC speeds - object error - op_Addition MethodNotFound

Hi,

I am having some object related issues when running this script to list physical NIC speeds per NIC per host. The error is:

Method invocation failed because http://System.Management.Automation.PSObject doesn't contain a me

thod named 'op_Addition'.

At line:12 char:23

+ $NicSpReportObj += <<<< $NicSpReportObj

+ CategoryInfo : InvalidOperation: (op_Addition:String) [], RuntimeException

+ FullyQualifiedErrorId : MethodNotFound

HostName pNIC Speed

esx14.domain.com pnic0 1000

So it will list what I would like to be the last entry in the object but obviously its not adding in the previous info (or wiping it out maybe).

If someone could take a look that would be great,

Thanks,

Dan

Script (also attached):

$VMHosts = Get-VMHost | Sort Name | Get-View

$NicSpReport = @()

$NicSpReportObj = "" | Select-Object HostName, pNIC, Speed

Foreach ($VMHost in $VMHosts)

{

$pnic = 0

Do {

$Speed = $VMHost.Config.Network.Pnic[$pnic].LinkSpeed.SpeedMb

$NicSpReportObj.HostName = $VMHost.Name

$NicSpReportObj.pNIC = "pnic$pnic"

$NicSpReportObj.Speed = $speed

$NicSpReportObj += $NicSpReportObj

$pnic ++}

Until ($pnic -eq ($VMHost.Config.Network.Pnic.Length))

}

$NicSpReport += $NicSpReportObj

$NicSpReport | ft -AutoSize

Reply
0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I did a little rewrite of your script. I hope it will do what you want now:

Get-VMHost | ForEach-Object {
  $VMHost = $_
  $VMHost.Extensiondata.Config.Network.Pnic | ForEach-Object {
    $NicSpReportObj = "" | Select-Object HostName, pNIC, Speed
    $NicSpReportObj.HostName = $VMHost.Name
    $NicSpReportObj.pNIC = $_.Device
    $NicSpReportObj.Speed = $_.LinkSpeed.SpeedMb
    $NicSpReportObj
  }
} | Sort-Object -property HostName,pNIC | Format-Table -AutoSize

Regards, Robert

Update: Moved the Sort-Object cmdlet to the end of the script to be able to also sort on the pNIC attribute.

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

Reply
0 Kudos
4 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

I did a little rewrite of your script. I hope it will do what you want now:

Get-VMHost | ForEach-Object {
  $VMHost = $_
  $VMHost.Extensiondata.Config.Network.Pnic | ForEach-Object {
    $NicSpReportObj = "" | Select-Object HostName, pNIC, Speed
    $NicSpReportObj.HostName = $VMHost.Name
    $NicSpReportObj.pNIC = $_.Device
    $NicSpReportObj.Speed = $_.LinkSpeed.SpeedMb
    $NicSpReportObj
  }
} | Sort-Object -property HostName,pNIC | Format-Table -AutoSize

Regards, Robert

Update: Moved the Sort-Object cmdlet to the end of the script to be able to also sort on the pNIC attribute.

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
a2alpha
Expert
Expert
Jump to solution

Exactly what I was after, better method than I was going down!

Thanks so much for the fast response, help and effort, really appreciated.

Dan

Reply
0 Kudos
E4F
Contributor
Contributor
Jump to solution

Excellent script, very cool!

Reply
0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Thanks Robbie !

Kind Regards,

AWT

/* Please feel free to provide any comments or input you may have. */
Reply
0 Kudos