Powershell linking datagridview to an array - powershell

I am currently trying to load a json file that is a DB into a datagridview within a form.
I currnetly have the JSON file loading into an array. What I cant figure out is how to get the array to load into the datagridview so I can display the data of what is in said array from within the form.
I dont think im too far off any help would be much appreciated.
#JSON DB Front end
$jsonDB = Get-Content 'C:\Support\HardwareCollection.json' | Out-String | ConvertFrom-Json
#jsonDb has $jsonDB.Date and $jsonDB.username
$Form = New-Object system.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(1050,425)
$form.MaximizeBox = $false
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "Hardware Collection"
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(500,200)
$dataGridView.Location = new-object system.drawing.size(1,150)
$dataGridView.ColumnCount = 2
$dataGridView.Columns[0].Name = "Date"
$dataGridView.Columns[1].Name = "Username"
$form.Controls.Add($dataGridView)
$datagridview.DataSource = $jsonDB
$Form.ShowDialog()
Many thanks!!!

You're right - you were close - the dataGridView likes .NET types for its data sources, not Powershell objects. So, just create an ArrayList from your Powershell collection, and it works:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Data
Add-Type -AssemblyName System.Collections
$jsonDB = Get-Content 'C:\Support\HardwareCollection.json' | Out-String | ConvertFrom-Json
$tableData = New-Object System.Collections.ArrayList
$tableData.AddRange($jsonDB)
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(1050, 425)
$Form.MaximizeBox = $False
$Form.StartPosition = "CenterScreen"
$Form.FormBorderStyle = 'Fixed3D'
$Form.Text = "Hardware Collection"
$dataGridView = New-Object System.Windows.Forms.DataGridView -Property #{
Size = New-Object System.Drawing.Size(500, 200)
Location = New-Object System.Drawing.Size(1, 150)
ColumnHeadersVisible = $True
DataSource = $tableData
}
$Form.Controls.Add($dataGridView)
$Form.ShowDialog()

Related

Powershell GUI tabbed layout script placement

I wrote six small gui scripts that I'd like to place in a single file, for the sake of converting said file into rxecutable with ps2exe.
I've found a script ,here on stack that is perfect for what I want. Unfortunatelly I cann't find any info on script placement within tabs and MS documentation leads me to ISE tabs, which is not helpfull.
Say I'd like to place this
Add-Type -AssemblyName PresentationFramework
$button_click = {
$folder = $textBox1.Text;
$pytanie = [System.Windows.MessageBox]::Show('Czy chcesz usunac folder?', '', '4');
If($pytanie -eq 'Yes')
{Remove-Item –path $folder –recurse -Force};
$test = Test-Path $folder;
if ($test -eq $false){[System.Windows.MessageBox]::Show('Folder Usuniety', '', '0')}}
$label2 = New-Object System.Windows.Forms.Label
$label2.AutoSize = $True
$label2.Text = ("Scieżka")
$label2.Location = New-Object System.Drawing.Point (10,30)
$label2.Size = New-Object System.Drawing.Size (25,70)
$label2.Font = [System.Drawing.Font]::new("Arial", 10, [System.Drawing.FontStyle]::Bold)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Mapowanie' ### Text to be displayed in the title
$form.Size = New-Object System.Drawing.Size(240,200) ### Size of the window
$form.StartPosition = 'Manual'
$form.Location = '10,10'
$form.Topmost = $true ### Optional - Opens on top of other windows
$form.Controls.AddRange(#($textBox1,$button, $label2))
$form.ShowDialog()
within a tab. How to do it?
I think that it the
$Tab1.Controls.Add($button)
You are looking for.
For example
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$ApplicationForm = New-Object System.Windows.Forms.Form
$ApplicationForm.StartPosition = "CenterScreen"
$ApplicationForm.Topmost = $false
$ApplicationForm.Size = "800,600"
$FormTabControl = New-object System.Windows.Forms.TabControl
$FormTabControl.Size = "755,475"
$FormTabControl.Location = "25,75"
$ApplicationForm.Controls.Add($FormTabControl)
$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab1.UseVisualStyleBackColor = $True
$Tab1.Name = "Tab1"
$Tab1.Text = "Tab1”
$FormTabControl.Controls.Add($Tab1)
$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,70) ### Location of the text box
$textBox1.Size = New-Object System.Drawing.Size(200,50) ### Size of the text box
$textBox1.Multiline = $false ### Allows multiple lines of data
$textBox1.Font = New-Object System.Drawing.Font("Consolas",10,[System.Drawing.FontStyle]::Regular)
$textBox1.ReadOnly=$false
$Tab1.Controls.Add($textBox1)
$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0
$Tab2.UseVisualStyleBackColor = $True
$Tab2.Name = "Tab2"
$Tab2.Text = "Tab2”
$FormTabControl.Controls.Add($Tab2)
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(10,120)
$button.Size = New-Object System.Drawing.Size (200,30)
$button.Text = "Usun Folder"
$button.Add_Click($button_click)
$Tab2.Controls.Add($button)
# Initlize the form
$ApplicationForm.Add_Shown({$ApplicationForm.Activate()})
[void] $ApplicationForm.ShowDialog()

Powershell Script to get software Version of Adobe Connect & Zoom with GUI Error

Working on Powershell script to get adobe connect and Zoom Version in GUI. and getting hyperlink displayed. Any help will be highly appreciated.
Issue 1 - Both software version is not visible. Only one getting displayed either zoom or Adobe in output
Issue 2 - Unable to get Hyperlink for both row. Getting only for one at a time.
$ErrorActionPreference = "SilentlyContinue"
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing
$font = New-Object System.Drawing.Font("Arial", 9)
$form = New-Object System.Windows.Forms.Form
$form.Text = 'PLT Readiness'
$form.Size = New-Object System.Drawing.Size(325,225)
$form.StartPosition = 'CenterScreen'
$form.Icon = 'C:\Users\C899414\Desktop.ico'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(120,150)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.TextAlign = 'MiddleCenter'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$LinkLabel = New-Object System.Windows.Forms.LinkLabel
$LinkLabel.Location = New-Object System.Drawing.Point(81,110)
$LinkLabel.Size = New-Object System.Drawing.Size(280,20)
$LinkLabel.Font = $font
$LinkLabel.LinkColor = "BLUE"
$LinkLabel.ActiveLinkColor = "RED"
$LinkLabel.Text = "Ticketsystem"
$LinkLabel.add_Click({[system.Diagnostics.Process]::start("https://service-now.com/nav_to.do?uri=%2Fincident.do%3Fsys_id%3D-1%26sysparm_query%3Dactive%3Dtrue%26sysparm_stack%3Dincident_list.do%3Fsysparm_query%3Dactive%3Dtrue")})
$Form.Controls.Add($LinkLabel)
$software = New-Object System.Windows.Forms.Label $software.Location = New-Object System.Drawing.Point(10,50) $software.Size = New-Object System.Drawing.Size(280,20) $software.Font = $font $software.Text = "Adobe Connect: $((Get-WMIObject Win32_Product | Where-Object {$_.Name -like 'Adobe Connect Application*'} | Select-Object Version))" $software.Text = "Zoom: $((Get-WMIObject Win32_Product | Where-Object {$_.Name -like 'Zoom*'} | Select-Object Version))" $form.Controls.Add($software)
$help = New-Object System.Windows.Forms.Label
$help.Location = New-Object System.Drawing.Point(10,110)
$help.Font = $font
$help.Size = New-Object System.Drawing.Size(280,20)
$help.Text = "Help:"
$form.Controls.Add($help)
$help2 = New-Object System.Windows.Forms.Label
$help2.Location = New-Object System.Drawing.Point(10,130)
$help2.Font = $font
$help2.Size = New-Object System.Drawing.Size(280,20)
$help2.Text = "Update From Software Center:"
$form.Controls.Add($help)
$form.Topmst = $true
$form.ShowDialog()

How to create CheckBox automatically using PowerShell?

I want to create checkbox automatically consider to the total a file inside a folder I selected.
In my script, the checkbox always create one checkbox, but my file more than one. And the checkbox always appear before I select the folder from listbox. Please give me advice if anyone know about this, Thank you so much.
This is the result look like
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "Select a Computer"
$objForm.Size = New-Object System.Drawing.Size(500,700)
$objForm.StartPosition = "CenterScreen"
$objForm.Text = "Test"
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,70)
$objListBox.Size = New-Object System.Drawing.Size(260,30)
$objListBox.Height = 250
$objListBox.add_SelectedIndexChanged($SelectedFile)
$objForm.Controls.Add($objListBox)
$objChktBox = New-Object System.Windows.Forms.CheckBox
$objChktBox.Location = New-Object System.Drawing.Size(10,400)
$objChktBox.Size = New-Object System.Drawing.Size(400,3000)
$objChktBox.Height = 200
$objForm.Controls.Add($objChktBox)
# Populate list.
#(Get-ChildItem -Directory ".\").Name | ForEach-Object {[void] $objListBox.Items.Add($_)}
$SelectedFile=
{
$objChktBox.Text = (Get-ChildItem $objListbox.SelectedItem)
}
$objForm.ShowDialog()

How to use maximize groupbox using GUI in PowerShell?

I would like to create a group box in my GUI form. I use windowsState maximized for my form. And I want to use the group box, and I need to make the group box also maximized but combine with margin and padding. So the size and position of the group box will not change even the resolution screen change.
I tried this, but it does not work.
Anyone can help me. Thank you.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.text = "Form"
$Form.TopMost = $false
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $false
$Form.startposition = "centerscreen"
$Form.WindowState = 'Maximized'
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.text = "Group Box"
$Groupbox1.location = New-Object System.Drawing.Point(8,13)
$Groupbox1.Padding = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
$Groupbox1.Margin = 2,2,2,2
$Form.controls.AddRange(#($Groupbox1))
[void]$Form.ShowDialog()
You would have to create a panel first of 100% width and height. Then put the groupbox inside the panel, this should work:
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.text = "Form"
$Form.TopMost = $false
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $false
$Form.startposition = "centerscreen"
$Form.WindowState = 'Maximized'
$Panel = New-Object System.Windows.Forms.TableLayoutPanel
$panel.Dock = "Fill"
$panel.ColumnCount = 1
$panel.RowCount = 1
$panel.CellBorderStyle = "single"
$panel.ColumnStyles.Add((new-object System.Windows.Forms.ColumnStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$panel.RowStyles.Add((new-object System.Windows.Forms.RowStyle([System.Windows.Forms.SizeType]::Percent, 100)))
$Groupbox1 = New-Object system.Windows.Forms.Groupbox
$Groupbox1.text = "Group Box"
$Groupbox1.location = New-Object System.Drawing.Point(8,13)
$Groupbox1.Padding = New-Object -TypeName System.Windows.Forms.Padding -ArgumentList (0,5,5,0)
$Groupbox1.Dock = "fill"
$form.controls.add($Panel)
$panel.controls.AddRange(#($Groupbox1))
[void]$Form.ShowDialog()

Outputting to Convertto-html Cmdlet Powershell

Evening everyone,
I've spent a while having a play on google learning how to create a basic form with powershell. Currently I'm having a problem coming to grips with how to get this basic form to output to a html file at the click of a button I have managed to get it to work with the services ie -
Get-Service | ConvertTo-Html | Out-File "Location"
However i'm wanting to input data on my form and then once the submit button is pressed, it puts the data into a table and then outputs it to the html file.
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "BasicForm"
$Form.Width = 800
$Form.AutoScroll = $True
#VRN label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,75)
$objLabel.Text = "Num:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#VRN Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,75)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,75)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,75)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Title by Label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(250,125)
$objLabel.Text = "Title:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Title by Dropdownbox
$objtitlelist = New-Object System.Windows.Forms.ComboBox
$objtitlelist.Location = New-Object System.Drawing.Size(350,125)
$objtitlelist.Size = New-Object System.Drawing.Size(125,50)
$objtitlelist.Items.Add("Mr")
$objtitlelist.Items.Add("Mrs")
$objtitlelist.Items.Add("Ms")
$Form.Controls.Add($objtitlelist)
#Title by Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(500,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Date label
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(50,125)
$objLabel.Text = "Date:"
$objLabel.AutoSize = $True
$Form.Controls.Add($objLabel)
#Date Textbox
$objtextbox = New-Object System.Windows.Forms.TextBox
$objtextbox.Location = New-Object System.Drawing.Size(100,125)
$objtextbox.Size = New-Object System.Drawing.Size(100,150)
$Form.Controls.Add($objtextbox)
#Add Button docs
$Buttondocs = New-Object System.Windows.Forms.Button
$Buttondocs.Location = New-Object System.Drawing.Size(175,35)
$Buttondocs.Size = New-Object System.Drawing.Size(120,23)
$Buttondocs.Text = "Documentation"
$Form.Controls.Add($Buttondocs)
#Add Button docs event
$Buttondocs.Add_Click({Button_Click})
Function Button_Click()
{
Invoke-Item "C:\Users\Michael\Documents\PowershellProjects\EIMI\*.txt"
}
#Add Button html
$Buttonhtml = New-Object System.Windows.Forms.Button
$Buttonhtml.Location = New-Object System.Drawing.Size(35,35)
$Buttonhtml.Size = New-Object System.Drawing.Size(120,23)
$Buttonhtml.Text = "Submit Form"
$Form.Controls.Add($Buttonhtml)
#Add Button html event
$Buttonhtml.Add_Click({Button_Click})
Function Button_Click()
{
ConvertTo-Html | Out-File "C:\Users\Michael\Documents\PowershellProjects\EIMI\index.html"
}
#Add Question 1
$Question1 = New-Object System.Windows.Forms.Label
$Question1.Location = New-Object System.Drawing.Size(50,175)
$Question1.Size = New-Object System.Drawing.Size(500,50)
$Question1.Text = "Random Form Question."
$Form.Controls.Add($Question1)
$Form.ShowDialog()
The documentation invoke-item section is another problem altogether but I beleive I have found a solution to that one. Unless someone has a simple method. Apologise if this is just trivial nonsense but I'm learning from playing with things. Any suggestions on good authors would be greatly appreciated, or even tips on how to improve this in general.
Thanks Mike.