VMware Cloud Community
umderr
Contributor
Contributor
Jump to solution

PowerCLI to export failed ping results

Hi all!

I am looking for help building a script to export only failed ping results of servers in a vcenter to a .txt or .csv and include name and IP.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You mean the Displayname of the VM?
Because when there are no VMware Tools, there is no hostname available.
I assume then that the VM's Displayname is the same as the hostname, and that the DNS suffixes are defined on the station where you run the tests?
Then this should do the trick.

Get-VM -PipelineVariable vm | ForEach-Object -Process {

    if($vm.PowerState -ne 'PoweredOn'){

        New-Object PSObject -Property @{

            VM = $vm.Name

            Status = 'Not powered on'

        }

    }

    elseif(-not (Test-Connection -ComputerName $vm.Name -Quiet -Count 1)){

        New-Object PSObject -Property @{

            VM = $vm.Name

            Status = 'Ping failed'

        }

    }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Something like this?

Note that if the VMware Tools are not running, the script can not obtain the IP address of the VM.
If the VM's Displayname is the same as it's hostname, and if there are DNS entries for each of these, we could skip the VMware Tools test.

Get-VM -PipelineVariable vm | ForEach-Object -Process {

    if($vm.Guest.State -ne 'Running'){

        New-Object PSObject -Property @{

            VM = $vm.Name

            IP = ''

            Status = 'VMware Tools not running'

        }

    }

    elseif(-not (Test-Connection -ComputerName $vm.Guest.IPAddress[0] -Quiet -Count 1)){

        New-Object PSObject -Property @{

            VM = $vm.Name

            IP = $vm.Guest.IPAddress[0]

            Status = 'Ping failed'

        }

    }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
umderr
Contributor
Contributor
Jump to solution

Yes!! that is very close to exactly what I am looking for. Not all VMs have Tools running, so we can skip that I guess. Can we replace it with VMs that are only powered on? Like only check powered on VMs?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid I would still need to test if the VMware Tools are running, if not there will not be an IP address.
And I added a test of the powerstate before that.

Get-VM -PipelineVariable vm | ForEach-Object -Process {

    if($vm.PowerState -ne 'PoweredOn'){

        New-Object PSObject -Property @{

            VM = $vm.Name

            IP = ''

            Status = 'Not powered on'

        }

    }

    elseif($vm.Guest.State -ne 'Running'){

        New-Object PSObject -Property @{

            VM = $vm.Name

            IP = ''

            Status = 'VMware Tools not running'

        }

    }

    elseif(-not $vm.Guest.IPAddress){

        New-Object PSObject -Property @{

            VM = $vm.Name

            IP = $vm.Guest.IPAddress[0]

            Status = 'No IP address'

        }

    }

    elseif(-not (Test-Connection -ComputerName $vm.Guest.IPAddress[0] -Quiet -Count 1)){

        New-Object PSObject -Property @{

            VM = $vm.Name

            IP = $vm.Guest.IPAddress[0]

            Status = 'Ping failed'

        }

    }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
umderr
Contributor
Contributor
Jump to solution

Ok what if we just axe the IP and just use hostname so that the .csv only contains powered on VMs that are not pinging?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You mean the Displayname of the VM?
Because when there are no VMware Tools, there is no hostname available.
I assume then that the VM's Displayname is the same as the hostname, and that the DNS suffixes are defined on the station where you run the tests?
Then this should do the trick.

Get-VM -PipelineVariable vm | ForEach-Object -Process {

    if($vm.PowerState -ne 'PoweredOn'){

        New-Object PSObject -Property @{

            VM = $vm.Name

            Status = 'Not powered on'

        }

    }

    elseif(-not (Test-Connection -ComputerName $vm.Name -Quiet -Count 1)){

        New-Object PSObject -Property @{

            VM = $vm.Name

            Status = 'Ping failed'

        }

    }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
umderr
Contributor
Contributor
Jump to solution

Perfect - Thank you!!

Reply
0 Kudos