ikiris
Enthusiast
Enthusiast

Trying to get start-job to work

I have a dozen or so vCenters and I wanted to dump some VM info from each of them to feed into another script that merges in some info from our CMDB.

I initially had a script that just had a stanza for each vCenter that did a get-vm and exported to csv but recently I thought that it would be great if I could have one script launch one script per vcenter so that they can run in  parallel.

This is where I got stuck. Since I am connecting to multiple vCenters, I am not connecting to the vCenter in the master script and passing the session down. I am do a connect-viserver in each child script. The connection seems to work fine, I can even do a get-vmhost and a write-host fine, but once I do a get-vm it gets stuck.

I looked through this forum and the only workaround that I saw was to run start-job with parameters "-runas32 -psversion 2.0". to no avail.

I am using PowerCLI 5.8R1 with powershell 3.0 on Windows 2008 R2 and .NET 4.5

I have tried specifying username/cred as well as the pass-through approach for connect-viserver

I have simplified the code to a bare minimum to recreate the issue.

Master script:

Write-Host "starting test job"

Start-Job -FilePath "E:\scripts\vm-guest-info\test.ps1"

$jobs=get-job

while($jobs.count -ne 0){

  foreach ($job in $jobs){

  $job

  if ($job.State -ne "Running"){

  Write-Host Job $job.Command has finished with state $job.State

  Write-Host Job Output

  Receive-Job $job

  Remove-Job $job

  }

  }

  $jobs=get-job

  start-sleep 60

}

Child script test.ps1:

Add-PSSnapin  -Name "VMware.VimAutomation.Core"

Connect-VIServer VCENTER

Get-VM VMINVCENTER

-Chris- http://www.twitter.com/ikiris http://blog.chrischua.net
Tags (1)
0 Kudos
13 Replies
LucD
Leadership
Leadership

I'm using PowerCLI v6, the one with the modules, and there it works without a glitch for me.

This is a rather simplistic sample, but it shows how it works.

Does this work for you ?

$vcenter1 = 'vcenter1'

$vcenter2 = 'vcenter2'

$name1 = 'vm1*'

$name2 = 'vm2*'

$jobcmd = {

    $data = $input.getEnumerator()

    Import-Module VMware.VimAutomation.Core

    $connection = Connect-VIServer $data[0]

    Get-VM | Where{$_.Name -like $data[1]}

    Disconnect-VIServer $connection -Confirm:$false -Force:$true

}

$job1 = Start-Job -InputObject @($vcenter1,$name1) -ScriptBlock $jobcmd

$job2 = Start-Job -InputObject @($vcenter2,$name2) -ScriptBlock $jobcmd

while($job1.State -ne 'Completed' -or $job2.State -ne 'Completed'){

  sleep 5

  $job1 = Get-Job -Id $job1.Id

  $job2 = Get-Job -Id $job2.Id

}

Receive-Job -Job $job1

Receive-Job -Job $job2


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

0 Kudos
ikiris
Enthusiast
Enthusiast

Thanks for the reply, I installed PowerCLI 6 and ran your sample code and it finishes connecting to vCenter but doesn't do anything after that.

-Chris- http://www.twitter.com/ikiris http://blog.chrischua.net
0 Kudos
LucD
Leadership
Leadership

Did you update the variables at the beginning of the script to reflect your environment ?

Did you check with Get-Job and Receive-Job what the jobs did ?


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

0 Kudos
ikiris
Enthusiast
Enthusiast

I Updated the two venter variables and the vm variables.

i Paused the execution in PowerGUI and ran get-job that showed the jobs were still running.

i Ran receive-job on both which showed the jobs connected to their respective vcenters but do not complete the get-vm

-Chris- http://www.twitter.com/ikiris http://blog.chrischua.net
0 Kudos
LucD
Leadership
Leadership

Are they just stuck in the Get-VM or do they produce an error ?


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

0 Kudos
ikiris
Enthusiast
Enthusiast

Just stuck. The only output was the initial vcenter connection.

-Chris- http://www.twitter.com/ikiris http://blog.chrischua.net
0 Kudos
LucD
Leadership
Leadership

Could you try with a Get-VM for a specific VMname, in other words, where the cmdlet only returns 1 object.

Does that get stuck as well ?

On another track, if you run the script from the PowerCLI prompt, does it also get stuck ?


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

0 Kudos
ikiris
Enthusiast
Enthusiast

I was using get-vm with a specific VM to start with, and that gets stuck. I tried get-vm that would get the entire datacenter with my original script.

I ran the script from the powercli command line and it gets stuck as well

-Chris- http://www.twitter.com/ikiris http://blog.chrischua.net
0 Kudos
LucD
Leadership
Leadership

It looks like this is only an issue on WIndows 2008 R2, my tests on Windows 2012 R2 all work perfectly.

Do you have a Windows 2012 R2 available to run the script and confirm my suspicion ?


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

0 Kudos
touimet
Enthusiast
Enthusiast

LucD,  what secret sauce you are using because I am having the same issues ikiris is having whether I use Windows 2008R2, PowerShell 5, PowerCLI 6.3 or Windows 2012 R2, PowerShell 5, PowerCLI 6.3.

I modified the script slightly just to troubleshoot what is going on:

$vcenter1 = 'vcenter1'

$name1 = 'myVM'

$jobcmd = {

   

    $data = $input.getEnumerator()

   

    $data[0]

    $data[1]

    Import-Module VMware.VimAutomation.Core

    $connection = Connect-VIServer $data[0] -User localuser -Password P@ssw0rd

    $connection

#   Get-VM $data[1]

#   Get-VM 'myVM'

    Disconnect-VIServer $connection -Confirm:$false -Force:$true

}

$job1 = Start-Job -InputObject @($vcenter1,$name1) -ScriptBlock $jobcmd

while($job1.State -ne 'Completed'){

  sleep 5

  $job1 = Get-Job -Id $job1.Id

}

Receive-Job -Job $job1

So if I run the script above as is (with Get-VM commented out) the job completes successfully and outputs the two variables and the vCenter connection as expected.

Then I make the following changes:

#  $data[0]

#  $data[1]

#  $connection

Get-VM $data[1]

#  Get-VM 'myVM'

Once I run the script it just stays in a "Running" state never completing.

Or if I try it like this it still will not complete.

#  $data[0]

#  $data[1]

#  $connection

#  Get-VM $data[1]

Get-VM 'myVM'

Any ideas?  This is driving me nuts as it seems like it should be so simple.

Thanks!!!

0 Kudos
LucD
Leadership
Leadership

Did you try disabling warning messages (DisplayDeprecationWarnings) with Set-PowerConfiguration

I have that set for the scope AllUsers


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

0 Kudos
touimet
Enthusiast
Enthusiast

Ha!  You beat me to it!  So I found this thread:

Background PowerCLI jobs hang in Server 2012 when output goes to console.

which I tried piping out to a csv and it worked.  Then I went back and did a Receive-job and found several Deprecation Warnings in the job.  I then figured I'd set the DisplayDeprecationWarnings to false and BAM! it worked!  So I was quite excited to post my findings on both threads but found once again the Master already had the answer.

Thank you for the reply Luc!!!!

DyJohnnY
Enthusiast
Enthusiast

I had the exact same issue, only this one was with get-host.

Running 2008 R2, PS 5,1, PowerCLI 6.5.1

It works flawless in Powershell_ISE and powershell, whenever i wrap it in a job, it just straight out hangs, whenever I return an object like the output of get-vmhost.

As soon as I changed deprecation warnings everything got unlocked.

Thank you for saving me many hair-pulling hours.

IonutN
0 Kudos