I need to be able to right click the selected rows in the DataGrid and delete or re-run the command against the selected rows.
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 #---------------------------------------------- #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' $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 #---------------------------------------------- # User Generated Script #---------------------------------------------- $FormEvent_Load={ } $textbox1_Validated={ if($textbox1.Text){ $i=$datagridview1.Rows.Add(1) $row=$datagridview1.Rows[$i] $row.SetValues(@($textbox1.Text,'pending')) $textbox1.Text='' $textbox1.Focus() } } $buttonRun_Click={ $datagridview1.Rows| ForEach-Object{ $row=[System.Windows.Forms.DataGridViewRow]$_ $CommandResult = Invoke-Command -ComputerName $row.cells[0].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[1].Value = $CommandResult} "Successful: Resume-VMReplication -Resynchronize"{$row.Cells |%{$_.Style.BackColor='lightgreen'} $Row.Cells[1].Value = $CommandResult} "Successful: Resume-VMReplication"{$row.Cells |%{$_.Style.BackColor='lightgreen'} $Row.Cells[1].Value = $CommandResult} "Successful: No action replication is NOT critical"{$row.Cells |%{$_.Style.BackColor='lightgreen'} $Row.Cells[1].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 } } #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 = '425, 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 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($Computer) [void]$datagridview1.Columns.Add($Status) $datagridview1.columns[1].Width = '240' $datagridview1.Location = '20, 113' $datagridview1.Name = 'datagridview1' $datagridview1.ReadOnly = $True $datagridview1.Size = '383, 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) # # 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