Skip to content

Instantly share code, notes, and snippets.

@SweetAsNZ
Created November 8, 2022 03:00
Show Gist options
  • Save SweetAsNZ/e142460e0a877e9f681c25d6bb6e4394 to your computer and use it in GitHub Desktop.
Save SweetAsNZ/e142460e0a877e9f681c25d6bb6e4394 to your computer and use it in GitHub Desktop.
Get ESX Host Management Network IP's
<#
.Synopsis
Connect to ESX Hosts and Get Mgt IP and Test Connectivity if requested
.DESCRIPTION
.EXAMPLE
Get-VMHostManagementNetwork -VIServer 'vc.domain1.local' -VMHost 'ESX01.domain1.local'
.EXAMPLE
Get-VMHostManagementNetwork -VIServer 'vc2.domain1.local'
#>
Function Get-VMHostManagementNetwork
{
[CmdletBinding(ConfirmImpact='Low')]
Param(
[Parameter(Mandatory=$true)]
[string[]] $VIServer,
[string[]] $Datacenter,
[string[]] $Cluster,
[string[]] $VMHost,
[bool]$Testing = $false,
[bool]$TestConnection = $false
)
# In case you are using snapins here it is
#If (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {Add-PSSnapin VMware.VimAutomation.Core | Out-Null}
if(-not(Get-Module VMware.VimAutomation.Core -ListAvailable)){
Install-Module VMware.VimAutomation.Core -Scope CurrentUser
}
Import-Module VMware.VimAutomation.Core
Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -InvalidCertificateAction Warn -ParticipateInCeip:$false -Scope Session -DisplayDeprecationWarnings:$true -Confirm:$false
# TESTING
if($Testing -eq $true){
$VIServer = 'vc.domain1.local'
$VMHost = 'esx01.domain1.local'
$VMHost = $null # All VMHosts
}
if($VIServer -like "*domain1.local"){
Write-Host -f Green "Connecting with Enhanced Linking On"
Connect-VIServer $VIServer -AllLinked | Out-Null
}
if($VIServer -notlike "*domain1.local"){
Write-Host -f Green "Connecting With Enhanced Linking Off"
Connect-VIServer $VIServer | Out-Null
}
if($Datacenter){
$VMHosts = Get-Datacenter $Datacenter | Get-VMHost
}
ElseIf($Cluster){
$VMHosts = Get-Cluster $Cluster | Get-VMHost
}
ElseIf($VMHost){
$VMHosts = Get-VMHost $VMHost
}
Else{
$VMHosts = Get-VMHost
}
$Mgt = $VMHosts | Select-Object @{N="Name";E={$_.Name}},@{N="ManagementNIC";E={(Get-VMHostNetworkAdapter -VMHost $_.Name | Where-Object {$_.Name -eq "vmk0"}).IP }} | Sort Name ; $Mgt
$MgtNIC = ($Mgt).ManagementNIC ;
if($TestConnection){
Write-Host -f Green "Testing Connectivity to ESX Host MGT IP's"
$Result = @()
foreach ($IP in $MgtNIC)
{
$PortList = @(22,80,427,443)
foreach ($Port in $PortList)
{
$Result += Test-NetConnection $IP -Port $Port | Where {($_.TCPTestSucceeded -eq $true)} | Select ComputerName.RemotePort,RemoteAddress,TcpTestSucceeded
}
}#END FOREACH BIG
}#END IF
$Result
if(-not($TestConnection)){
Write-Warning "Not Testing Connectivity to ESX Host Mgt IP. If Required Use Parameter -TestConnectivity:$true"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment