VMware Cloud Community
vin01
Expert
Expert
Jump to solution

Test telnet connection

Hi

Can someone help me on this script.

There is a network change is going on in one of the customer environment so after change I need to test the VM's network connectivity from my workstation. The problem here is ping was disabled in the environment so I thought of testing be telnet the RDP ports for windows machines and  SSH port for nonwindows VM's.

I have created a script which will get all the port groups and from each port group it picks one windows VM and one linux VM. and tests the network connectivity and writes the output.

Problem:

1.The problem here is if i try to pick one IP either windows or linux box from $winvms and $othervms some IP's are like '169.254.151.255' or some or of blank so here I need to select exact one IP.

Like my Ip series starts with 153* so how to pick any one IP with 153* other than writing (|Select-Object -Property IP -First 1).

2.when I execute this script in powersell ise I can see the computer name but if I try to export to .csv or save in a variable its failing to save computer name.

$pgs = Get-VDPortgroup

foreach ($pg in $pgs) {

$vms = $pg | Get-View | select -ExpandProperty vm | % {$_.tostring()}

$winvms= Get-View -Id $vms| ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -like '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg } |select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}} |Select-Object -Property IP -First 1

$othervms=Get-View -Id $vms| ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -notlike '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg} |select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}} |Select-Object -Property IP -First 1

$output=Test-NetConnection -ComputerName $winvms.IP -Port 3389 -WarningAction SilentlyContinue -InformationLevel Detailed

$output1=Test-NetConnection -ComputerName $othervms.IP -Port 22 -WarningAction SilentlyContinue -InformationLevel Detailed

$report = ""|Select-Object  @{N="Windows VM";E={$output.ComputerName}},@{N="Windows Tested Port";E={$output.RemotePort}},

@{N="Windows VM Tested Portgroup";E={$pg}},

@{N="Windows VM Telnet Status";E={$output.TcpTestSucceeded}},

@{N="Linux VM";E={$output1.ComputerName}},

@{N="Linux VM Tested Port";E={$output1.RemotePort}},

@{N="Linux VM Telnet Status";E={$output1.TcpTestSucceeded}}

}

$report |Export-Csv  -Path 'C:\telnetinfo.csv' -NoTypeInformation -NoClobber

Regards Vineeth.K
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Got it, try like this

$pgs = Get-VDPortgroup

$report = foreach ($pg in $pgs) {

    $vms = $pg | Get-View | select -ExpandProperty vm | % {$_.tostring()}

    $winvms= Get-View -Id $vms|

        ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -like '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg } |

        select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net | where{$_.IpAddress -match "^153."} | %{$_.IpAddress |where{$_.Split('.').Count -eq 4} | Select -first 1 | %{$_}}))}} |

        Select-Object -Property IP -First 1

    $othervms=Get-View -Id $vms| ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -notlike '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg} |

       select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net |  where{$_.IpAddress -match "^153."} | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | Select -first 1 | %{$_}}))}} |

        Select-Object -Property IP -First 1

    if($winvms){

        $output=Test-NetConnection -ComputerName $winvms.IP -Port 3389 -WarningAction SilentlyContinue -InformationLevel Detailed

    }

    else{

        $output = ''

    }

    if($othervms){

        $output1=Test-NetConnection -ComputerName $othervms.IP -Port 22 -WarningAction SilentlyContinue -InformationLevel Detailed

    }

    else{

        $output1 = ''

    }

    if($output -or -$output1){

        ""|Select-Object  @{N="Windows VM";E={$output.ComputerName}},@{N="Windows Tested Port";E={$output.RemotePort}},

            @{N="Windows VM Tested Portgroup";E={$pg}},

            @{N="Windows VM Telnet Status";E={$output.TcpTestSucceeded}},

            @{N="Linux VM";E={$output1.ComputerName}},

            @{N="Linux VM Tested Port";E={$output1.RemotePort}},

            @{N="Linux VM Telnet Status";E={$output1.TcpTestSucceeded}}

    }

}

$report |Export-Csv  -Path 'C:\telnetinfo.csv' -NoTypeInformation -NoClobber


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

View solution in original post

6 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$pgs = Get-VDPortgroup  

$report = foreach ($pg in $pgs) { 

    $vms = $pg | Get-View | select -ExpandProperty vm | % {$_.tostring()} 

    $winvms= Get-View -Id $vms|

        ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -like '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg } |

        select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net | where{$_.IpAddress -match "^153."} | %{$_.IpAddress |where{$_.Split('.').Count -eq 4} | %{$_}}))}} |

        Select-Object -Property IP -First 1 

    $othervms=Get-View -Id $vms| ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -notlike '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg} |

        select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net |  where{$_.IpAddress -match "^153."} | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}} |

        Select-Object -Property IP -First 1 

    $output=Test-NetConnection -ComputerName $winvms.IP -Port 3389 -WarningAction SilentlyContinue -InformationLevel Detailed 

    $output1=Test-NetConnection -ComputerName $othervms.IP -Port 22 -WarningAction SilentlyContinue -InformationLevel Detailed 

    ""|Select-Object  @{N="Windows VM";E={$output.ComputerName}},@{N="Windows Tested Port";E={$output.RemotePort}}, 

        @{N="Windows VM Tested Portgroup";E={$pg}}, 

        @{N="Windows VM Telnet Status";E={$output.TcpTestSucceeded}}, 

        @{N="Linux VM";E={$output1.ComputerName}}, 

        @{N="Linux VM Tested Port";E={$output1.RemotePort}}, 

        @{N="Linux VM Telnet Status";E={$output1.TcpTestSucceeded}} 

}

 

$report |Export-Csv  -Path 'C:\telnetinfo.csv' -NoTypeInformation -NoClobber 


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

0 Kudos
vin01
Expert
Expert
Jump to solution

Thanks LucD. This works partially.

Finally there are two conditions to be added in the script to get correct output.

1. In some vCenter's there are few portgroups with no VM's or all powered off vms so I need to exclude those portgroups in first loop ($vms = $pg | Get-View | select -ExpandProperty vm | % {$_.tostring())

2.For few VM's there are multiple IP's with 153.* series which I can telnet manually with all IPs So Is there any possibility to create a logic to pick only one IP among multiple 153 Ips for a VM.

Ex:

Windows VM                  : 153.71.54.131,153.71.54.133

Windows Tested Port         : 0

Windows VM Tested Portgroup : LAB_3254

Windows VM Telnet Status    : False

Linux VM                    :

Linux VM Tested Port        : 22

Linux VM Telnet Status      : False

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

The Select -First 1 should already limit the selection to 1 IP address.
Do you it otherwise?

Can you give a screenshot of the result where it seems to select more than 1?

$pgs = Get-VDPortgroup 

$report = foreach ($pg in $pgs) {

    $vms = $pg | Get-View | select -ExpandProperty vm | % {$_.tostring()}

    $winvms= Get-View -Id $vms|

        ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -like '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg } |

        select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net | where{$_.IpAddress -match "^153."} | %{$_.IpAddress |where{$_.Split('.').Count -eq 4} | %{$_}}))}} |

        Select-Object -Property IP -First 1

    $othervms=Get-View -Id $vms| ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -notlike '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg} |

       select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net |  where{$_.IpAddress -match "^153."} | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | %{$_}}))}} |

        Select-Object -Property IP -First 1

    if($winvms){

        $output=Test-NetConnection -ComputerName $winvms.IP -Port 3389 -WarningAction SilentlyContinue -InformationLevel Detailed

    }

    else{

        $output = ''

    }

    if($othervms){

        $output1=Test-NetConnection -ComputerName $othervms.IP -Port 22 -WarningAction SilentlyContinue -InformationLevel Detailed

    }

    else{

        $output1 = ''

    }

    if($output -or -$output1){

        ""|Select-Object  @{N="Windows VM";E={$output.ComputerName}},@{N="Windows Tested Port";E={$output.RemotePort}},

            @{N="Windows VM Tested Portgroup";E={$pg}},

            @{N="Windows VM Telnet Status";E={$output.TcpTestSucceeded}},

            @{N="Linux VM";E={$output1.ComputerName}},

            @{N="Linux VM Tested Port";E={$output1.RemotePort}},

            @{N="Linux VM Telnet Status";E={$output1.TcpTestSucceeded}}

    }

}

$report |Export-Csv  -Path 'C:\telnetinfo.csv' -NoTypeInformation -NoClobber


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

0 Kudos
vin01
Expert
Expert
Jump to solution

The Select -First 1 should already limit the selection to 1 IP address.

Can you give a screenshot of the result where it seems to select more than 1?

Yes Select -First 1 is working correctly and it is picking only one VM with IP address but in certain cases its picking a VM(Single VM) which contains multiple IPs below is the output in this a VM contains two IP '153.71.54.131,153.71.54.133' from this I need only one IP to be picked for telnet.

It means after selecting a VM with (Select -First 1) and if the selected VM contains two 153* series IPs it should select only one IP.

Is there any logic to pick any one IP after selecting single VM which contains two 153 series IPs.

telnet.JPG

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Got it, try like this

$pgs = Get-VDPortgroup

$report = foreach ($pg in $pgs) {

    $vms = $pg | Get-View | select -ExpandProperty vm | % {$_.tostring()}

    $winvms= Get-View -Id $vms|

        ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -like '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg } |

        select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net | where{$_.IpAddress -match "^153."} | %{$_.IpAddress |where{$_.Split('.').Count -eq 4} | Select -first 1 | %{$_}}))}} |

        Select-Object -Property IP -First 1

    $othervms=Get-View -Id $vms| ?{$_.Runtime.PowerState -eq 'poweredOn' -and $_.Guest.GuestFullName -notlike '*windows*' -and $_.guest.ToolsRunningStatus -eq 'guestToolsRunning' -and (Get-View -id $_.Network).Name -match $pg} |

       select name, @{N='IP';E={[string]::Join(',',($_.Guest.Net |  where{$_.IpAddress -match "^153."} | %{$_.IpAddress | where{$_.Split('.').Count -eq 4} | Select -first 1 | %{$_}}))}} |

        Select-Object -Property IP -First 1

    if($winvms){

        $output=Test-NetConnection -ComputerName $winvms.IP -Port 3389 -WarningAction SilentlyContinue -InformationLevel Detailed

    }

    else{

        $output = ''

    }

    if($othervms){

        $output1=Test-NetConnection -ComputerName $othervms.IP -Port 22 -WarningAction SilentlyContinue -InformationLevel Detailed

    }

    else{

        $output1 = ''

    }

    if($output -or -$output1){

        ""|Select-Object  @{N="Windows VM";E={$output.ComputerName}},@{N="Windows Tested Port";E={$output.RemotePort}},

            @{N="Windows VM Tested Portgroup";E={$pg}},

            @{N="Windows VM Telnet Status";E={$output.TcpTestSucceeded}},

            @{N="Linux VM";E={$output1.ComputerName}},

            @{N="Linux VM Tested Port";E={$output1.RemotePort}},

            @{N="Linux VM Telnet Status";E={$output1.TcpTestSucceeded}}

    }

}

$report |Export-Csv  -Path 'C:\telnetinfo.csv' -NoTypeInformation -NoClobber


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

vin01
Expert
Expert
Jump to solution

Oops Such a simple thing I missed in the script:smileysilly:.

Thanks for helping LucD. Its working as expected.

Regards Vineeth.K
0 Kudos