Powershell Runspace not expected output (miss understanding?)

Welcome Forums General PowerShell Q&A Powershell Runspace not expected output (miss understanding?)

Viewing 3 reply threads
  • Author
    Posts
    • #195449
      Participant
      Topics: 1
      Replies: 1
      Points: 17
      Rank: Member
      $ScriptBlock = {
      Param (
      $Vm,
      $vc,
      $session)
      $VCenter = Connect-VIServer -Server $vc -session $session -force
      $status = Get-VM $Vm
      
      Write-host $status.Name
      
      if ($status.PowerState -eq “PoweredOff”)
      {
      $Clone = ‘_2.2’
      
      $NewVM = $Vm + $Clone
      Write-host $NewVM
      
      }
      
      }
      
      $Results = @()
      $Throttle = 5 #threads
      $sesssionstate = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()
      $sesssionstate.ImportPSModule(“Vmware.VimAutomation.Core”)
      $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $Throttle, $sesssionstate, $host)
      $RunspacePool.Open()
      $Jobs = @()
      
      $Vms = gc .\vmlist.txt
      Foreach ($Vm in $Vms)
      {
      #Start-Sleep -Minutes 1
      $Job = [powershell]::Create().AddScript($ScriptBlock).AddArgument($Vm).AddArgument($con.Name).AddArgument($con.SessionId)
      $Job.RunspacePool = $RunspacePool
      $Jobs += New-Object PSObject -Property @{
      Job = $Job
      Result = $Job.BeginInvoke()
      }
      
      }
      
      while ($Jobs -Contains $false){}
      
      foreach($Job in $jobs)
      {
      $results+=$Job.Job.EndInvoke($job.Result)
      $Job.Job.Dispose()
      }
      
       
      
      $results
      
      

      Input :

      ======

      INTCL2K8
      INTCL2K82
      INTCL2k83

      Output:

      =============

      PS C:\Users\Administrator\Desktop> .\tests.ps1
      INTCL2K8
      INTCL2K8_2.2
      INTCL2K82
      INTCL2K82_2.2
      INTCL2k83
      INTCL2K83_2.2
      PS C:\Users\Administrator\Desktop> .\tests.ps1
      INTCL2K82
      INTCL2K8
      INTCL2K82_2.2
      INTCL2K8_2.2
      INTCL2k83
      INTCL2K83_2.2
      PS C:\Users\Administrator\Desktop> .\tests.ps1
      INTCL2K82
      INTCL2K8
      INTCL2k83
      INTCL2K82_2.2
      INTCL2K8_2.2
      INTCL2K83_2.2

       

      My understanding is runspace  take input sequentially and run separately.but the output is not sequential. is it because of runtime different and output which one complete first?

      The second doubt is how the output “_2.2” VMs comes after one if the thread is running independently. is the code wrong?

      the output should be always like this

      INTCL2K8
      INTCL2K8_2.2
      INTCL2K82
      INTCL2K82_2.2
      INTCL2k83
      INTCL2K83_2.2

       

       

    • #195476
      js
      Participant
      Topics: 31
      Replies: 835
      Points: 2,613
      Helping Hand
      Rank: Community Hero

      With parallel processing, the order of the output is random.

    • #195548
      Participant
      Topics: 1
      Replies: 1
      Points: 17
      Rank: Member

      With parallel processing, the order of the output is random.

      Thanks for response, still i cant understand the “with parallel processing output is random” because the script block has the write-host to output one after another . how the “_2.2” output comes before respective input INTCLWK8. How do i ensure the thread is running independently?

       

    • #195557
      js
      Participant
      Topics: 31
      Replies: 835
      Points: 2,613
      Helping Hand
      Rank: Community Hero

      I know write-host can have a funny timing when used along with other kinds of output. The implied running of format-table adds different kinds of weird delays.

      [pscustomobject]@{message = "first"}; write-host second
      
      
      second
      message
      -------
      first
      
Viewing 3 reply threads
  • The topic ‘Powershell Runspace not expected output (miss understanding?)’ is closed to new replies.