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.
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
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
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?
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
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?
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
Perfect - Thank you!!