cbreuser
Enthusiast
Enthusiast

script to ping multiple adapters

Jump to solution

HI,

A little help please, i have the below code which looks at all the guests on a host and writes to a text file the Server name, Number of adapters and the ip  of the primary adapter and if it responds to ping. What i want to add which i am struggling with is that if the server has more than 1 adapter (can have a max of 4 in our environment) to ping each adapter and then write to the log that each adapter is either up or down.

foreach ($name in $names){

$Adps = $name | Select Name, @{n="NumNICs"; e={(Get-NetworkAdapter -VM $_ | Measure-Object).Count}} | ?{$_.NumNICs -gt 0}

$numOfNics = $Adps.NumNICs

$vmIP4 = $name | select @{N="IP Address";E={@($_.guest.IPAddress[0])}}

$vmAdp0IP = $vmIP4.'IP Address'

$Output+= "$name has $numOfNics network adapters"

#$Output+= "$name has an IP address of $vmAdp0IP"

  if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){

   $Output+= "$name,and IP $vmAdp0IP is up"

   Write-Host "$Name,up"

  }

  else{

    $Output+= "$nam and IP $vmAdp0IP is,down"

    Write-Host "$Name,down"

  }

}

$Output | Out-file "C:\Scripts\Output\Rich\pingTestResultsfixed_$ESXHost.csv"

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Something like this?

I'm not sure what you are planning to do with $output, but that is not the format to send it a CSV file.

$names = 'vm1','vm2','vm3'

foreach ($vm in Get-VM -Name $names){

    $output += "$($vm.Name) has $($vm.ExtensionData.Network.Count) network adapter(s)`n`r"

    $vm.Guest.IPAddress | where{([System.Net.IPAddress]$_).AddressFamily -eq 'InterNetwork'} | %{

        if(Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue){

            $output += "$($vm.Name),and IP $_ is up`n`r"

            Write-Host "$($vm.Name), $_, up"

        }

        else{

            $output += "$($vm.Name),and IP $_ is down`n`r"

            Write-Host "$($vm.Name), $_, down"

        }

    }

}

$Output

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

Something like this?

I'm not sure what you are planning to do with $output, but that is not the format to send it a CSV file.

$names = 'vm1','vm2','vm3'

foreach ($vm in Get-VM -Name $names){

    $output += "$($vm.Name) has $($vm.ExtensionData.Network.Count) network adapter(s)`n`r"

    $vm.Guest.IPAddress | where{([System.Net.IPAddress]$_).AddressFamily -eq 'InterNetwork'} | %{

        if(Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue){

            $output += "$($vm.Name),and IP $_ is up`n`r"

            Write-Host "$($vm.Name), $_, up"

        }

        else{

            $output += "$($vm.Name),and IP $_ is down`n`r"

            Write-Host "$($vm.Name), $_, down"

        }

    }

}

$Output

---------------------------------------------------------------------------------------------------------

Was it helpful? Let us know by completing this short survey here.


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

0 Kudos
cbreuser
Enthusiast
Enthusiast

That's pretty much it, the network count was always showing a value of 1, not sure if its because they are in a vDS, but I've just added my bit back in and we are all good.

Thanks for your help as always.

0 Kudos