markvm2
Enthusiast
Enthusiast

Invoke-VMScript inside Start-Job/ScriptBlock leaves Powershell process running when execution is done

I have a script that runs Invoke-VMScript from inside a function that starts a new Job with a ScriptBlock and then connects to vCenter with Connect-VIServer and executes Invoke-VMScript. The Invoke-VMScript and the entire function runs fine but I recently noticed that the PowerShell process created by he Start-Job stays running and never exits. When I use this script I start to see a lot of PowerShell processes running in the task manager and they never exit. I have tried various things like disconnecting the VISession after Invoke-VMScript and killing the Start-Job process, but nothing seems to work. Also, in the vSphere client if I look at the open session the sessions remain open.

Does anyone know what would cause this and how to kill that process?

0 Kudos
3 Replies
LucD
Leadership
Leadership

It could be that the script is waiting for input.

Or it can be caused by a warning message, can you try to set

$WarningPreference = "SilentlyContinue"


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

0 Kudos
Brian_Graf
Enthusiast
Enthusiast

one thing you can do is add a timer to your script that will output the get-job status to a text file. I'm assuming you are already logging what happens in your script? with a timestamp? that is where I would start. Output to a file and then cross it with the job status timestamps to see if it finishes, just stays running, etc.

at the end of the powershell scripts do you exit-pssession?

I had the same issue for a while with some scripts but it turned out the jobstatus wasn't "completed" but "failed" or one of the other ones, my timer would terminate the processes once it saw that the jobstatus was completed... just a thought.

Senior Product Manager - Distributed Resource Management | @vBrianGraf
0 Kudos
naiksidd
Enthusiast
Enthusiast

Hi,

I recently did a big migration using parallel executions with start-job and Add-PSSnapin , simplest way I did was to create a job monitor function which would allow me to end any process.

I am not very good at writing small one liners but may be this will help

do{

                                 $jobsInfo = Get-Job;

                                 $jobcounters = 1;

                                 foreach($sepJob in $jobsInfo)

                                 {

                                 $JobID = $sepJob.ID;

                                 $JobState = $sepJob.State;

  Write-Host "Batch $jobcounters $JobState JobID : $JobID";

                                 $jobcounters = $jobcounters+1;

                                 }

  $toggle = Read-Host "Enter 1 to Stop/Start a Batch, else Press 0"

  if($toggle -eq 1)

  {

  $stopJob = Read-Host "Enter JobID to Stop a Batch, Press 2 to start a Job"

  if($stopJob -eq 2)

  {

  restartJob; #call restart function restarts previosly stopped function.

  }

  if($stopJob -ne 2)

  {

  $jobinfo = Get-Job -Id $stopJob;

                                    $sss = $jobinfo.State

                                    Write-host "Current Job State is $sss";

                                    Write-Host " ";

                                    Write-Host " ";

                                   

  Write-Host "Stopping and removing Batch with Job ID $stopJob";

  Remove-Job -Id $stopJob -Force;

  }

  }

  Write-Host " ";

                                 Write-Host " ";

                                 $a = read-Host "Press [y] to see job details, Press n to exit"} while($a -ne "n")

I call this function from the calling function

0 Kudos