获取结果时,Start-Job \ Receive-Job挂起\冻结 - PrimalForm CE GUI

时间:2015-09-07 04:53:25

标签: winforms powershell powershell-v4.0

我可以开始工作,但是如何在获取信息的同时继续使用我的程序,如果我执行'while'循环它只是冻结程序直到结果完成。

以下是PrimalForms CE版本生成的脚本,我的代码位于button1_OnClick

最终结果我想让脚本运行以检查锁定的AD用户(但实际上可以是任何东西)然后在运行时我想查看以前的结果并在需要时解锁用户...

#Generated Form Function
function GenerateForm {
  ########################################################################
  # Code Generated By: SAPIEN Technologies PrimalForms (Community Edition)      v1.0.10.0
  # Generated On: 7/09/2015 2:24 PM
  # Generated By: stojanp2
  ########################################################################

  #region Import the Assemblies
  [reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
  [reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
  #endregion

  #region Generated Form Objects
  $form1 = New-Object System.Windows.Forms.Form
  $button3 = New-Object System.Windows.Forms.Button
  $button2 = New-Object System.Windows.Forms.Button
  $listView1 = New-Object System.Windows.Forms.ListView
  $button1 = New-Object System.Windows.Forms.Button
  $Users = New-Object System.Windows.Forms.ColumnHeader
  $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
  #endregion Generated Form Objects

  #----------------------------------------------
  #Generated Event Script Blocks
  #----------------------------------------------
  #Provide Custom Code for events specified in PrimalForms.
  $button3_OnClick =
  {
    #TODO: Place custom script here
  }

  $button1_OnClick =
  {
    Start-Job -Name FindUsers -ScriptBlock { Search-ADAccount -LockedOut | Select-Object * }#end Start-Job

    Do {
       Start-Sleep -Seconds 1
       write-host "waiting" # so i can see its doing something in the console
    } Until (@(Get-Job -Name FindUsers).State -eq "Completed")

    Write-Host "done"

    $us = Receive-Job -Name FindUsers
    ForEach ($u IN $us){
      $listView1.Items.Add($u.sAMAccountName)
    }

    Get-Job -Name FindUsers | Remove-Job -Force
  }

  $button2_OnClick =
  {
    #TODO: Place custom script here
  }

  $handler_listView1_SelectedIndexChanged =
  {
    #TODO: Place custom script here
  }

  $OnLoadForm_StateCorrection =
  { #Correct the initial state of the form to prevent the .Net maximized form issue
    $form1.WindowState = $InitialFormWindowState
  }

  #----------------------------------------------
  #region Generated Form Code
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 469
  $System_Drawing_Size.Width = 271
  $form1.ClientSize = $System_Drawing_Size
  $form1.DataBindings.DefaultDataSourceUpdateMode = 0
  $form1.Name = "form1"
  $form1.Text = "Primal Form"

  $button3.DataBindings.DefaultDataSourceUpdateMode = 0

  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 142
  $System_Drawing_Point.Y = 418
  $button3.Location = $System_Drawing_Point
  $button3.Name = "button3"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 23
  $System_Drawing_Size.Width = 117
  $button3.Size = $System_Drawing_Size
  $button3.TabIndex = 3
  $button3.Text = "button3"
  $button3.UseVisualStyleBackColor = $True
  $button3.add_Click($button3_OnClick)

  $form1.Controls.Add($button3)

  $button2.DataBindings.DefaultDataSourceUpdateMode = 0

  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 418
  $button2.Location = $System_Drawing_Point
  $button2.Name 

  = "button2"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 23
  $System_Drawing_Size.Width = 117
  $button2.Size = $System_Drawing_Size
  $button2.TabIndex = 2
  $button2.Text = "button2"
  $button2.UseVisualStyleBackColor = $True
  $button2.add_Click($button2_OnClick)

  $form1.Controls.Add($button2)

  $listView1.Columns.Add($Users)|Out-Null
  $listView1.DataBindings.DefaultDataSourceUpdateMode = 0
  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 73
  $listView1.Location = $System_Drawing_Point
  $listView1.Name = "listView1"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 339
  $System_Drawing_Size.Width = 247
  $listView1.Size = $System_Drawing_Size
  $listView1.TabIndex = 1
  $listView1.UseCompatibleStateImageBehavior = $False
  $listView1.View = 1
  $listView1.add_SelectedIndexChanged($handler_listView1_SelectedIndexChanged)

  $form1.Controls.Add($listView1)

  $button1.DataBindings.DefaultDataSourceUpdateMode = 0

  $System_Drawing_Point = New-Object System.Drawing.Point
  $System_Drawing_Point.X = 12
  $System_Drawing_Point.Y = 12
  $button1.Location = $System_Drawing_Point
  $button1.Name = "button1"
  $System_Drawing_Size = New-Object System.Drawing.Size
  $System_Drawing_Size.Height = 55
  $System_Drawing_Size.Width = 247
  $button1.Size = $System_Drawing_Size
  $button1.TabIndex = 0
  $button1.Text = "button1"
  $button1.UseVisualStyleBackColor = $True
  $button1.add_Click($button1_OnClick)

  $form1.Controls.Add($button1)

  $Users.Name = "Users"
  $Users.Text = "User Names"
  $Users.Width = 116

  #endregion Generated Form Code

  #Save the initial state of the form
  $InitialFormWindowState = $form1.WindowState
  #Init the OnLoad event to correct the initial state of the form
  $form1.add_Load($OnLoadForm_StateCorrection)
  #Show the Form
  $form1.ShowDialog() | Out-Null
} #End Function

#Call the Function
GenerateForm

1 个答案:

答案 0 :(得分:0)

我不能谈论PrimalForm,但在PowerShell Studio中,Button - Start Job部分中有一个名为Toolbox > Control Sets的单独按钮:

enter image description here

使用此类按钮时,会提供一个名为Add-JobTracker的额外功能,用于跟踪作业进度并确保作业在其自己的PowerShell会话中在后台运行。因此,如果您愿意,它不会冻结界面甚至更新进度条。

有关SAPIEN页面PowerShell Studio: Creating responsive forms的更多信息。

每次作业状态都是界面冻结的时候,你正在做一个循环来检查。它会在每次停止时停止,只有在作业完成后才能再次释放。

如果您将PrimalForms更新到最新版本,您可以使用此功能。