5 Replies Latest reply on Nov 12, 2018 11:36 PM by RvdNieuwendijk

    Need a script to ping all servers in vcenter and email results

    derekchandler23 Lurker

      Hello,

      I am hoping to get a script that will ping all servers in a certain vcenter, then email the servers that are not responding. It would be even better if we can find a way to report NIC status as connected or not connected. Thanks in advance!

        • 1. Re: Need a script to ping all servers in vcenter and email results
          alleninbroomfield Novice

          This would be very easy to write, although you'd need to allow e-mail relay, but there are much better ways to do this.  A "ping" is only valuable if you have a server that responds to it, and only if you're pinging from within the same non-routable subnet or through a routable one without firewall restriction... and a ping will only tell you if a NIC is answering back or not, it won't tell you status of any services, etc., so it's only a very small part of the picture.  Depending on the number of servers we're talking, sending a ping to each of them could take time... I've written a PS application that uses multithreading to check on 50 servers (ping, status of services, hard disk space remaining, etc.) simultaneously because we have thousands and it takes a while to run through them all, but I'm not sure I see the value of a ping to each individual server.  Are you trying to locate a subnet failure, or just see if every single server is up during one particular moment in time each day?

           

          SolarWinds has a very good tool for around-the-clock monitoring all your nodes (server, network, application, whatever else you can think of) across the enterprise... very powerful tool actually that you can write your own custom application monitors, etc., and send alerts.

          • 2. Re: Need a script to ping all servers in vcenter and email results
            derekchandler23 Lurker

            We are doing a migration and are having a specific issue where some VMs are getting powered back on with the NIC not connected, and it needs to be deleted and reconfigured. This would allow us to locate these VMs mich quicker, as not all servers are configured for monitoring

            • 3. Re: Need a script to ping all servers in vcenter and email results
              RvdNieuwendijk Virtuoso
              vExpertUser Moderators

              You can use the following PowerCLI command to do a ping test to all virtual machines in your vCenter server:

               

              Get-VM | ForEach-Object {Test-Connection -ComputerName $_.Name -Count 1}

               

              The following PowerCLI shows you the power state and NIC connection status of all of your virtual machines:

               

              Get-VM -PipelineVariable VM | Get-NetworkAdapter |

              Select-Object -Property @{N='VM';E={$VM.Name}},@{N='PowerState';E={$VM.PowerState}},ConnectionState

               

              To send the output in an email, pipe the output of the command to the Export-CSV cmdlet and save the output in a CSV file, for example:

               

              Get-VM | ForEach-Object {Test-Connection -ComputerName $_.Name -Count 1} |

              Export-Csv -Path pingtest.csv -NoTypeInformation -UseCulture

               

              You can use the Send-MailMessage cmdlet to send the CSV file to an email account using the following command:

               

              Send-MailMessage -From "PingTest <pingtest@yourdomain.com>" -To "Derek <derek@yourdomain.com>" -Subject "Ping test result" -Body "The ping test result can be found in the attachment." -Attachments "pingtest.csv" -SmtpServer "smtp.yourdomain.com"

              • 4. Re: Need a script to ping all servers in vcenter and email results
                derekchandler23 Lurker

                Thank you for your help!

                 

                I am hung up on putting it all together. What do I need to add to get the results of "if the server did not ping, then add it to the .csv and continue"

                • 5. Re: Need a script to ping all servers in vcenter and email results
                  RvdNieuwendijk Virtuoso
                  vExpertUser Moderators

                  The following PowerCLI command saves the name and power state of all of the virtual machines that cannot be connected in a CSV file named ConnectionFailed.csv:

                   

                  Get-VM | Foreach-Object {if (-not (Test-Connection -ComputerName $_.Name -Count 1 -Quiet)) {$_ | Select-Object -Property Name,PowerState}} |

                  Export-CSV -Path ConnectionFailed.csv -NoTypeInformation -UseCulture