VMware Cloud Community
SCharchouf
Hot Shot
Hot Shot
Jump to solution

Assistance to Connect to vCenters Using a Function

Hello

I have created the below function in order to connect to some vcenters, unfortunatly like what I made it ask for crendiatial for each vcenter despite it's the same for All, so I need help to correct the function

Function ConnectTo-vCenter

{

foreach ($vCenter in $vCenterList){

   

    Fonc_LOG "Connecting to $vCenter..."

    $connection = Get-Credential -Message “Please enter your password”

    If($? -Eq $True){

    Fonc_LOG "Connected"

}

}

}

ConnectTo-vCenter

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Inside the Foreach loop, something like this

Function ConnectTo-vCenter

{

    $connection = Get-Credential -Message “Please enter your password”

    foreach ($vCenter in $vCenterList){

        Fonc_LOG "Connecting to $vCenter..."

        Connect-VIServer -Server $vCenter -Credential $connection

        If($? -Eq $True){

            Fonc_LOG "Connected"

        }

    }

}


ConnectTo-vCenter


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

You could move the Get-Credential outside of the Foreach loop.
Btw, the Connect-VIServer cmdlet is missing, I assume that is a typo.

Function ConnectTo-vCenter

{

    $connection = Get-Credential -Message “Please enter your password”

    foreach ($vCenter in $vCenterList){

        Fonc_LOG "Connecting to $vCenter..."

        If($? -Eq $True){

            Fonc_LOG "Connected"

        }

    }

}


ConnectTo-vCenter


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

0 Kudos
SCharchouf
Hot Shot
Hot Shot
Jump to solution

to be honest I missed it and it's working :smileyblush:

Connect-VIServer cmdlet is supposed to be placed where in the script?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Inside the Foreach loop, something like this

Function ConnectTo-vCenter

{

    $connection = Get-Credential -Message “Please enter your password”

    foreach ($vCenter in $vCenterList){

        Fonc_LOG "Connecting to $vCenter..."

        Connect-VIServer -Server $vCenter -Credential $connection

        If($? -Eq $True){

            Fonc_LOG "Connected"

        }

    }

}


ConnectTo-vCenter


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