1

My question involves the specific module for PowerCLI, but I'm hoping it involves a more generic issue to PS Jobs. I'm loading a module needed by commands in the job scriptblock but those commands are not recognized, as though the module is not loaded.

I am loading the VMware powerCLI modele before start-job, as follows, but the code in the scriptblock does not recognize that the module is loaded so the get-vm command is not recognized.

import-module VMWare.VimAutomation.core Connect-VIServer -server VIServer01 $NewJob = start-job -scriptblock {Get-VM -server VIServer01 | Select Name, @{N="Cluster";E={Get-Cluster -VM $}},@{N="ESXHost";E={Get-VMHost -VM $}}}

I am familiar with the need to, and methods of, pass parameters from the script to the job script block, but how do I get it to recognize that the module is loaded? Also, if I load the module from within the scriptblock, I get a stack-overflow failure, I guess due to the repeated looped loading of the module.

Tony A
  • 21
  • 2

1 Answers1

1

Starting a PS job is like starting a new instance of PS console. It does not contain any of the variables or loaded modules/snapins from the scrip from which the job is being created. So I did have to load the snapin and properly pass the variable into the scriptblock) from within the job scriptblock. But by adding the -RunAs32 I was able to prevent the stack overflow error in PS. I do not know why this resolves that error.

foreach ($VIServer in $VIServers){
    $NewJob = start-job {} -scriptblock { param([string]$VIServer)
          Import-Module VMWare.VimAutomation.core
          Connect-VIServer $VIServer
          Get-VM -server $VIServer | Select Name, @(N="vCenter";E={$VIServer}},`
                                  @{N="Cluster";E={Get-Cluster -VM $_}}, @{N="ESXHost";E={Get-VMHost -VM $_}}} -RunAs32 -ArgumentList $VIserver
Tony A
  • 21
  • 2