I have a question about my code.
I realize a form in powershell and when i click on the button, The code add in the datagridview the variable $Test, if checkbox is checked and $test = YES. After the first action on the button, I change the variable $test to "NO".
But when I click again on the button, the variable $Test is again to "YES" ... and I don't understand why the variable is reset to YES and not saved with the new value "NO"
Example :
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$Test = "YES"
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$MyForm = New-Object System.Windows.Forms.Form
$MyForm.Text="test"
$MyForm.Size = New-Object System.Drawing.Size(1000,350)
$mButton1 = New-Object System.Windows.Forms.Button
$mButton1.Text="test"
$mButton1.Top="155"
$mButton1.Left="30"
$mButton1.Anchor="Left,Top"
$mButton1.Size = New-Object System.Drawing.Size(100,23)
$MyForm.Controls.Add($mButton1)
$mButton1.Add_Click({
If($mCheckBox4.Checked -eq $True -and $Test -eq "YES"){
write-host "IN IF"$Test
$DataGridView.Rows.Add($Test)
}
$Test = "NO"
write-host "Out IF"$Test
})
$mCheckBox4 = New-Object System.Windows.Forms.CheckBox
$mCheckBox4.Text="test"
$mCheckBox4.Top="80"
$mCheckBox4.Left="10"
$mCheckBox4.Anchor="Left,Top"
$mCheckBox4.Size = New-Object System.Drawing.Size(100,23)
$MyForm.Controls.Add($mCheckBox4)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.RowTemplate.DefaultCellStyle.ForeColor = [System.Drawing.Color]::FromArgb(255,0,128,0)
$dataGridView.Name = 'dataGridView'
$dataGridView.DataBindings.DefaultDataSourceUpdateMode = 0
$dataGridView.ReadOnly = $True
$dataGridView.Top="5"
$dataGridView.Left="500"
$dataGridView.Size = New-Object System.Drawing.Size(425,185)
$dataGridView.AllowUserToDeleteRows = $False
$dataGridView.RowHeadersVisible = $False
#$dataGridView.TabIndex = 8
$dataGridView.SelectionMode = 'FullRowSelect'
#$dataGridView.Anchor = 15
$dataGridView.AutoSizeColumnsMode = 16
$dataGridView.AllowUserToAddRows = $False
#$dataGridView.ColumnHeadersHeightSizeMode = 2
#$dataGridView.AllowUserToOrderColumns = $True
#$dataGridView.AllowUserToResizeRows = $False
$dataGridView.ColumnCount = 1
$dataGridView.ColumnHeadersVisible = $true
$dataGridView.Columns[0].Name = "Trigger"
$MyForm.Controls.Add($dataGridView)
$MyForm.ShowDialog()
https://www.hostingpics.net/viewer.php?id=8107302017081612h0222.png
In this pictures, i add a write-host to see the value of my variable $Test, and he switch into "YES/NO" "YES/NO" ...
Thanks for your help
Regards
Related
Good afternoon, dear forum users. Please help me with the next question. I am trying to create a GUI application in Powershell GUI. Since I am a beginner in programming and knowledge, I have not decided to contact the forum for help. The essence of the application is to search by full name in the Active Directory accounts of the required employee account and view the date the password was changed for this account.
I have a ready-made code that works when entering an account login in English. At the same time, I want to implement a search in Russian. The code that I give below searches in Russian, but only in text form displays a list of accounts by name, this text can only be copied. I ask you to tell me how you can program the program so that it searches not in the form of text, full name, but in the form of elements on whose name you can click with the mouse cursor and get the result. I attach a screenshot of the program https://i.stack.imgur.com/GJ5qP.png. Thank you in advance for your help.
$Form.Size = New-Object System.Drawing.Size(460,350) # Mold size
$Form.Text ="Pass info"
$Form.AutoSize = $false
$Form.MaximizeBox = $false # button expand program
$Form.MinimizeBox = $true # minimize program button
$Form.BackColor = "#c08888" # Form color
$Form.ShowIcon = $true # Enable icon (upper left corner) $ true, disable icon
$Form.SizeGripStyle = [System.Windows.Forms.SizeGripStyle]::Hide # Prevent form stretching
#$Form.SizeGripStyle = "Hide"
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D # Prevent form stretching _2
$Form.WindowState = "Normal"
$Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
$Form.Opacity = 1.0 # Form transparency
$Form.TopMost = $false #
Over other windows
########### Function start:
function Info {
$wks=$InputBox.text;
Write-Host $wks
$regex1 = "[^-a-zA-Z0-9_#.!#]+"
$regex2 = "[^-а-яА-Я0-9_#.!#]+"
If($wks -match $regex2) {
$Result1=Get-ADUser -identity $wks -Properties * | select CN -ExpandProperty CN
$outputBox.text=$Result1
$Result2=Get-ADUser -identity $wks -Properties * | select PasswordLastset -ExpandProperty PasswordLastset
$outputBox2.text=$Result2
}
If($wks -match $regex1) {
$wks2 = "$wks*"
Write-Host $wks2
$Result3=Get-AdUser -Filter 'name -Like $wks2' | Select Name -expandproperty Name| Sort Name | fl | out-string
Write-Host $Result3
#$Result3 = ($Result3 -replace ' ','_')
#$Result3 = $Result3 -split '_',2 -join ' '
$ListBox.text = $Result3
}
}
########### End Function.
############################################## Start text fields
#### Group selection of labels 2 and 3$groupBox = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,230)
$groupBox.size = New-Object System.Drawing.Size(280,80)
$groupBox.text = "Info:"
$Form.Controls.Add($groupBox)
$FormLabel1 = New-Object System.Windows.Forms.Label
$FormLabel1.Text = "User AD:"
$FormLabel1.ForeColor = "#3009f1"
$FormLabel1.Font = "Microsoft Sans Serif,8"
$FormLabel1.Location = New-Object System.Drawing.Point(10,10)
$FormLabel1.AutoSize = $true
$Form.Controls.Add($FormLabel1)
$FormLabel2 = New-Object System.Windows.Forms.Label
$FormLabel2.Text = "ФИО:"
$FormLabel2.Location = New-Object System.Drawing.Point(10,25)
$FormLabel2.ForeColor = "#3009f1"
$FormLabel2.Font = "Microsoft Sans Serif,8"
$FormLabel2.AutoSize = $true
#$Form.Controls.Add($FormLabel2) # Метка явл. частью общ. Form
$groupBox.Controls.Add($FormLabel2) # Метка явл. частью groupBox
$FormLabel3 = New-Object System.Windows.Forms.Label
$FormLabel3.Text = "Дата:"
$FormLabel3.Location = New-Object System.Drawing.Point(10,47)
$FormLabel3.ForeColor = "#3009f1"
$FormLabel3.Font = "Microsoft Sans Serif,8"
$FormLabel3.AutoSize = $true
#$Form.Controls.Add($FormLabel3) # Метка явл. частью общ. Form
$groupBox.Controls.Add($FormLabel3) # Метка явл. частью groupBox
############################################ InputBox #########################################
$InputBox = New-Object System.Windows.Forms.TextBox
$InputBox.Location = New-Object System.Drawing.Size(65,5)
$InputBox.Size = New-Object System.Drawing.Size(150,20)
$Form.Controls.Add($InputBox)
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(80,20)
$outputBox.Size = New-Object System.Drawing.Size(180,20)
$outputBox.ReadOnly = $True
$groupBox.Controls.Add($outputBox)
$outputBox2 = New-Object System.Windows.Forms.TextBox
$outputBox2.Location = New-Object System.Drawing.Size(80,42)
$outputBox2.Size = New-Object System.Drawing.Size(180,42)
$outputBox2.ReadOnly = $True
$groupBox.Controls.Add($outputBox2)
############################################## ListBox
$ListBox = New-Object System.Windows.Forms.TextBox
$ListBox.Location = New-Object System.Drawing.Size(65,30)
$ListBox.Size = New-Object System.Drawing.Size(220,200)
$ListBox.MultiLine = $True #declaring the text box as multi-line
$ListBox.AcceptsReturn = $true
$ListBox.ScrollBars = "Vertical"
$ListBox.AcceptsTab = $true
$ListBox.WordWrap = $True
$ListBox.ReadOnly = $True
$Form.Controls.Add($ListBox)
############################################## End ListBox
############################################## search in Russian and in English
$regex1 = "[^-a-zA-Z0-9_#.!#]+"
$regex2 = "[^-а-яА-Я0-9_#.!#]+"
$TestName = "Mouse" # Mouse или мышь
If($TestName -match $regex1){echo "English '$TestName'"}
If($TestName -match $regex2){echo "Русский '$TestName'"}
get-aduser -filter 'name -like "*" ' | select name -expandproperty name
############################################## End search in Russian and in English
################ Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(310,20)
$Button.Size = New-Object System.Drawing.Size(110,80)
$Button.Text = "Загрузить данные"
$Button.BackColor = "#d7f705"
$Button.Add_Click({Info})
$Form.Controls.Add($Button)
################ End Button
################ Binding a button in the program to the Enter key on the keyboard ...
$Form.KeyPreview = $True
$Form.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{
# if enter, perform click
$Button.PerformClick()
}
})
################ End Binding a button
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
This is some example how is works:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$MyForm = New-Object System.Windows.Forms.Form
$MyForm.Text = "USER info Lastlogondate"
$MyForm.Size = New-Object System.Drawing.Size(500, 500)
#user info
$allinfo=$null
#change your domin info
$allinfo=Get-ADUser -filter * -SearchBase "OU=users,DC=domain,DC=local" -properties Name, SamAccountName,Lastlogondate
#select in the column user name to fill $mTextBox1.Text
$Selectuser =
{
$c = $mDataGrid1.CurrentCell.columnindex
$colHeader = $mDataGrid1.columns[$c].name
if ($colHeader -eq "SamAccountName") {
$username = $mDataGrid1.CurrentCell.Value
$mTextBox1.Text = $username
Write-Host $username
}
} #
#usernema textbox--------------------------------------------
$mTextBox1 = New-Object System.Windows.Forms.TextBox
$mTextBox1.Text = ""
$mTextBox1.Top = "20"
$mTextBox1.Left = "26"
$mTextBox1.Anchor = "Left,Top"
$mTextBox1.Size = New-Object System.Drawing.Size(170, 23)
$MyForm.Controls.Add($mTextBox1)
#gridview
$mDataGrid1 = New-Object System.Windows.Forms.DataGridView
$mDataGrid1.Text = "DataGrid1"
$mDataGrid1.Top = "60"
$mDataGrid1.Left = "27"
$mDataGrid1.Anchor = "Left,Top"
$mDataGrid1.AutoSizeColumnsMode = 16
$mDataGrid1.add_CellContentDoubleClick($Selectuser)
$mDataGrid1.Size = New-Object System.Drawing.Size(400, 330)
$MyForm.Controls.Add($mDataGrid1)
#GetAllUsers button------------------------------------------
$mButton1 = New-Object System.Windows.Forms.Button
$mButton1.Text = "Get All Users"
$mButton1.Top = "20"
$mButton1.Left = "200"
$mButton1.Anchor = "Left,Top"
$mButton1.Size = New-Object System.Drawing.Size(100, 23)
$MyForm.Controls.Add($mButton1)
$mButton1.Add_Click( { GetAllusers})
Function GetAllusers {
$mDataGrid1.DataSource = $null
$array = New-Object System.Collections.ArrayList
$result=$allinfo |Select-Object Name, SamAccountName,Lastlogondate|sort -Property Name
$array.Addrange($result)
$mDataGrid1.Datasource = ($array)
$MyForm.Refresh()
}
#exit---------------------------------------------------------
$mButton4 = New-Object System.Windows.Forms.Button
$mButton4.Text = "Exit"
$mButton4.Top = "20"
$mButton4.Left = "320"
$mButton4.Anchor = "Left,Top"
$mButton4.Size = New-Object System.Drawing.Size(120, 23)
$mButton4.Add_Click( {$MyForm.Close()})
$MyForm.Controls.Add($mButton4)
$MyForm.ShowDialog()
I want to prevent my Powershell Form from closing after a submit button is pressed. After the function "search_PC" there is no more code and the form closes. I want to still be able to write stuff in there after the function is finished. I have already tried the pause function and while($true) loops. I would be very happy about an answer.
Import-Module ActiveDirectory
# Assembly for GUI
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
# Create Form
$objForm = New-Object System.Windows.Forms.Form
$objForm.Size = New-Object System.Drawing.Size(400,500)
$objForm.Backcolor="white"
$objForm.StartPosition = "CenterScreen"
$objForm.Text = "Example Software"
$objForm.Icon="C:\Users\...\icon.ico"
# Label
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(80,30)
$objLabel1.Size = New-Object System.Drawing.Size(60,20)
$objLabel1.Text = "UserID:"
$objForm.Controls.Add($objLabel1)
# Textbox
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(160,28)
$objTextBox1.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox1)
# Label
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(80,72)
$objLabel2.Size = New-Object System.Drawing.Size(80,20)
$objLabel2.Text = "PC-Name:"
$objForm.Controls.Add($objLabel2)
# Textbox
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(160,70)
$objTextBox2.Size = New-Object System.Drawing.Size(100,20)
$objForm.Controls.Add($objTextBox2)
# Button for closing
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,220)
$CancelButton.Size = New-Object System.Drawing.Size(163,25)
$CancelButton.Text = "Exit"
$CancelButton.Name = "Exit"
$CancelButton.DialogResult = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
# Checkbox
$objTypeCheckbox1 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox1.Location = New-Object System.Drawing.Size(80,120)
$objTypeCheckbox1.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox1.Text = "Internal Session Support"
$objTypeCheckbox1.TabIndex = 1
$objTypeCheckbox1.Add_Click({$objTypeCheckbox2.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox1)
# Checkbox
$objTypeCheckbox2 = New-Object System.Windows.Forms.Checkbox
$objTypeCheckbox2.Location = New-Object System.Drawing.Size(80,140)
$objTypeCheckbox2.Size = New-Object System.Drawing.Size(500,20)
$objTypeCheckbox2.Text = "Session Support Internal & external"
$objTypeCheckbox2.TabIndex = 2
$objTypeCheckbox2.Add_Click({$objTypeCheckbox1.Checked = $false})
$objForm.Controls.Add($objTypeCheckbox2)
# Button fo checking the User Input
$SubmitButton = New-Object System.Windows.Forms.Button
$SubmitButton.Location = New-Object System.Drawing.Size(100,190)
$SubmitButton.Size = New-Object System.Drawing.Size(163,25)
$SubmitButton.Text = "Submit"
$SubmitButton.Name = "Submit"
$SubmitButton.DialogResult = "OK"
$objForm.AcceptButton = $SubmitButton
$SubmitButton.Add_Click({
$User = $objTextBox1.Text
$PC = $objTextBox2.Text
# Check if all User Inputs are filled out
if ( !($User.trim()) -or !($PC.trim()) ) {
[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
if($objTypeCheckbox1.checked -eq $true) {$Software = "Internal"}
elseif($objTypeCheckbox2.checked -eq $true) {$Software = "Internal&External"}
else {[void] [Windows.Forms.MessageBox]::Show("Please fill out every value!", "Error", [Windows.Forms.MessageBoxButtons]::ok, [Windows.Forms.MessageBoxIcon]::Warning)}
search_user
})
$objForm.Controls.Add($SubmitButton)
$statusBar1 = New-Object System.Windows.Forms.StatusBar
$objForm.controls.add($statusBar1)
function createFile
{
$Date = Get-Date -Format d.M.yyyy
$Time = (Get-Date).ToString(„HH:mm:ss“)
$Time2 = (Get-Date).ToString(„HH-mm-ss“)
$Date2 = Get-Date -Format yyyy-M-d;
$FileName = $Time2 + " " + $Date2
$wrapper = New-Object PSObject -Property #{ 1 = $Date; 2 = $Time; 3 = $User; 4 = $PC; 5 = $Software }
Export-Csv -InputObject $wrapper -Path C:\Users\...\$FileName.csv -NoTypeInformation
[void] [Windows.Forms.MessageBox]::Show("Successful!")
}
function search_user
{
if (#(Get-ADUser -Filter {SamAccountName -eq $User}).Count -eq 0) {
$objTextBox1.BackColor = "Red";
$statusBar1.Text = "Couldn't find user!"
$objForm.Dispose()
}
else{$objTextBox1.BackColor = "Green"
search_PC}
}
function search_PC
{
$Client = $PC
if (#(Get-ADComputer -Filter {DNSHostName -eq $Client}).Count -eq 0) {
$objTextBox2.BackColor = "Red";
$statusBar1.Text = "Couldn't find PC!"
$objForm.Dispose()
}
else{$objTextBox2.BackColor = "Green"
createFile}
}
$objForm.ShowDialog() #Showing Form and elements
Screenshot of the form:
This line is your problem:
$SubmitButton.DialogResult = "OK"
It should be:
$SubmitButton.DialogResult = [System.Windows.Forms.DialogResult]::None
Or just be removed.
DialogResult
I want to show the picturebox, but it does not show once I use form.show(). But if I change to Form.showdialog, the picturebox will show but, the process can not continue until I close the GUI. The picture box show but it does not moving, it stuck like picture.
Function Handling
{
$Form.Close()
$Form.Dispose()
$Form = New-Object system.Windows.Forms.Form
$Form.ControlBox = $true
$Form.BackColor = "#d0021b"
$Form.WindowState = "Maximized"
$Form.TopMost = $false
[void]$Form.Show()
# Message Box
[System.Windows.MessageBox]::Show("OK", "[Error]", "0", "Error")
$ExitCode = "1"
if($ExitCode -ne "107A")
{
$Form.Close()
$Form.Dispose()
Exit
}
else{
$Form.Close()
$Form.Dispose()
Exit
}
}
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$form.BackgroundImageLayout = 'Center'
$Form.WindowState = 'Maximized'
$Form.BackColor = "#ffffff"
$file2 = Get-ChildItem -Path "D:\3.png"
$cover = [Drawing.Image]::FromFile($file2)
$form.BackgroundImage = $img2
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'D:\6.gif')
$img = [System.Drawing.Image]::Fromfile($file)
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Image = $img
$pictureBox.SizeMode = "Autosize"
$pictureBox.Anchor = "Bottom, left"
$Form.controls.add($pictureBox)
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file3 = (get-item 'D:\6.gif')
$img3 = [System.Drawing.Image]::Fromfile($file3)
$pictureBox2 = new-object Windows.Forms.PictureBox
$pictureBox2.Image = $img3
$pictureBox2.SizeMode = "Autosize"
$pictureBox2.Anchor = "Bottom, right"
$Form.controls.add($pictureBox2)
$form.Show()
Write-Host "next process"
####
# some process
###
Start-Sleep -s 2
Handling
Anyone can give me idea please. Really appreciate for your help. Thank you.
Can you try like this? Put your background operation in place of Start-Sleep -s 2 and the gif file still show moving.
$Form = New-Object system.Windows.Forms.Form
$Form.Location= New-Object System.Drawing.Size(100,100)
$Form.Size= New-Object System.Drawing.Size(550,170)
$Form.StartPosition = "Manual"
$Form.Visible=$false
$Form.Enabled = $true
$Form.Add_Shown({$Form.Activate()})
[reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
$file = (get-item 'D:\6.gif')
$img = [System.Drawing.Image]::Fromfile($file);
[System.Windows.Forms.Application]::EnableVisualStyles();
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Size(0,1)
$pictureBox.Size = New-Object System.Drawing.Size($img.Width,$img.Height)
$pictureBox.Image = $img
$Form.controls.add($pictureBox)
$WaitForm.Topmost = $True
$rs = [Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace()
$rs.Open()
$rs.SessionStateProxy.SetVariable("Form", $Form)
$data = [hashtable]::Synchronized(#{text=""})
$rs.SessionStateProxy.SetVariable("data", $data)
$p = $rs.CreatePipeline({ [void] $Form.ShowDialog()})
$p.Input.Close()
$p.InvokeAsync()
## Enter the rest of your script here while you want the form to display
Start-Sleep -s 2
$WaitForm.close()
I have observed a difference in behavior between PowerShell 2.0 code and 3.0 code but can't seem to fix it. I am hoping I can get some help. Please see code below.
If you run this in V2 (please change ServerA and ServerB to a server on your network) you will see after selecting and clicking the button, write-host shows them as seperate names. In version 3 the listbox populates it as 1.
$Servers = #()
[array]$ServerstoPing = ("ServerA","ServerB")
# Generate the form
Add-Type -AssemblyName System.Windows.Forms
$Form1 = New-Object system.Windows.Forms.Form
$Form1.Text = "Ping Some Servers"
$Font = New-Object System.Drawing.Font("Calibri",18,[System.Drawing.FontStyle]::regular)
$Fontboxes = New-Object System.Drawing.Font("Lucida Sans",8,[System.Drawing.FontStyle]::regular)
$Fontdate = New-Object System.Drawing.Font("Lucida Sans",11,[System.Drawing.FontStyle]::bold)
$FontProgress = New-Object System.Drawing.Font("Lucida Sans",11,[System.Drawing.FontStyle]::bold)
$Form1.Font = $Font
$Serverlistbox = New-Object System.Windows.Forms.Listbox
$Serverlistbox.Location = New-Object System.Drawing.Size(11,137)
$Serverlistbox.Size = New-Object System.Drawing.Size(200,170)
$Serverlistbox.SelectionMode = "MultiExtended"
$Serverlistbox.font = $Fontboxes
foreach($item in $ServerstoPing){
[void] $Serverlistbox.Items.Add("$item")
}
$Form1.Controls.Add($Serverlistbox)
$SubmitButton = New-Object System.Windows.Forms.button
$SubmitButton.text = "Ping Servers"
$SubmitButton.Location = New-Object System.Drawing.Size(171,305)
$SubmitButton.AutoSize = $True
$Form1.Controls.Add($SubmitButton)
$Form1.StartPosition = "CenterScreen"
$OutputPing = New-Object System.Windows.Forms.TextBox
$OutputPing.MultiLine = $True
$OutputPing.ScrollBars = "Vertical"
$OutputPing.text = "Server ping status will be displayed here once done'."
$OutputPing.font = $Fontboxes
$OutputPing.Location = New-Object System.Drawing.Size(226,40)
$OutputPing.Size = New-Object System.Drawing.Size(200,255)
$Form1.Controls.Add($OutputPing)
$SubmitButton.add_click({
#foreach ($objItem in $ServerstoPing)
foreach ($objItem in $Serverlistbox.SelectedItems)
{
$Servers += $objItem
}
write-host $ServerstoPing
Foreach($server in $servers)
{
ping -n 1 -w 1000 $server | out-null
if($? -eq $true){
write-host ("$server ping success")
}
else
{
write-host ("$server did not respond to ping, not extracting events")
}
}
})
$Form1.ShowDialog()
I'm trying to create a powershell form that will get all processes into a datagridview (which works OK so far). I'm then adding an additional column infront of the datasource with each row as a checkbox.
I'd then like to press a button on my form which starts the Function called Do-Grid, and it is meant to go through and see which checkboxes in this first column have been checked.
And this is where I get stuck. I'm unable to figure out how to loop through each row/cell only in this column and see which checkbox has been ticked (or have the value of true).
Thank you.
Function GenerateForm {
[Void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ButtonUpdateGrid = New-Object Windows.Forms.Button
$ButtonUpdateGrid.Location = New-Object Drawing.Point 7,15
$ButtonUpdateGrid.size = New-Object Drawing.Point 90,23
$ButtonUpdateGrid.Text = "&Get Packages"
$ButtonUpdateGrid.add_click({Get-info})
$Button1 = New-Object Windows.Forms.Button
$Button1.Location = New-Object Drawing.Point 200,15
$Button1.size = New-Object Drawing.Point 90,23
$Button1.Text = "columns"
$button1.add_Click($button1_OnClick)
$ButtonStartExport = New-Object Windows.Forms.Button
$ButtonStartExport.Location = New-Object Drawing.Point 100,15
$ButtonStartExport.size = New-Object Drawing.Point 90,23
$ButtonStartExport.Text = "rows"
$ButtonStartExport.add_click({Do-Grid})
$dataGridView1 = New-Object System.Windows.Forms.DataGridView
$dataGridView1.Location = New-Object Drawing.Point 7,40
$dataGridView1.size = New-Object Drawing.Point 1000,700
$dataGridView1.MultiSelect = $false
$dataGridView1.ColumnHeadersVisible = $true
$dataGridView1.RowHeadersVisible = $false
$form = New-Object Windows.Forms.Form
$form.text = "Package Exporter v0.1"
$form.Size = New-Object Drawing.Point 1024, 768
$form.topmost = 1
$form.Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$form.Controls.Add($dataGridView1)
$form.controls.add($Button1)
$form.controls.add($ButtonStartExport)
$form.controls.add($ButtonUpdateGrid)
#$form.add_Load($OnLoadForm)
$form.ShowDialog()
}
Function Get-info{
If($datagridview1.columncount -gt 0){
$dataGridview1.DataSource = $null
$DataGridView1.Columns.RemoveAt(0)
}
$Column1 = New-Object System.Windows.Forms.DataGridViewCheckBoxColumn
$Column1.width = 30
$Column1.name = "Exp"
$DataGridView1.Columns.Add($Column1)
$array = New-Object System.Collections.ArrayList
$Script:procInfo = get-process | Select-Object name, company, description, product, id, vm, fileversion
$array.AddRange($procInfo)
$dataGridview1.DataSource = $array
$form.refresh()
}
Function Do-Grid{
#?????????????????????????????????? not really sure what to do here
for($i=0;$i -le $datagridview1.Rows.Count;$i++){
write-host $datagridview1.Rows[$i].value
}
}
Generate Form
Function Do-Grid{
for($i=0;$i -lt $datagridview1.RowCount;$i++){
if($datagridview1.Rows[$i].Cells['exp'].Value -eq $true)
{
write-host "cell #$i is checked"
#uncheck it
#$datagridview1.Rows[$i].Cells['exp'].Value=$false
}
else
{
#check it
#$datagridview1.Rows[$i].Cells['exp'].Value=$true
write-host "cell #$i is not-checked"
}
}
}