DataGridView。并行输入新行时运行作业

时间:2018-10-16 16:00:57

标签: powershell datagridview start-job

包含的脚本用于检查我添加的每个服务器的复制和修复。我想到的目标是,在添加计算机时自动检查每台计算机的连接并检查正常运行时间。

我当前遇到的问题是,在允许我输入下一台计算机之前,它会延迟检查正常运行时间。

您可以在我的脚本中看到,我正在尝试使用一个对我来说根本不起作用的开始作业,因为它说'ComputerName'为空,但是ComputerName是在Get-Uptime中设置的功能。

这可能是我错了。任何帮助都会很棒!

function Check-Replication {
    #----------------------------------------------
    #region Import the Assemblies
    #----------------------------------------------
    [void][Reflection.Assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][Reflection.Assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][Reflection.Assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    [void][Reflection.Assembly]::Load('System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    [void][Reflection.Assembly]::Load('System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    #endregion Import Assemblies

    $base64Image = "iVBORw0KGgoAAAANSUhEUgAAABMAAAATCAQAAADYWf5HAAAABGdBTUEAALGPC/xhBQAAAAJiS0dEAP+Hj8y/AAAACXBIWXMAAWG3AAFhtwFW7IyrAAAAB3RJTUUH4goDDxY48JvtEgAAAg9JREFUKM9NkT9MU1EUxn/33kf/APVPYQGaYImLYQAikU6kidFoWBwYjLgw6SZxkLhgnExc0A1ZHDC6uOmMDQ5KLCYuqINxoFgq2Ce0fbx/912GvhDOlzN9X77znXMEAFyhL7Uxmbl9vjjQn1ahrv35Vdp7NbxWc1eJaxFJIX9h+UZ9yZTMF/PVlM1Hs2Jm6yPLl/KwCIinPGZwxHsxPnGTXhQSgSEi4oB3rK7LO1vfFlA75Ib2X54rTJNFIBGAISLE0MdOrjo69GHDlteT9nxXYZQMEWBOQNPBGJmCPX81qZzL/kIuPUwPHVjxSIiI0Pj4/MXOV8pWa6Yzm0XFhIzXimLAGTqzrRkrKnbThSYgxELEwoiQkJCATrppFi09kCKJwSNJgEEgMBhCAjwCEqTQA5ZW7SME+AiiE24BPgaJQiuptI5zaCLMcRs0bSZEaSm3D/HwCdBodCzS6Dibh4valrLUoEGLQ1x8guPoPh6HODRpQkkNOs5UIp0mgUIAEZoQH5cm+/yjym6965GarXwfdC8msOJfhrFPW1Sjgl65tqRsfXrz/4SXkxg0IR4uDg322aPGFo3PPfc399QD3ti95dZYKxfg4+LQ4gCbXapUaKwn7/78MYeA59yjkK8/NNPpsxlSKDQuTRxbvs0++fT7GXOI9g+n6E+VJw9umaLJhcrSoiJKp16Pr1Xd9wAcAYNiEWcBi781AAAAJXRFWHRkYXRlOmNyZWF0ZQAyMDE4LTEwLTAzVDE1OjIyOjU2LTA0OjAwISeC9gAAACV0RVh0ZGF0ZTptb2RpZnkAMjAxOC0xMC0wM1QxNToyMjo1Ni0wNDowMFB6OkoAAAAASUVORK5CYII="
    $imageBytes = [Convert]::FromBase64String($base64Image)
    $memStream = New-Object IO.MemoryStream($imageBytes, 0, $imageBytes.Length)
    $memStream.Write($imageBytes, 0, $imageBytes.Length);
    $imageGrey = [System.Drawing.Bitmap]::FromStream($memStream, $true)

    $base64Image = "iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsSAAALEgHS3X78AAAAB3RJTUUH4goDDxstKOh3tAAABJVJREFUOMtVlNtvVFUUxn/r3M8w7bSddloYWkpALlqoEBJ9IIQoSUUTEYj6B8CLMSbySOKTvtQYnlDf1ITggwHFiEAIl4CVxPBAAhQKIVwCpXTaTjuXM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TCKpIIhK2yVX3xp0VgRmAK5KGivzTAx2s+JJvqjjq7Or86evToGX10dJSBgYG7Cwvz3SbmlqVmn3hzVYJKiBEYaDUdKYFWFIw5E6dg4046rA3W8MGqPfSlelVHR8eRda+uGx3eOBwKwKVLl2hra+u6ffv2l4Xpwr6F6rxzv/SAp/EkDScAp6UPLdBwohT97gCr21eRttJRNps9svqV1Qc9z5vduXPnywU7NjZGJpNxb9y8+dH83NyBeq021IgC3U8CYmJEwBILx3AxdbPpOs69bHf28Lq1646Uy2Vvx44d/9/YAIVCgVwux4WLFwann03v9TxvTxAEr8VxnBEEXdcrlmVNpNvSJ3t6en4ZGRm5PzU1pfL5/AuOfwFWfAyFldyKLAAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0xMC0wM1QxNToyNzo0NS0wNDowMDpMU7EAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMTAtMDNUMTU6Mjc6NDUtMDQ6MDBLEesNAAAAAElFTkSuQmCC"
    $imageBytes = [Convert]::FromBase64String($base64Image)
    $memStream = New-Object IO.MemoryStream($imageBytes, 0, $imageBytes.Length)
    $memStream.Write($imageBytes, 0, $imageBytes.Length);
    $imageGreen = [System.Drawing.Bitmap]::FromStream($memStream, $true)

    $base64Image = "iVBORw0KGgoAAAANSUhEUgAAABcAAAAXCAYAAADgKtSgAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAABIAAAASABGyWs+AAAAB3RJTUUH4goDDx4G+SN6sQAABZpJREFUSMd1lVtoHNcZx39z27O7o93ZlWKtLoa0dV27riQLW7Ij96GlRtQQFze4xXlRCAaDIHlo+lBBi8FpMZRe5Ba/GNyIgikFU9TEYNclgVygpDFRaBrbsdNE7a5UybrsfS47Mzs7ffARVZv4wP9hvnPm933nP9+co/CIMTExwczMDDdv3jRN0/yiYRifU1XVBNqdTmc1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9tVgkJf3bAjvS2y14SyarAhW5izqQHxzkKyMjUaPReC2ZTEZqrVZjfGzs357nzX/j+HFEdzcVWdU6sAqsAEvbtCzj68CmhDeBo08+SS6fv72xsfFao9FAu3LlCmNjYzSbzaUdvb2TqOpj7y4sEMUxbem1J61oSgtqsuqaVAXYPTrK6enp0Gu1fpJKpV4vl8toAEePHqWvr7+ysblZ3btv36TTaonb9+8TxPH/wO1tCbZUBQZ27eJ7MzOIZPL3y0tLP3VdN5iamnoIv3btGuPj45RKpbtCCHt0fPyr6UxGfPjxx1R9H1/67G77aRpAS9M4cOQIz7/wAinTfGWpVPq+puub129c5/69+w/hADdu3GBycjJeWlpaUFW1uG94ePjgE0/0aEJQc12aQYAHtHUdkcuxd/9+nn7mGY6fONFstVq/+efi4g8URVldWFjg6tWrn32HnjlzhsuXL3P27NndhULh2Ww2e6Ldbn++Ua+nPM9TDMMgm82GadPcaLVaf1lbW3upVCq+3mV2Bb+cnX30Hbp9HDt2jHK5rB48eHCgp6fngGmae3Rdz8RxHHiet1yr1f5WLBbv9fX1tS5dukQcx59i/AdB3aKdFwyReAAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxOC0xMC0wM1QxNTozMDowNi0wNDowMKy8g28AAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTgtMTAtMDNUMTU6MzA6MDYtMDQ6MDDd4TvTAAAAAElFTkSuQmCC"
    $imageBytes = [Convert]::FromBase64String($base64Image)
    $memStream = New-Object IO.MemoryStream($imageBytes, 0, $imageBytes.Length)
    $memStream.Write($imageBytes, 0, $imageBytes.Length);
    $imageRed = [System.Drawing.Bitmap]::FromStream($memStream, $true)

    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $labelTypeEachComputerName = New-Object 'System.Windows.Forms.Label'
    $textbox1 = New-Object 'System.Windows.Forms.TextBox'
    $datagridview1 = New-Object 'System.Windows.Forms.DataGridView'
    $buttonRun = New-Object 'System.Windows.Forms.Button'
    $Online = New-Object 'System.Windows.Forms.DataGridViewImageColumn'
    $Uptime = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $Computer = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $Status = New-Object 'System.Windows.Forms.DataGridViewTextBoxColumn'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    #endregion Generated Form Objects

    #----------------------------------------------
    # Function Get-Uptime
    #----------------------------------------------

    #region - Get-Uptime
    $Uptime1 = {
        function Get-Uptime {
            Params ($ComputerName)
            $global:ComputerName = $row.cells[2].Value
            $os = Get-WmiObject win32_operatingsystem -ComputerName $ComputerName -ErrorAction SilentlyContinue
            if ($os.LastBootUpTime) {
                $uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime)
                #Write-Output ("Last boot: " + $os.ConvertToDateTime($os.LastBootUpTime) )
                Write-Output ("" + $uptime.Days + "d " + $uptime.Hours + "h " + $uptime.Minutes + "m" )
            } else {
                Write-Warning "Unable to connect to $computername"
            }
        }
    }
    #endregion

    #----------------------------------------------
    # User Generated Script
    #----------------------------------------------

    $FormEvent_Load = {
    }

    $textbox1_Validated = {
        if ($textbox1.Text -ne "") {
            $i = $datagridview1.Rows.Add(1)
            $row = $datagridview1.Rows[$i]
            $row.SetValues(@($imageGrey,'',$textbox1.Text,'pending'))
            $textbox1.Text = ''
            $textbox1.Focus()

        if ($row.Cells[2].Value -ne "") {
            if (Test-Connection -ComputerName $row.cells[2].Value -Count 1 -Quiet) {
                $Time = Get-Date

                Start-Job -InitializationScript $Uptime1 -scriptblock {(Get-Uptime -ComputerName $args[0])} -Args $row.cells[2].Value |
                #Start-Job -InitializationScript $Uptime1 -scriptblock {(Get-Uptime)}|
                Wait-Job | Receive-Job
                $row.SetValues(@($imageGrey,($Uptime),$row.Cells[2].Value))
                #Remove-Job -Name CheckSiteUptime
                $Time = Get-date

                $row.Cells[0].Value = $imageGreen
            } else {
                $row.Cells[0].Value = $imageRed}
            }
        }
    }

    $buttonRun_Click = {
        $datagridview1.Rows | ForEach-Object {
            $row = [System.Windows.Forms.DataGridViewRow]$_
            $CommandResult = Invoke-Command -ComputerName $row.cells[2].Value -ArgumentList $row -ScriptBlock{
                Param($row)
                Import-Module Hyper-V
                if ((Get-VM -ErrorAction Stop | Where-Object {$_.name -like '*SR*'} | Get-VMReplication -ErrorAction Stop).Replicationhealth -eq 'critical') {
                    try{
                        Get-VM -ErrorAction Stop | Where-Object {$_.name -like '*SR*'} | Resume-VMReplication -ErrorAction Stop
                        if ((Get-VM -ErrorAction Stop | Where-Object {$_.name -like '*SR*'} | Get-VMReplication -ErrorAction Stop).Replicationhealth -eq 'critical') {
                            throw [System.Exception] "Replicationhealth critical"
                        }
                    } catch {
                        try{
                            Get-VM -ErrorAction Stop | Where-Object {$_.name -like '*SR*'} | Resume-VMReplication -Resynchronize -ErrorAction Stop
                        } catch {
                            return 'FAILED: Resume-VMReplication -Resynchronize'
                            break
                        }
                        return 'Successful: Resume-VMReplication -Resynchronize'
                        break
                    }
                    return 'Successful: Resume-VMReplication'
                } else {
                    return 'Successful: No action replication is NOT critical'
                }
            }
            switch ($CommandResult) {
                "FAILED: Resume-VMReplication -Resynchronize" {
                    $row.Cells | %{$_.Style.BackColor = 'pink'}
                    $Row.Cells[3].Value = $CommandResult
                }
                "Successful: Resume-VMReplication -Resynchronize" {
                    $row.Cells | %{$_.Style.BackColor = 'lightgreen'}
                    $Row.Cells[3].Value = $CommandResult
                }
                "Successful: Resume-VMReplication" {
                    $row.Cells | %{$_.Style.BackColor = 'lightgreen'}
                    $Row.Cells[3].Value = $CommandResult
                }
                "Successful: No action replication is NOT critical" {
                    $row.Cells | %{$_.Style.BackColor = 'lightgreen'}
                    $Row.Cells[3].Value = $CommandResult
                }
            }
        }
        $datagridview1.ReadOnly = $true
    }

    # --End User Generated Script--
    #----------------------------------------------
    #region Generated Events
    #----------------------------------------------

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

    $Form_Cleanup_FormClosed = {
        #Remove all event handlers from the controls
        try {
            $textbox1.remove_Validated($textbox1_Validated)
            $buttonRun.remove_Click($buttonRun_Click)
            $form1.remove_Load($FormEvent_Load)
            $form1.remove_Load($Form_StateCorrection_Load)
            $form1.remove_FormClosed($Form_Cleanup_FormClosed)
        } catch {
            Out-Null <# Prevent PSScriptAnalyzer warning #>
        }
    }
    #endregion Generated Events

    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $form1.SuspendLayout()
    #
    # form1
    #
    $form1.Controls.Add($labelTypeEachComputerName)
    $form1.Controls.Add($textbox1)
    $form1.Controls.Add($datagridview1)
    $form1.Controls.Add($buttonRun)
    $form1.AutoScaleDimensions = '6, 13'
    $form1.AutoScaleMode = 'Font'
    $form1.ClientSize = '625, 600'
    $form1.FormBorderStyle = 'FixedDialog'
    $form1.MaximizeBox = $False
    $form1.MinimizeBox = $False
    $form1.Name = 'form1'
    $form1.StartPosition = 'CenterScreen'
    $form1.Text = 'Replication Check'
    $form1.add_Load($FormEvent_Load)
    #
    # labelTypeEachComputerName
    #
    $labelTypeEachComputerName.Location = '20, 18'
    $labelTypeEachComputerName.Name = 'labelTypeEachComputerName'
    $labelTypeEachComputerName.Size = '240, 49'
    #$labelTypeEachComputerName.TabIndex = 5
    $labelTypeEachComputerName.Text = 'Type each computer name ending with a <tab>  it will be added to the list.  Click run when alll have been added.'
    $labelTypeEachComputerName.UseCompatibleTextRendering = $True
    #
    # textbox1
    #
    $textbox1.CharacterCasing = 'Upper'
    $textbox1.Location = '20, 81'
    $textbox1.Name = 'textbox1'
    $textbox1.Size = '285, 20'
    #$textbox1.TabIndex = 1
    $textbox1.add_Validated($textbox1_Validated)
    #
    # datagridview1
    #
    $datagridview1.AllowUserToAddRows = $False
    $datagridview1.AllowUserToDeleteRows = $False
    $datagridview1.AllowUserToResizeColumns = $True
    $datagridview1.AllowUserToResizeRows = $False
    $datagridview1.ColumnHeadersHeightSizeMode = 'AutoSize'
    [void]$datagridview1.Columns.Add($Online)
    [void]$datagridview1.Columns.Add($Uptime)
    [void]$datagridview1.Columns.Add($Computer)
    [void]$datagridview1.Columns.Add($Status)
    $datagridview1.columns[0].Width = '40'
    $datagridview1.columns[3].Width = '250'
    $datagridview1.Location = '20, 113'
    $datagridview1.Name = 'datagridview1'
    $datagridview1.ReadOnly = $True
    $datagridview1.Size = '583, 470'
    $datagridview1.TabIndex = 3
    $datagridview1.DefaultCellStyle.WrapMode = "True"
    #
    # buttonRun
    #
    $buttonRun.Location = '325, 80'
    $buttonRun.Name = 'buttonRun'
    $buttonRun.Size = '75, 23'
    $buttonRun.TabIndex = 2
    $buttonRun.TabStop = $False
    $buttonRun.Text = 'Run'
    $buttonRun.UseCompatibleTextRendering = $True
    $buttonRun.UseVisualStyleBackColor = $True
    $buttonRun.add_Click($buttonRun_Click)
    #
    # Online
    #
    $Online.HeaderText = 'Online'
    $Online.Name = 'Online'
    $Online.DataPropertyName = 'Online'
    #
    # Uptime
    #
    $Uptime.HeaderText = 'Uptime'
    $Uptime.Name = 'Uptime'
    $Uptime.ReadOnly = $True
    #
    # Computer
    #
    $Computer.HeaderText = 'Server'
    $Computer.Name = 'Server'
    $Computer.ReadOnly = $True
    #
    # Status
    #
    $Status.HeaderText = 'Status'
    $Status.Name = 'Status'
    $Status.ReadOnly = $True

    $form1.ResumeLayout()
    #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($Form_StateCorrection_Load)
    #Clean up the control events
    $form1.add_FormClosed($Form_Cleanup_FormClosed)
    #Show the Form
    return $form1.ShowDialog()

} #End Function

#Call the form
Check-Replication | Out-Null

2 个答案:

答案 0 :(得分:0)

因为PowerShell作业在单独的运行空间中运行,即使将变量声明为$ global:也不会使它在您的作业中可用。相反,您需要在创建作业时将其传递给作业。

例如,您可以按以下方式更改代码:

...
Function Get-Uptime {
    params ($ComputerName)
    $global:ComputerName = $ComputerName
... 
Start-Job -InitializationScript $Uptime1 -scriptblock {(Get-Uptime -ComputerName $args[0])} -Args $row.cells[2].Value

此处描述了类似的问题:https://social.technet.microsoft.com/Forums/ie/en-US/5b369c15-d2ad-4ee8-a1fc-3f0ca8df230a/powershell-jobs-and-global-variables?forum=ITCG

除PS作业外,其他选项的另一种资源通常效率相对较低:https://randombrainworks.com/2018/01/28/powershell-background-jobs-runspace-jobs-thread-jobs/

答案 1 :(得分:0)

要解决的将表单/ GUI与后端代码分离的基本问题,另一种方法是使用运行空间和同步哈希表。该模型的优势在于,您可以有多个线程同时执行工作并主动更新表单,并且您无需传递变量,而只需一个副本并传递引用。

这是一篇很棒的入门文章:https://learn-powershell.net/2012/10/14/powershell-and-wpf-writing-data-to-a-ui-from-a-different-runspace/