PowerCLI

 View Only
Expand all | Collapse all

Pass vCenter connection to a Job

  • 1.  Pass vCenter connection to a Job

    Posted Jan 06, 2020 03:01 PM

    When you use Start-Job, the connection information for vCenter is not passed. Is there a way to pass connection information for vCenter to a job? Or is there a best practice to do so? Or should the job always re-login each time it's called?

    Just looking for information on the best way to handle jobs and the vCenter connection.

    Thanks



  • 2.  RE: Pass vCenter connection to a Job

    Posted Jan 06, 2020 03:33 PM

    Use the value in the SessionId property on an existing connection ($global:defaultVIServer).

    Then do Connect-VIServer with the Session parameter and this value.



  • 3.  RE: Pass vCenter connection to a Job

    Posted Jan 06, 2020 03:43 PM

    I forgot, I even have a sample in my Dives page.

    See Running a background job



  • 4.  RE: Pass vCenter connection to a Job

    Posted Jan 06, 2020 04:21 PM

    Thank you LucD as always! I saw the session option, but how to pass it wasn't clear on the documentation page. Thank you.



  • 5.  RE: Pass vCenter connection to a Job

    Posted Jan 07, 2020 03:27 PM

    Is there a simple way to pass mutliple vCenters to a job? Using:

    $global:DefaultVIServers.Name and $global:DefaultVIServers.SessionId

    Seems like no matter how I pass those to the Job, it just doesn't reconnect like it does if it's a single vCenter (I even tried to force them to a string and connect)



  • 6.  RE: Pass vCenter connection to a Job

    Posted Jan 07, 2020 03:39 PM

    I think I solved it by parsing the array that is passed to the job

    $code = {

         param (

              [array]$arrServer,

              [array]$arrSessionId

         )

         Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null

         $intCount = 0

         foreach ($strServer in $arrServer)

         {

              Connect-VIServer -Server $strServer -Session $arrSessionId[$intCount] | Out-Null

              $intCount++

         }

         (Get-VM).Count

    }

    $sJOb = @{

         ScriptBlock  = $code

         ArgumentList = $global:DefaultVIServers.Name, $global:DefaultVIServers.SessionId

    }

    Start-Job @sJOb

    Seems messy and not reliable since the SessionID may or may not be in the right order. Although, the way it is, it does work for either single entries or multiple entries. I'm just not 100% that the SessionID will always be in the same order as the vCenter servers list so they match up.



  • 7.  RE: Pass vCenter connection to a Job
    Best Answer

    Posted Jan 07, 2020 04:31 PM

    As an alternative, you can pass an array pf PSObjects.
    Something like this

    $code = {

        param (

             [array]$arrServer

        )


        Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -Confirm:$false | Out-Null

        foreach ($objServer in $arrServer)

        {

             Connect-VIServer -Server $objServer.Name -Session $objServer.Id  | Out-Null

        }

        (Get-VM).Count

    }


    $sJob = @{

        ScriptBlock  = $code

        ArgumentList = $global:DefaultVIServers | ForEach-Object -Process {New-Object PSObject -Property @{Name=$_.Name;Id=$_.SessionId}}

    }

    Start-Job @sJob



  • 8.  RE: Pass vCenter connection to a Job

    Posted Jan 07, 2020 07:01 PM

    Perfect. Thank you LucD. That will make me feel better that things will line up with vCenter names and SessionIDs.



  • 9.  RE: Pass vCenter connection to a Job

    Posted Jan 08, 2020 02:49 PM

    Well, I gave the code a shot :smileyhappy: It seems as though when the PS Object is passed to the job it only pulls from the last logged into server even though multiple vCenters are connected and the $global:DefaultVIServers shows all of them. Once passed to the job, the job only pulls data from the last vCenter connected to. Weird.



  • 10.  RE: Pass vCenter connection to a Job

    Posted Jan 08, 2020 02:58 PM

    You are right, passing an array on ArgumentList is limited.
    One option is to pass a dummy parameter at the end.

    Something like this

    $job = {

        param(

            [PSObject[]]$Array,

            [String]$Dummy

        )


        $Array | ForEach-Object -Process {

            $_

        }

    }


    $arrP = 1..4 | ForEach-Object -Process {

        New-Object -TypeName PSObject -Property @{

            Field1 = $_

            Field2 = $_ * 2

        }

    }


    Start-Job -ScriptBlock $job -ArgumentList $arrP,'Dummy' |

    Wait-Job | Receive-Job -Keep



  • 11.  RE: Pass vCenter connection to a Job

    Posted Jan 08, 2020 03:00 PM

    And an alternative is to use this method

    $job = {

        param(

            [PSObject[]]$Array

        )


        $Array | ForEach-Object -Process {

            $_

        }

    }


    $arrP = 1..4 | ForEach-Object -Process {

        New-Object -TypeName PSObject -Property @{

            Field1 = $_

            Field2 = $_ * 2

        }

    }


    Start-Job -ScriptBlock $job -ArgumentList (,$arrP) |

    Wait-Job | Receive-Job -Keep



  • 12.  RE: Pass vCenter connection to a Job

    Posted Jan 08, 2020 03:02 PM

    I find it strange that you removed the Correct Answer.
    I think I answered your initial question, the later question about passing multiple connections was indeed not yet answered.



  • 13.  RE: Pass vCenter connection to a Job

    Posted Jan 08, 2020 03:04 PM

    I was in the middle of moving the correct answer and got caught up with a walk up at work. You caught me in the middle of it. Apologies.

    Thank you for the additional code. :smileyhappy:



  • 14.  RE: Pass vCenter connection to a Job

    Posted Jan 08, 2020 03:05 PM

    No problem, I imagined it was something like that :smileyhappy: