VMware Cloud Community
pamiller21
Enthusiast
Enthusiast

Exporting NIC Report

I am not the most crafty with powerCLI and have been using PowerGUI tools to make some and automate them.  I was looking around and there is a script to find NICs that are under 1Gb/s, following that I was able to make some filters and finally produce a report.  But when I take the pre-made script and attempt to make it automatically produce a .csv it fails.  Here is the script:

$noConnection = $true

if ($defaultVIServers) {

  foreach ($managedHost in $defaultVIServers) {

  $noConnection = $false

  Get-VMHost -Server $managedHost | ForEach-Object {

  $vmh = Get-View -id $_.id -Server $managedHost

  $vmh.Config.Network.Pnic | ForEach-Object {

  $pNicDevId = $_.Pci

  $key = $_.key

  $PCIDevice = $vmh.Hardware.PciDevice | Where-Object {$_.id -eq $pNicDevId}

  $vSwitch = $vmh.Config.Network.Vswitch | Where-Object {$_.Pnic -contains $key}

  $Duplex = "Half"

  if ($_.LinkSpeed.Duplex -eq $True){$Duplex = "Full"}

  $LinkStatus = "Up"

  if ($_.LinkSpeed -eq $null){

  $LinkStatus = "Down"

  $Duplex = $null

  }

  $LinkConfig = "Auto Negotiate"

  if ($_.Spec.LinkSpeed -ne $null){

  $SpecDuplex = "Half"

  if ($_.Spec.LinkSpeed.Duplex -eq $True){$SpecDuplex = "Full"}

  $LinkConfig = "$($_.Spec.LinkSpeed.SpeedMb, $SpecDuplex)"

  }

  if ($_.LinkSpeed.SpeedMb -ne 1000) {

  $pNic = New-Object PSObject

  $pNic.PSObject.TypeNames.Insert(0,"PhysicalNIC#VmwarePowerPackExtension")

  $pNic `

  | Add-Member -MemberType NoteProperty -Name VMHost -Value $vmh.Name -PassThru `

  | Add-Member -MemberType NoteProperty -Name Device -Value $_.Device -PassThru `

  | Add-Member -MemberType NoteProperty -Name LinkSpeed -Value $_.LinkSpeed.SpeedMb -PassThru `

  | Add-Member -MemberType NoteProperty -Name Duplex -Value $Duplex -PassThru `

  | Add-Member -MemberType NoteProperty -Name "Hardware Vendor" -Value $PCIDevice.VendorName -PassThru `

  | Add-Member -MemberType NoteProperty -Name "Hardware Model" -Value $PCIDevice.DeviceName -PassThru `

  | Add-Member -MemberType NoteProperty -Name Location -Value $_.Pci -PassThru `

  | Add-Member -MemberType NoteProperty -Name Driver -Value $_.Driver -PassThru `

  | Add-Member -MemberType NoteProperty -Name Status -Value $LinkStatus -PassThru `

  | Add-Member -MemberType NoteProperty -Name "Wake On LAN" -Value $_.WakeOnLanSupported -PassThru `

  | Add-Member -MemberType NoteProperty -Name LinkConfig -Value $LinkConfig -PassThru `

  | Add-Member -MemberType NoteProperty -Name "Virtual Switch" -Value $vSwitch.name -PassThru `

  | Add-Member -MemberType NoteProperty -Name MACAddress -Value $_.Mac -PassThru `

  | Add-Member -MemberType NoteProperty -Name key -Value $_.key -PassThru `

  | Add-Member -MemberType NoteProperty -Name Host -Value $managedHost.Name -PassThru

  }

  }

  }

  }

}

if ($noConnection){

  Show-MessageBox -Text 'You must connect to one or more vSphere servers before you can run the Hosts with NIC under 1Gb/s best practice query on those servers. Please click on the ''Managed Hosts'' node, connect to one or more of the vSphere servers you have configured there, and then try again.' -Caption 'Connection not established' -Buttons 'OK' -Icon 'Information' | Out-Null

}

Any advice would be great!

Andy

Reply
0 Kudos
1 Reply
RvdNieuwendijk
Leadership
Leadership

Hi Andy,

you can't use the output of the foreach statement in the pipeline because it is a statement and not a cmdlet. You can use the PowerShell & operator to invoke the foreach command as a scriptblock and pipe the output of the scriptblock to the Export-CSV cmdlet. In short:

& { foreach ...} | Export-CSV -Path SpeedInfo.csv -NoTypeInformation -UseCulture


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