Highlighted
Hot Shot
Hot Shot

Assistance to Connect to vCenters Using a Function

Jump to solution

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
Highlighted
User Moderator
User Moderator

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
Highlighted
User Moderator
User Moderator

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
Highlighted
Hot Shot
Hot Shot

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
Highlighted
User Moderator
User Moderator

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