VMware Cloud Community
SCharchouf
Hot Shot
Hot Shot
Jump to solution

Connect to different vCenter

Would you please help me to understand how we can connect to different vcenter using PowerShell script?

I started with this lines, unfortunately each time I add a vcenter to this script it xonnect to last one on the list:

 


#Initialize PowerCLI
add-pssnapin VMware.VimAutomation.Core

# vCenter Login
$vCUser="domain\user"
$vCPass="Password"

# LIST OF vCenters
$vCenterIP = "x.x.x.x"

$vCenterIP = "x.x.x.x"

foreach ($IPAddress in $vCenterIP){
    # Connessione a vCenter
    Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443
}

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You need to place the names in an array if you want to use a foreach loop like that.

Check what is in $global:defaultviservers after the foreach loop, it should show all your connected vCenters.

# vCenter Login

$vCUser="domain\user"

$vCPass="Password"


# LIST OF vCenters

$vCenterIP = "x.x.x.x","y.y.y.y","z.z.z.z"


foreach ($IPAddress in $vCenterIP){

    # Connessione a vCenter

    Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443

}


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

You need to place the names in an array if you want to use a foreach loop like that.

Check what is in $global:defaultviservers after the foreach loop, it should show all your connected vCenters.

# vCenter Login

$vCUser="domain\user"

$vCPass="Password"


# LIST OF vCenters

$vCenterIP = "x.x.x.x","y.y.y.y","z.z.z.z"


foreach ($IPAddress in $vCenterIP){

    # Connessione a vCenter

    Connect-VIServer $IPAddress -User $vCUser -Password $vCPass -port 443

}


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

SCharchouf
Hot Shot
Hot Shot
Jump to solution

Thank you LucD Smiley Happy

0 Kudos
SCharchouf
Hot Shot
Hot Shot
Jump to solution

it's Ok now and I'm able to connect to 2 vCenter

but regarding $global:defaultviservers I don't have this variable in the script that I'm using.

I'm using the script that I find here Report_VirtualMachine.ps1

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The $global:defaultviserver and $global:defaultviservers are 2 automatic variables that get populated when you do a successful Connect-ViServer


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

0 Kudos