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
As an alternative, you can pass an array pf PSObjects.
Something like this
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
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
I forgot, I even have a sample in my Dives page.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you LucD as always! I saw the session option, but how to pass it wasn't clear on the documentation page. Thank you.
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)
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.
As an alternative, you can pass an array pf PSObjects.
Something like this
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
Perfect. Thank you LucD. That will make me feel better that things will line up with vCenter names and SessionIDs.
Well, I gave the code a shot 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.
You are right, passing an array on ArgumentList is limited.
One option is to pass a dummy parameter at the end.
Something like this
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
And an alternative is to use this method
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
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
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.
No problem, I imagined it was something like that
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference