Run GUI powershell script by right clicking on a file - powershell

I have built a powershell script using the GUI .net framework that provides the user with a graphical interface to add alternate data streams (ADS) to files on a NTFS file system.
Below is the code I wrote for the powershell script:
<#
This script is a GUI featured way to add extended attributes to files
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,600'
$Form.text = "Add Extended Attributes"
$Form.TopMost = $false
# Add Extended Attributes Label
$mainLabel = New-Object system.Windows.Forms.Label
$mainLabel.text = "Add Extended Attributes"
$mainLabel.AutoSize = $true
$mainLabel.width = 25
$mainLabel.height = 10
$mainLabel.location = New-Object System.Drawing.Point(180,10)
$mainLabel.Font = 'Microsoft Sans Serif,18'
# text box for entering file path
$filePath = New-Object system.Windows.Forms.TextBox
$filePath.multiline = $false
$filePath.width = 300
$filePath.height = 20
$filePath.location = New-Object System.Drawing.Point(200,80)
$filePath.Font = 'Microsoft Sans Serif,10'
# label for the file path text box "File Path: "
$FilePathLabel = New-Object system.Windows.Forms.Label
$FilePathLabel.text = "File Path: "
$FilePathLabel.AutoSize = $true
$FilePathLabel.width = 25
$FilePathLabel.height = 10
$FilePathLabel.location = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font = 'Microsoft Sans Serif,10'
# Attributes Label
$idLabel = New-Object system.Windows.Forms.Label
$idLabel.text = "Attributes"
$idLabel.AutoSize = $true
$idLabel.width = 25
$idLabel.height = 10
$idLabel.location = New-Object System.Drawing.Point(80,150)
$idLabel.Font = 'Microsoft Sans Serif,12'
# Values Label
$valueLabel = New-Object system.Windows.Forms.Label
$valueLabel.text = "Value"
$valueLabel.AutoSize = $true
$valueLabel.width = 25
$valueLabel.height = 10
$valueLabel.location = New-Object System.Drawing.Point(300,150)
$valueLabel.Font = 'Microsoft Sans Serif,12'
# Checkbox for ID attribute
$fileId = New-Object System.Windows.Forms.CheckBox
$fileId.text = "Id"
$fileId.AutoSize = $true
$fileId.width = 104
$fileId.height = 20
$fileId.location = New-Object System.Drawing.Point(100,200)
$fileId.Font = 'Microsoft Sans Serif,10'
# Label for the ID checkbox
$idValue = New-Object system.Windows.Forms.TextBox
$idValue.multiline = $false
$idValue.width = 300
$idValue.height = 20
$idValue.location = New-Object System.Drawing.Point(202,200)
$idValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Description attribute
$description = New-Object System.Windows.Forms.CheckBox
$description.text = "Description"
$description.AutoSize = $true
$description.width = 104
$description.height = 20
$description.location = New-Object System.Drawing.Point(100,250)
$description.Font = 'Microsoft Sans Serif,10'
# Label for the Description checkbox
$descriptionValue = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline = $false
$descriptionValue.width = 300
$descriptionValue.height = 20
$descriptionValue.location = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Type attribute
$type = New-Object System.Windows.Forms.CheckBox
$type.text = "Type"
$type.AutoSize = $true
$type.width = 104
$type.height = 20
$type.location = New-Object System.Drawing.Point(100,300)
$type.Font = 'Microsoft Sans Serif,10'
# Label for the type checkbox
$typeValue = New-Object system.Windows.Forms.TextBox
$typeValue.multiline = $false
$typeValue.width = 300
$typeValue.height = 20
$typeValue.location = New-Object System.Drawing.Point(202,300)
$typeValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for silo attribute
$silo = New-Object System.Windows.Forms.CheckBox
$silo.text = "Silo"
$silo.AutoSize = $true
$silo.width = 104
$silo.height = 20
$silo.location = New-Object System.Drawing.Point(100,350)
$silo.Font = 'Microsoft Sans Serif,10'
# Label for the silo checkbox
$siloValue = New-Object system.Windows.Forms.TextBox
$siloValue.multiline = $false
$siloValue.width = 300
$siloValue.height = 20
$siloValue.location = New-Object System.Drawing.Point(202,350)
$siloValue.Font = 'Microsoft Sans Serif,10'
# submitt button
$button = New-Object System.Windows.Forms.Button
$button.text = "Submit"
$button.AutoSize = $true
$button.location = New-Object System.Drawing.Point(250,500)
$button.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))
#region gui events {
function SubmitForm(){
if($fileId.checked -eq $true){
sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
}
if($description.checked -eq $true){
sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
}
if($type.checked -eq $true){
sc -path $filePath.Text -stream $type.text -value $typeValue.text
}
if($silo.checked -eq $true){
sc -path $filePath.Text -stream $silo.text -value $siloValue.text
}
[System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event
$Button.Add_Click({SubmitForm})
#endregion events }
#endregion GUI }
# logic here
[void]$Form.ShowDialog()
Currently the user would have to actually run the powershell script from the root folder and then add the file path to the text input the GUI, along with the rest of the extended attributes. An example of what I currently have is below:
I would like the user to be able to right click on any file and have the form come up with the path of the file that was right clicked in windows explorer, instead of the path being manually entered by the individual making the updates to the alternate data streams. Something similar as how you would extract a file using zip7 (example below).
Can someone tell me if this is even possible? Should I be trying to tackle this problem in another language than using powershell?

You can do all this with Powershell.
First you want to create a script from your code and make input parameter for chosen folder. Like so:
param($FileName)
<#
This script is a GUI featured way to add extended attributes to files
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
#region begin GUI{
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '600,600'
$Form.text = "Add Extended Attributes"
$Form.TopMost = $false
# Add Extended Attributes Label
$mainLabel = New-Object system.Windows.Forms.Label
$mainLabel.text = "Add Extended Attributes"
$mainLabel.AutoSize = $true
$mainLabel.width = 25
$mainLabel.height = 10
$mainLabel.location = New-Object System.Drawing.Point(180,10)
$mainLabel.Font = 'Microsoft Sans Serif,18'
# text box for entering file path
$filePath = New-Object system.Windows.Forms.TextBox
$filePath.multiline = $false
$filePath.width = 300
$filePath.height = 20
$filePath.location = New-Object System.Drawing.Point(200,80)
$filePath.Font = 'Microsoft Sans Serif,10'
$filePath.Text = $FileName
# label for the file path text box "File Path: "
$FilePathLabel = New-Object system.Windows.Forms.Label
$FilePathLabel.text = "File Path: "
$FilePathLabel.AutoSize = $true
$FilePathLabel.width = 25
$FilePathLabel.height = 10
$FilePathLabel.location = New-Object System.Drawing.Point(80,85)
$FilePathLabel.Font = 'Microsoft Sans Serif,10'
# Attributes Label
$idLabel = New-Object system.Windows.Forms.Label
$idLabel.text = "Attributes"
$idLabel.AutoSize = $true
$idLabel.width = 25
$idLabel.height = 10
$idLabel.location = New-Object System.Drawing.Point(80,150)
$idLabel.Font = 'Microsoft Sans Serif,12'
# Values Label
$valueLabel = New-Object system.Windows.Forms.Label
$valueLabel.text = "Value"
$valueLabel.AutoSize = $true
$valueLabel.width = 25
$valueLabel.height = 10
$valueLabel.location = New-Object System.Drawing.Point(300,150)
$valueLabel.Font = 'Microsoft Sans Serif,12'
# Checkbox for ID attribute
$fileId = New-Object System.Windows.Forms.CheckBox
$fileId.text = "Id"
$fileId.AutoSize = $true
$fileId.width = 104
$fileId.height = 20
$fileId.location = New-Object System.Drawing.Point(100,200)
$fileId.Font = 'Microsoft Sans Serif,10'
# Label for the ID checkbox
$idValue = New-Object system.Windows.Forms.TextBox
$idValue.multiline = $false
$idValue.width = 300
$idValue.height = 20
$idValue.location = New-Object System.Drawing.Point(202,200)
$idValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Description attribute
$description = New-Object System.Windows.Forms.CheckBox
$description.text = "Description"
$description.AutoSize = $true
$description.width = 104
$description.height = 20
$description.location = New-Object System.Drawing.Point(100,250)
$description.Font = 'Microsoft Sans Serif,10'
# Label for the Description checkbox
$descriptionValue = New-Object system.Windows.Forms.TextBox
$descriptionValue.multiline = $false
$descriptionValue.width = 300
$descriptionValue.height = 20
$descriptionValue.location = New-Object System.Drawing.Point(202,250)
$descriptionValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for Type attribute
$type = New-Object System.Windows.Forms.CheckBox
$type.text = "Type"
$type.AutoSize = $true
$type.width = 104
$type.height = 20
$type.location = New-Object System.Drawing.Point(100,300)
$type.Font = 'Microsoft Sans Serif,10'
# Label for the type checkbox
$typeValue = New-Object system.Windows.Forms.TextBox
$typeValue.multiline = $false
$typeValue.width = 300
$typeValue.height = 20
$typeValue.location = New-Object System.Drawing.Point(202,300)
$typeValue.Font = 'Microsoft Sans Serif,10'
# Checkbox for silo attribute
$silo = New-Object System.Windows.Forms.CheckBox
$silo.text = "Silo"
$silo.AutoSize = $true
$silo.width = 104
$silo.height = 20
$silo.location = New-Object System.Drawing.Point(100,350)
$silo.Font = 'Microsoft Sans Serif,10'
# Label for the silo checkbox
$siloValue = New-Object system.Windows.Forms.TextBox
$siloValue.multiline = $false
$siloValue.width = 300
$siloValue.height = 20
$siloValue.location = New-Object System.Drawing.Point(202,350)
$siloValue.Font = 'Microsoft Sans Serif,10'
# submitt button
$button = New-Object System.Windows.Forms.Button
$button.text = "Submit"
$button.AutoSize = $true
$button.location = New-Object System.Drawing.Point(250,500)
$button.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($mainLabel, $fileId,$filePath,$idLabel,$valueLabel,$FilePathLabel,$idValue,$descriptionValue,$description, $type, $typeValue, $silo, $siloValue, $button))
#region gui events {
function SubmitForm(){
if($fileId.checked -eq $true){
sc -path $filePath.Text -stream $fileId.text -value $idValue.Text
}
if($description.checked -eq $true){
sc -path $filePath.Text -stream $description.text -value $descriptionValue.text
}
if($type.checked -eq $true){
sc -path $filePath.Text -stream $type.text -value $typeValue.text
}
if($silo.checked -eq $true){
sc -path $filePath.Text -stream $silo.text -value $siloValue.text
}
[System.Windows.Forms.MessageBox]::Show("Successfully Added Attributes")
}
#Add Button event
$Button.Add_Click({SubmitForm})
#endregion events }
#endregion GUI }
# logic here
[void]$Form.ShowDialog()
Next you would need to create registry reference for context menu item and powershell script according to it. Like so:
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
New-Item HKCR:\directory\shell\PowerShellScript
New-Item HKCR:\directory\shell\PowerShellScript\command
Set-ItemProperty 'HKCR:\directory\shell\PowerShellScript\command' -Name '(default)' -Value 'Powershell -WindowStyle Hidden -ExecutionPolicy Bypass -NoExit -File "C:\Test.ps1" "%L"'
Context menu item:
Chosen directory passed to script's input parameter:

Related

How to move a device in AD to a different OU in a GUI

We have all of our Autopilot deployed devices in 1 OU and the techs have to move them to their own site's OU I have written a GUI to do this. they enter the device name and the location name. The location name is tha final ou the device will reside in. My GUI gets the OUs for the site and lists them in a Out-Gridview you click on the ou you want and click ok. it sends that to a textbox. then you click move. thats where I ger the error that the device cannot be found. I am sure I have some silly syntax wrong. Thanks in advance.
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'
[Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)
<#
.NAME
AP Device Move
#>
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,400)
$Form.text = "Form"
$Form.TopMost = $false
$LBL_APDEVICE = New-Object system.Windows.Forms.Label
$LBL_APDEVICE.text = "Computer Name"
$LBL_APDEVICE.AutoSize = $true
$LBL_APDEVICE.width = 25
$LBL_APDEVICE.height = 10
$LBL_APDEVICE.location = New-Object System.Drawing.Point(0,2)
$LBL_APDEVICE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TBX_APDEVICE = New-Object system.Windows.Forms.TextBox
$TBX_APDEVICE.Text = ""
$TBX_APDEVICE.multiline = $false
$TBX_APDEVICE.width = 100
$TBX_APDEVICE.height = 20
$TBX_APDEVICE.location = New-Object System.Drawing.Point(6,37)
$TBX_APDEVICE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$LBL_SITE = New-Object system.Windows.Forms.Label
$LBL_SITE.text = "Site Name"
$LBL_SITE.AutoSize = $true
$LBL_SITE.width = 25
$LBL_SITE.height = 10
$LBL_SITE.location = New-Object System.Drawing.Point(4,71)
$LBL_SITE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$LOCATION = New-Object system.Windows.Forms.TextBox
$LOCATION.multiline = $false
$LOCATION.width = 100
$LOCATION.height = 20
$LOCATION.location = New-Object System.Drawing.Point(3,101)
$LOCATION.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$DESTINATION_OU = New-Object system.Windows.Forms.TextBox
$DESTINATION_OU.text = ""
$DESTINATION_OU.multiline = $false
$DESTINATION_OU.width = 100
$DESTINATION_OU.height = 20
$DESTINATION_OU.location = New-Object System.Drawing.Point(14,194)
$DESTINATION_OU.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TARGET_OU = New-Object system.Windows.Forms.Label
$TARGET_OU.text = "Target OU"
$TARGET_OU.AutoSize = $true
$TARGET_OU.width = 25
$TARGET_OU.height = 10
$TARGET_OU.location = New-Object System.Drawing.Point(12,139)
$TARGET_OU.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Get_OU = New-Object system.Windows.Forms.Button
$Get_OU.text = "Get OUs for Site"
$Get_OU.width = 104
$Get_OU.height = 30
$Get_OU.location = New-Object System.Drawing.Point(133,54)
$Get_OU.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$BTN_MOVE = New-Object system.Windows.Forms.Button
$BTN_MOVE.text = "Move Device"
$BTN_MOVE.width = 91
$BTN_MOVE.height = 30
$BTN_MOVE.location = New-Object System.Drawing.Point(34,246)
$BTN_MOVE.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Form.controls.AddRange(#($LBL_APDEVICE,$TBX_APDEVICE,$LBL_SITE,$LOCATION,$DESTINATION_OU,$TARGET_OU,$Get_OU,$BTN_MOVE))
$Get_OU.Add_Click({ GetSiteOUs })
$BTN_MOVE.Add_Click({ doit })
#region Logic
function GetSiteOUs {
$DESTINATION_OU.Text = Get-ADOrganizationalUnit -Filter "Name -Like '*$($LOCATION.Text.Trim())*'" |
Select-Object -ExpandProperty 'Distinguishedname' |
Out-GridView -PassThru -Title "Select the OU"
}
function doit{
$DEVICE = $TBX_APDEVICE.TEXT
$DestOU = "OU=$DESTINATION_OU.text,OU=Computers,OU=World,OU=Disney,OU=Goofy,OU=Duck,OU=Donald,DC=Mickey,DC=Mouse,"
Move-ADObject –Identity "CN=$Device,OU=Autopilot,OU=Lucy,OU=linus,OU=Brown,OU=charlie,DC=Mickey,DC=Mouse," -TargetPath $DestOU
}
#endregion
[void]$Form.ShowDialog()
The answer was to adjust the doit function
function doit{
$DEVICE = $TBX_APDEVICE.TEXT
$DestOU = $DESTINATION_OU.text
Move-ADObject –Identity "CN=$Device,OU=Autopilot,OU=Lucy,OU=linus,OU=Brown,OU=charlie,DC=Mickey,DC=Mouse," -TargetPath $DestOU-TargetPath $DestOU
}

Copy listview1 to listview2

I'm trying to copy the checked item in ListADGroup and copy to ListADGroup2. ListADGroup updates with all the groups but when I try adding them to ListADGroup2 it copies the right amount checkboxes but they are empty. Im not sure what is missing from the button command to copy the data properly.
Add-Type -AssemblyName "System.Windows.Forms"
Add-Type -AssemblyName "System.Drawing"
Add-Type -AssemblyName PresentationFramework
Clear-Host
#== Create a New Form ==#
$TSTAPP=New-Object System.Windows.Forms.Form
$TSTAPP.topmost=$true
$TSTAPP.Text="Test App"
$TSTAPP.Location.x=750
$TSTAPP.Location.Y=330
$TSTAPP.Size=New-Object System.Drawing.Size(750,330)
#== Now Lock the form so it cannot be re-sized ==#
$TSTAPP.MaximumSize=New-Object System.Drawing.Size(750,330)
$TSTAPP.MinimumSize=New-Object System.Drawing.Size(750,330)
#== Group List Label ==#
$GroupL = New-Object System.Windows.Forms.Label
$GroupL.Text = "Select Group:"
$GroupL.Location = New-Object System.Drawing.Point(4,5)
$GroupL.AutoSize = $true
$TSTAPP.Controls.Add($GroupL)
#== AD Users Listbox ==#
$ListADGroup = New-Object system.Windows.Forms.Listview
$ListADGroup.Width = 356
$ListADGroup.height = 220
$ListADGroup.location = New-Object System.Drawing.Point(4,22)
#$ListADGroup.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ListADGroup.CheckBoxes = $true
$ListADGroup.Name = "Main"
$ListADGroup.AutoArrange = $true
$ListADGroup.GridLines = $true
$ListADGroup.MultiSelect = $false
$ListADGroup.View = "Details"
$ListADGroup.AutoSize = $true
#$ListADGroup.Columns[0].
$ListADGroup.Columns.Add("Groups")
$AllADGroups = Get-ADGroup -filter * -SearchBase 'OU=Accounts,DC=domain,DC=internal' -SearchScope subtree -Properties Name | Sort-Object Name | foreach{[void]$ListADGroup.Items.Add($_.name)}
$TSTAPP.controls.add($ListADGroup)
#== AD Users Listbox ==#
$ListADGroup2 = New-Object system.Windows.Forms.Listview
$ListADGroup2.Width = 356
$ListADGroup2.height = 220
$ListADGroup2.location = New-Object System.Drawing.Point(370,22)
#$ListADGroup2.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ListADGroup2.CheckBoxes = $true
$ListADGroup2.Name = "Main"
$ListADGroup2.AutoArrange = $true
$ListADGroup2.GridLines = $true
$ListADGroup2.MultiSelect = $false
$ListADGroup2.View = "Details"
$ListADGroup2.AutoSize = $true
#$ListADGroup2.Columns.Width = 100
$ListADGroup2.Columns.Add("Groups2")
$TSTAPP.controls.add($ListADGroup2)
#== Add Button ==#
$AddGroup = New-Object system.Windows.Forms.Button
$AddGroup.text = "Add"
$AddGroup.width = 100
$AddGroup.height = 30
$AddGroup.location = New-Object System.Drawing.Point(265,249)
$AddGroup.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$TSTAPP.controls.add($AddGroup)
$AddGroup.Add_Click({
$itm = $ListADGroup.CheckedItems
foreach ($items in $itm){
$ListADGroup2.Items.Add($items.name)
}
})
$TSTAPP.ShowDialog()
If you try to debug using ISE or Visual Code you will see that the property containing the name is called Text
So replace :
$ListADGroup2.Items.Add($items.name)
by
$ListADGroup2.Items.Add($items.Text)

Trying to modify powershell to include combobox to populate listbox

I have a working script that has a single printserver name hard coded into it which populates a listbox. you can click on a printer and click install it and then click to make it the default printer. This works fine but I would like to have a dropdown menu that contains several printservers. when a printserver is chosen from the list it would populate the listbox with all of the printers on that printserver. I have fried everything I can think of to make this work. what am I not getting right Below are the 2 sets of code. Working one first.
Working Code
### Variables
#Printserver
$printserver = "Myprintserver"
### Hide PowerShell Console
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
### GUI
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '350,237'
$Form.text = "Printer Install Tool"
$Form.TopMost = $false
$btn_Cancel = New-Object system.Windows.Forms.Button
$btn_Cancel.text = "Close"
$btn_Cancel.width = 110
$btn_Cancel.height = 30
$btn_Cancel.location = New-Object System.Drawing.Point(202,181)
$btn_Cancel.Font = 'Microsoft Sans Serif,10'
$btn_Install = New-Object system.Windows.Forms.Button
$btn_Install.text = "Install printer"
$btn_Install.width = 110
$btn_Install.height = 30
$btn_Install.location = New-Object System.Drawing.Point(202,101)
$btn_Install.Font = 'Microsoft Sans Serif,10'
$btn_Default = New-Object system.Windows.Forms.Button
$btn_Default.text = "Set as Default"
$btn_Default.width = 110
$btn_Default.height = 30
$btn_Default.location = New-Object System.Drawing.Point(202,141)
$btn_Default.Font = 'Microsoft Sans Serif,10'
$txtBox_Location = New-Object system.Windows.Forms.TextBox
$txtBox_Location.multiline = $false
$txtBox_Location.width = 147
$txtBox_Location.height = 20
$txtBox_Location.location = New-Object System.Drawing.Point(179,35)
$txtBox_Location.Font = 'Microsoft Sans Serif,8'
$listBox_Printers = New-Object system.Windows.Forms.ListBox
$listBox_Printers.text = "listBox"
$listBox_Printers.width = 159
$listBox_Printers.height = 192
$listBox_Printers.location = New-Object System.Drawing.Point(11,35)
$lbl_ChoosePrinter = New-Object system.Windows.Forms.Label
$lbl_ChoosePrinter.text = "Choose a printer"
$lbl_ChoosePrinter.AutoSize = $true
$lbl_ChoosePrinter.width = 25
$lbl_ChoosePrinter.height = 10
$lbl_ChoosePrinter.location = New-Object System.Drawing.Point(11,16)
$lbl_ChoosePrinter.Font = 'Microsoft Sans Serif,10'
$lbl_PrinterLoc = New-Object system.Windows.Forms.Label
$lbl_PrinterLoc.text = "Printer location"
$lbl_PrinterLoc.AutoSize = $true
$lbl_PrinterLoc.width = 25
$lbl_PrinterLoc.height = 10
$lbl_PrinterLoc.location = New-Object System.Drawing.Point(179,16)
$lbl_PrinterLoc.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($btn_Cancel,$btn_Install,$txtBox_Location,$listBox_Printers,$lbl_ChoosePrinter,$lbl_PrinterLoc,$btn_Default))
### Eventhandlers
$listBox_Printers.Add_SelectedIndexChanged({
$printName = $listBox_Printers.SelectedItem
#write-host $printName
$txtBox_Location.Text = Get-Printer -ComputerName $printserver -Name $printName | Select-Object -ExpandProperty "Location"
})
$btn_Default.Add_Click({
$printName = $listBox_Printers.SelectedItem
(New-Object -ComObject WScript.Network).SetDefaultPrinter("\\$printserver\$printName")
})
$btn_Cancel.Add_Click({
$Form.Close()
})
$btn_Install.Add_Click({
$printName = $listBox_Printers.SelectedItem
get-wmiobject -class win32_printer -computer $printserver
rundll32 printui.dll,PrintUIEntry /in /n \\$printserver\$printName
})
### Main
$printName = $listBox_Printers.SelectedItem
$printers = Get-Printer -ComputerName $printserver | Select-Object -Property "Location", "Name"
foreach($printer in $printers){
$listBox_Printers.Items.Add($printer.Name)
}
[void]$Form.ShowDialog()
Non working code
### Variables
#Printserver
$printserver = "$cmbx_Server.SelectedItem"
### Hide PowerShell Console
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
[Console.Window]::ShowWindow($consolePtr, 0)
### GUI
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '350,450'
$Form.text = "Printer Install Tool"
$Form.TopMost = $false
$lbl_Server = New-Object system.Windows.Forms.Label
$lbl_Server.text = "Print Server"
$lbl_Server.AutoSize = $true
$lbl_Server.width = 25
$lbl_Server.height = 10
$lbl_Server.location = New-Object System.Drawing.Point(11,16)
$lbl_Server.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$cmbx_Server = New-Object system.Windows.Forms.ComboBox
$cmbx_Server.text = "Choose Server"
$cmbx_Server.width = 100
$cmbx_Server.height = 30
#('Printserver1','Printserver2','Printserver3','Printserver3','Printserver4','Printserver5','Printserver6','Printserver7','Printserver8','Printserver9','Printserver0') | ForEach-Object {[void] $cmbx_Server.Items.Add($_)}
$cmbx_Server.location = New-Object System.Drawing.Point(6,46)
$cmbx_Server.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$btn_Server = New-Object system.Windows.Forms.Button
$btn_Server.text = "Select Server"
$btn_Server.width = 150
$btn_Server.height = 30
$btn_Server.location = New-Object System.Drawing.Point(110,46)
$btn_Server.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$btn_Cancel = New-Object system.Windows.Forms.Button
$btn_Cancel.text = "Close"
$btn_Cancel.width = 100
$btn_Cancel.height = 30
$btn_Cancel.location = New-Object System.Drawing.Point(225,410)
$btn_Cancel.Font = 'Microsoft Sans Serif,10'
$btn_Install = New-Object system.Windows.Forms.Button
$btn_Install.text = "Install printer"
$btn_Install.width = 100
$btn_Install.height = 30
$btn_Install.location = New-Object System.Drawing.Point(10,410)
$btn_Install.Font = 'Microsoft Sans Serif,10'
$btn_Default = New-Object system.Windows.Forms.Button
$btn_Default.text = "Set as Default"
$btn_Default.width = 100
$btn_Default.height = 30
$btn_Default.location = New-Object System.Drawing.Point(117,410)
$btn_Default.Font = 'Microsoft Sans Serif,10'
$txtBox_Location = New-Object system.Windows.Forms.TextBox
$txtBox_Location.multiline = $false
$txtBox_Location.width = 147
$txtBox_Location.height = 20
$txtBox_Location.location = New-Object System.Drawing.Point(179,110)
$txtBox_Location.Font = 'Microsoft Sans Serif,8'
$listBox_Printers = New-Object system.Windows.Forms.ListBox
$listBox_Printers.text = "listBox"
$listBox_Printers.width = 159
$listBox_Printers.height = 192
$listBox_Printers.location = New-Object System.Drawing.Point(11,110)
$lbl_ChoosePrinter = New-Object system.Windows.Forms.Label
$lbl_ChoosePrinter.text = "Choose a printer"
$lbl_ChoosePrinter.AutoSize = $true
$lbl_ChoosePrinter.width = 25
$lbl_ChoosePrinter.height = 10
$lbl_ChoosePrinter.location = New-Object System.Drawing.Point(11,90)
$lbl_ChoosePrinter.Font = 'Microsoft Sans Serif,10'
$lbl_PrinterLoc = New-Object system.Windows.Forms.Label
$lbl_PrinterLoc.text = "Printer location"
$lbl_PrinterLoc.AutoSize = $true
$lbl_PrinterLoc.width = 25
$lbl_PrinterLoc.height = 10
$lbl_PrinterLoc.location = New-Object System.Drawing.Point(179,90)
$lbl_PrinterLoc.Font = 'Microsoft Sans Serif,10'
$Form.controls.AddRange(#($btn_Cancel,$btn_Install,$txtBox_Location,$listBox_Printers,$lbl_ChoosePrinter,$lbl_PrinterLoc,$btn_Default,$lbl_Server,$cmbx_Server,$btn_Server))
### Eventhandlers
$cmbx_Server.Add_SelectedIndexChanged({
write-host $cmbx_Server.text
})
$listBox_Printers.Add_SelectedIndexChanged({
$printName = $listBox_Printers.SelectedItem
#write-host $printName
$txtBox_Location.Text = Get-Printer -ComputerName $printserver -Name $printName | Select-Object -ExpandProperty "Location"
})
$btn_Default.Add_Click({
Get-PSDrive
Set-Location 'HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows'
Get-ItemProperty -path .
Set-ItemProperty -path . -name "LegacyDefaultPrinterMode" -value 1
c:
$printName = $listBox_Printers.SelectedItem
(New-Object -ComObject WScript.Network).SetDefaultPrinter("\\$printserver\$printName")
})
$btn_Server.Add_Click({$listBox_Printers.Text = $cmbx_Server.SelectedItem("Get-printer -Computername $printserver")})
#})
$btn_Cancel.Add_Click({
$Form.Close()
})
$btn_Install.Add_Click({
$printName = $listBox_Printers.SelectedItem
#get-wmiobject -class win32_printer -computer $printserver
#rundll32 printui.dll,PrintUIEntry /in /n \\$printserver\$printName
Add-Printer -ConnectionName \\$printServer\$printName
})
### Main
$printName = $listBox_Printers.SelectedItem
$printers = Get-Printer -ComputerName $printserver | Select-Object -Property "Location", "Name"
foreach($printer in $cmbx_Server.SelectedItem){
$listBox_Printers.Items.Add($printer.Name)
}
[void]$Form.ShowDialog()
Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static ex`enter code here`tern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);
'
$consolePtr = [Console.Window]::GetConsoleWindow()
#[Console.Window]::ShowWindow($consolePtr, 0)
### GUI
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '700,450'
$Form.text = "Printer Installation Tool"
$Form.TopMost = $false
$cmbx_server = New-Object system.Windows.Forms.ComboBox
#$cmbx_server.text = "Printserver1"
$cmbx_server.width = 130
$cmbx_server.height = 20
#('Printserver1','Printserver2','Printserver3') | ForEach-Object {[void] $cmbx_server.Items.Add($_)}
$cmbx_server.location = New-Object System.Drawing.Point(11,59)
$cmbx_server.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$lbl_Server = New-Object system.Windows.Forms.Label
$lbl_Server.text = "Choose Server"
$lbl_Server.AutoSize = $true
$lbl_Server.width = 25
$lbl_Server.height = 10
$lbl_Server.location = New-Object System.Drawing.Point(11,16)
$lbl_Server.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$btn_Cancel = New-Object system.Windows.Forms.Button
$btn_Cancel.text = "Close"
$btn_Cancel.width = 110
$btn_Cancel.height = 30
$btn_Cancel.location = New-Object System.Drawing.Point(300,400)
$btn_Cancel.Font = 'Microsoft Sans Serif,10'
$btn_Install = New-Object system.Windows.Forms.Button
$btn_Install.text = "Install printer"
$btn_Install.width = 100
$btn_Install.height = 30
$btn_Install.location = New-Object System.Drawing.Point(11,310)
$btn_Install.Font = 'Microsoft Sans Serif,10'
$btn_Remove = New-Object system.Windows.Forms.Button
$btn_Remove.text = "Remove Printer"
$btn_Remove.width = 120
$btn_Remove.height = 30
$btn_Remove.location = New-Object System.Drawing.Point(375,310)
$btn_Remove.Font = 'Microsoft Sans Serif,10'
$lbl_Note = New-Object system.Windows.Forms.Label
$lbl_Note.text = "Note: Cannot remove Default Printer"
$lbl_Note.AutoSize = $true
$lbl_Note.width = 300
$lbl_Note.height = 10
$lbl_Note.location = New-Object System.Drawing.Point(350,340)
$lbl_Note.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$txtBox_Location = New-Object system.Windows.Forms.TextBox
$txtBox_Location.multiline = $false
$txtBox_Location.width = 147
$txtBox_Location.height = 20
$txtBox_Location.location = New-Object System.Drawing.Point(179,59)
$txtBox_Location.Font = 'Microsoft Sans Serif,8'
$listBox_Printers = New-Object system.Windows.Forms.ListBox
$listBox_Printers.text = ""
$listBox_Printers.width = 275
$listBox_Printers.height = 192
$listBox_Printers.location = New-Object System.Drawing.Point(11,120)
$listBox_Local_Printers = New-Object system.Windows.Forms.ListBox
$listBox_Local_Printers.text = ""
$listBox_Local_Printers.width = 310
$listBox_Local_Printers.height = 192
$listBox_Local_Printers.location = New-Object System.Drawing.Point(375,120)
$lbl_Local_Printer = New-Object system.Windows.Forms.Label
$lbl_Local_Printer.text = "Installed Printers"
$lbl_Local_Printer.AutoSize = $true
$lbl_Local_Printer.width = 25
$lbl_Local_Printer.height = 10
$lbl_Local_Printer.location = New-Object System.Drawing.Point(400,95)
$lbl_Local_Printer.Font = 'Microsoft Sans Serif,10'
$lbl_ChoosePrinter = New-Object system.Windows.Forms.Label
$lbl_ChoosePrinter.text = "Available Network Printers"
$lbl_ChoosePrinter.AutoSize = $true
$lbl_ChoosePrinter.width = 25
$lbl_ChoosePrinter.height = 10
$lbl_ChoosePrinter.location = New-Object System.Drawing.Point(11,95)
$lbl_ChoosePrinter.Font = 'Microsoft Sans Serif,10'
$lbl_PrinterLoc = New-Object system.Windows.Forms.Label
$lbl_PrinterLoc.text = "Printer location"
$lbl_PrinterLoc.AutoSize = $true
$lbl_PrinterLoc.width = 25
$lbl_PrinterLoc.height = 10
$lbl_PrinterLoc.location = New-Object System.Drawing.Point(179,16)
$lbl_PrinterLoc.Font = 'Microsoft Sans Serif,10'
### Set as Default Button
$btn_Default = New-Object system.Windows.Forms.Button
$btn_Default.text = "Set as Default"
$btn_Default.AutoSize = $false
$btn_Default.width = 110
$btn_Default.height = 30
$btn_Default.location = New-Object System.Drawing.Point(575,310)
$btn_Default.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
### Refresh button
$btn_Refresh = New-Object system.Windows.Forms.Button
$btn_Refresh.text = "Refresh"
$btn_Refresh.AutoSize = $false
$btn_Refresh.width = 80
$btn_Refresh.height = 30
$btn_Refresh.location = New-Object System.Drawing.Point(495,310)
$btn_Refresh.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Form.controls.AddRange(#($lbl_Note, $btn_Remove, $btn_Refresh, $btn_Cancel,$btn_Install,$txtBox_Location,$listBox_Printers,$lbl_ChoosePrinter,$lbl_PrinterLoc,$cmbx_server,$lbl_server,$btn_Default,$listBox_Local_Printers,$lbl_Local_Printer))
### Eventhandlers
$cmbx_server.Add_SelectedValueChanged({
$global:printserver = $cmbx_server.Items[$cmbx_server.SelectedIndex]
DisplayPrinters
})
$listBox_Printers.Add_SelectedValueChanged({ $cmbx_Server
$printName = $listBox_Printers.SelectedItem
$txtBox_Location.Text = Get-Printer -ComputerName $global:printserver -Name $printName | Select-Object -ExpandProperty "Location"
})
$btn_Default.Add_Click({
$printName = $listBox_Local_Printers.SelectedItem
SetPrinterAsDefault $printName
})
$btn_Remove.Add_Click({
$printName = $listBox_Local_Printers.SelectedItem
Remove-Printer $printName
})
$btn_Refresh.Add_Click({
DisplayPrinters
})
$btn_Cancel.Add_Click({
$Form.Close()
})
$btn_Install.Add_Click({
$printName = $listBox_Printers.SelectedItem
Add-Printer -ConnectionName \\$global:printserver\$printName
#SetPrinterAsDefault \\$global:printserver\$printName
})
Function SetPrinterAsDefault($printName){
Set-ItemProperty -path "HKCU:\Software\Microsoft\Windows NT\CurrentVersion\Windows" -name "LegacyDefaultPrinterMode" -value 1
if($printName -like "*(Default Printer)"){
#Printer is already default
DisplayPrinters
return
}
$printInfo = Get-Printer $printName
if($printInfo -ne $null){
if($printInfo.Type -eq "Local"){
$printer = Get-CimInstance -Class Win32_Printer -Filter "Name='$printName'"
Invoke-CimMethod -InputObject $printer -MethodName SetDefaultPrinter
}
else{
(New-Object -ComObject WScript.Network).SetDefaultPrinter("$printName")
}
}
DisplayPrinters
}
Function DisplayPrinters(){
#Remove all old
$listBox_Printers.Items.Clear()
$listBox_local_Printers.Items.Clear()
$printers = Get-Printer -ComputerName $global:printserver | Select-Object -Property "Location", "Name"
foreach($printer in $printers){
$listBox_Printers.Items.Add($printer.Name) | Out-Null
}
$defaultPrinter = Get-WmiObject -Query " SELECT * FROM Win32_Printer WHERE Default=$true" | select -ExpandProperty name
$local_printers = Get-Printer | Select-Object Name
foreach($printer in $local_printers){
if($printer.Name -eq $defaultPrinter){
$printer.name = $printer.name + " (Default Printer)"
}
$listBox_local_Printers.Items.Add($printer.Name) | Out-Null
}
}
### Main
$cmbx_server.text = $cmbx_server.Items[0]
[void]$Form.ShowDialog()

PowerShell GUI - Function within a Form not working

currently i am working on an little PowerShell GUI Porject.
I have a Form within this Form there's a Button to call another Form that provides an Listbox with some items in it.
So far so good - i'm getting the results as expected.
Now i mark one of the items - click on OK - then it should do something.
And here is my Problem, it does nothing :D No Error, no warning, just nothing.
If i test with write-host "$($listBox.SelectedItem)" - i get the selected Item as i expect it.
But if you want to use it with get-adfsrelyingpartytrust -Name $listBox.SelectedItem - nothing happens.
If i execute the function without the rest (so that only the listbox appears) everything works as intended O_o.
Code:
function single_rpt{ $RPTForm = New-Object System.Windows.Forms.Form
$RPTForm.ClientSize = '400,200'
$RPTForm.Text = "Choose"
$RPTForm.BackColor = "#b2b2b2"
$RPTForm.AutoSize = $false
$RPTForm.StartPosition = 'CenterScreen'
$RPTForm.ControlBox = $true
$rptButton = New-Object System.Windows.Forms.Button
$rptButton.BackColor = "#ffffff"
$rptButton.Text = "OK"
$rptButton.Width = 90
$rptButton.Height = 30
$rptButton.Location = New-Object System.Drawing.Point(20,125)
$rptButton.Font = 'Microsoft Sans Serif,10'
$rptButton.ForeColor = "#000000"
$rptButton.UseVisualStyleBackColor = $true
$rptButton.DialogResult = 1
# Test List Box
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(20,50)
$listBox.Size = New-Object System.Drawing.Size(350,200)
$listBox.Height = 80
$RPTForm.Controls.AddRange(#($listBox,$rptButton))
$listboxArray = #()
$ADFS_RPTS = Get-AdfsRelyingPartyTrust
Foreach($rpt in $ADFS_RPTS)
{
#$Object = Add-Member -TypeName NoteProperty -Name Service -Value $service.Name
$listboxArray += $rpt.Name
}
$listBox.Items.AddRange($listboxArray)
[void]$RPTForm.ShowDialog()
if($RPTForm.DialogResult -eq "OK")
{
Get-AdfsReylingPartyTrus -Name $listBox.SelectedItem
}
}
# Init PowerShell Gui
Add-Type -AssemblyName System.Windows.Forms
# Create Form with size, title and background color, etc.
$TESTForm = New-Object System.Windows.Forms.Form
$TESTForm.ClientSize = '250,300'
$TESTForm.Text = "TEST"
$TESTForm.BackColor = "#ffffff"
$TESTForm.AutoSize = $false
$TESTForm.StartPosition = 'CenterScreen'
$TESTForm.ControlBox = $true
$RPTSingle = New-Object System.Windows.Forms.Button
$RPTSingle.BackColor = "#ffffff"
$RPTSingle.Text = "RPTSingle"
$RPTSingle.Width = 90
$RPTSingle.Height = 30
$RPTSingle.Location = New-Object System.Drawing.Point(75,125)
$RPTSingle.Font = 'Microsoft Sans Serif,10'
$RPTSingle.ForeColor = "#000000"
$RPTSingle.UseVisualStyleBackColor = $true
$RPTSingle.Add_Click({single_rpt})
# Add a Cancel Button
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.BackColor = "#ffffff"
$CancelButton.Text = "Cancel"
$CancelButton.Width = 90
$CancelButton.Height = 30
$CancelButton.Location = New-Object System.Drawing.Point(75,250)
$CancelButton.Font = 'Microsoft Sans Serif,10'
$CancelButton.ForeColor = "#000000"
$CancelButton.UseVisualStyleBackColor = $true
$CancelButton.DialogResult = 2
$TESTForm.Controls.AddRange(#($RPTSingle,$CancelButton))
# Display the form
[void]$TESTForm.ShowDialog()
#------------------------------------#

getting database table to show in dataGridView in powershell

I am building a powershell gui application that pulls info from a database table that I need to monitor. I am having an issue getting that data into the datagridview. I can do it manually so it shows at least one entry but I need it to show the full table results. This is my 1st powershell GUI project.
add-type -AssemblyName System.Data.OracleClient
Function ShowJobsInQueue()
{
## To connect by Service Name
$ora_server = "dm01-scan.campsys.com"
$ora_user = "appuser"
$ora_pass = "hillary"
$ora_servicename = "dbcamp1_svc.campsys.com"
## by ServiceName
$connection = new-object system.data.oracleclient.oracleconnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$ora_server)(PORT=1521)) (CONNECT_DATA=(SERVICE_NAME=$ora_servicename)));User Id=$ora_user;Password=$ora_pass;")
$connection.open()
$query = "SELECT * FROM ASYNCJOB WHERE TRUNC(START_DATE)= TRUNC(SYSDATE) AND job_type <>15 and STATUS in ('1','2') ORDER BY ASYNCJOB_ID DESC"
$list_set = new-object system.data.dataset
#$array = New-Object System.Collections.ArrayList
#$array.AddRange($list_table)
#$dataGridView.DataSource = $array
$list_adapter = new-object system.data.oracleclient.oracledataadapter($query, $connection)
$list_adapter.Fill($list_set) | Out-Null
$list_table = new-object system.data.datatable
$list_table = $list_set.Tables[0]
$DBValues = $list_table
while($dataGridView.Rows.Count -lt $list_table.Count) {
$dataGridView.Rows.Add() | Out-Null
}
for ($i=0;$i -lt $list_table.Count ;$i++) {
$dataGridView.rows[$i].Cells[0].Value = $list_table[$i].Item("ASYNCJOB_ID")
}
$connection.close()
$form.refresh()
}
$OnLoadForm_UpdateGrid=
{
ShowJobsInQueue
}
########################################
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = "Report Monitor"
$Form.TopMost = $true
$Form.minimumSize = New-Object System.Drawing.Size(1024,750)
$Form.maximumSize = New-Object System.Drawing.Size(1024,750)
$dataGridView = New-Object System.Windows.Forms.DataGridView
$dataGridView.Size=New-Object System.Drawing.Size(1024,570)
$form.Controls.Add($dataGridView)
$buttonRefresh = New-Object system.windows.Forms.Button
$buttonRefresh.Text = "Refresh"
$buttonRefresh.Width = 65
$buttonRefresh.Height = 35
$buttonRefresh.Add_Click({
$Form.close()
})
$buttonRefresh.location = new-object system.drawing.point(338,600)
$buttonRefresh.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonRefresh)
$buttonExit = New-Object system.windows.Forms.Button
$buttonExit.Text = "Exit"
$buttonExit.Width = 65
$buttonExit.Height = 35
$buttonExit.Add_Click({
$Form.close()
})
$buttonExit.location = new-object system.drawing.point(500,600)
$buttonExit.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonExit)
#Create an unbound DataGridView by declaring a column count.
$dataGridView.ColumnCount = 6
$dataGridView.ColumnHeadersVisible = $true
#Set the column header names.
$dataGridView.Columns[0].Name = "Row#"
$dataGridView.Columns[1].Name = "ASYNCJOB_ID"
$dataGridView.Columns[2].Name = "USER_ID"
$dataGridView.Columns[3].Name = "START_DATE"
$dataGridView.Columns[4].Name = "END_DATE"
$dataGridView.Columns[5].Name = "STATUS"
#$dataGridView.Rows[0].Cells[0].Value = "Value1"
#$dataGridView.Rows[0].Cells[1].Value = "Value2"
#$dataGridView.Rows[0].Cells[2].Value = "Value3"
#$dataGridView.Rows.Add();
#############################################################
$buttonOn = New-Object system.windows.Forms.RadioButton
$buttonOn.Text = "On"
$buttonOn.Width = 65
$buttonOn.Height = 35
$buttonOn.Add_Click({
# create function to turn on auto mode
$Form.close()
})
$buttonOn.location = new-object system.drawing.point(920,600)
$buttonOn.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonOn)
## button off
$buttonOff = New-Object system.windows.Forms.RadioButton
$buttonOff.Text = "Off"
$buttonOff.Width = 65
$buttonOff.Height = 35
$buttonOff.Add_Click({
# create function to turn off auto mode
$Form.close()
})
$buttonOff.location = new-object system.drawing.point(870,600)
$buttonOff.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($buttonOff)
## end button off
#### auto mode label
$Automode = New-Object system.windows.Forms.Label
$Automode.Text = "Auto Mode"
$Automode.Width = 25
$Automode.Height = 10
$Automode.AutoSize = $true
$Automode.location = new-object system.drawing.point(875,580)
$Automode.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($Automode)
#### end label
#Add Form event
$form.add_Load($OnLoadForm_UpdateGrid)
[void]$Form.ShowDialog()
$Form.Dispose()
Here is an example how you could do it:
$DBValues = "Value1","Value2","Value3","Value4","Value5","Value6","Value7","Value8","Value9","Value10"
while($dataGridView.Rows.Count -lt $DBValues.Count) {
$dataGridView.Rows.Add() | Out-Null
}
for ($i=0;$i -lt $DBValues.Count ;$i++) {
$dataGridView.rows[$i].Cells[0].Value = $DBValues[$i]
}
In your case you should do something like that:
while($dataGridView.Rows.Count -lt $list_table.Rows.Count) {
$dataGridView.Rows.Add() | Out-Null
}
for ($i=0;$i -lt $list_table.Rows.Count ;$i++) {
$dataGridView.rows[$i].Cells[0].Value = $list_table.Rows[$i].Item("ASYNCJOB_ID")
}