VMware Cloud Community
mamoth100
Contributor
Contributor
Jump to solution

Pass vCenter connection to a Job

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

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

Reply
0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

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

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


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

See Running a background job


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

Reply
0 Kudos
mamoth100
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
mamoth100
Contributor
Contributor
Jump to solution

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)

Reply
0 Kudos
mamoth100
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
mamoth100
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
mamoth100
Contributor
Contributor
Jump to solution

Well, I gave the code a shot Smiley Happy 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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


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

Reply
0 Kudos
mamoth100
Contributor
Contributor
Jump to solution

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. Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem, I imagined it was something like that Smiley Happy


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

Reply
0 Kudos