Highlighted
Hot Shot
Hot Shot

Check vCenter Connectivity

Jump to solution

I'm trying to connect to some vCenters via PowerCLi and I would like to be sure that there's no session already logged.

I'm using this script but it show me always the last vcenter connected twice

 

$vCenterList = Get-Content ".\vCenterList.txt"

###Check if we are connected to vCenter Server(s)
foreach ($vCenter in $vCenterList){
if($global:DefaultVIServers.Count -gt 0){
# Return current connection
$global:DefaultVIServers[0]
}
else
{
# Finally, connect to the VMware vCenter
Connect-VIServer -Server $vCenterList
}
}

would you please assist me to correct the script in order to show to which vcenter(s) I'm already connected if not connected then it proceed with connectivity

0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

Then I would do something like this

$vCenterList = Get-Content ".\vCenterList.txt"

foreach ($vCenter in $vCenterList){
    if($global:DefaultVIServers.Name -contains $vCenter){
        # Return existing connection
        $global:DefaultVIServers | where{$_.Name -eq $vCenter}
    }
    else
    {
        # Finally, connect to the VMware vCenter
        Connect-VIServer -Server $vCenterList
    }
}


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

View solution in original post

3 Replies
Highlighted
User Moderator
User Moderator

What is in $global:DefaultVIServers before you start your script?
Perhaps do a Disconnect-VIServer -Server * -Confirm:$false at the start of the script.


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

0 Kudos
Highlighted
Hot Shot
Hot Shot

I have nothing in $global:DefaultVIServers

Disconnect-VIServer -Server * -Confirm:$false at the start of the script.

I thought about doing it but it would be smarter to do it via the loop unless it is not possible

0 Kudos
Highlighted
User Moderator
User Moderator

Then I would do something like this

$vCenterList = Get-Content ".\vCenterList.txt"

foreach ($vCenter in $vCenterList){
    if($global:DefaultVIServers.Name -contains $vCenter){
        # Return existing connection
        $global:DefaultVIServers | where{$_.Name -eq $vCenter}
    }
    else
    {
        # Finally, connect to the VMware vCenter
        Connect-VIServer -Server $vCenterList
    }
}


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

View solution in original post