powershell system.windows.forms

Hi I need some help designing a GUI. I know some of the basics (text boxes ect). But what I really need is
1.) A browse box so the user can look through the file system to find the file. Once they have selected the file I need it be assigned to a $variable so I can use it with the Import-Csv cmdlet.
2.) 3 x Tick boxs linking to 3 Variables ($tick1, $tick2 & $tick3) and assigning a value of 1 if ticked and 0 if unticked
3.) ok and cancel buttons (when cancel is selected I need it to exit the whole script not just proceed)

Thanks in advance
Dead_EyesAsked:
Who is Participating?
 
Steven HarrisConnect With a Mentor PresidentCommented:
Did you select a File?  It is acting as if you canceled the Explorer Window.

Instead of pushing it to a variable, try the following to return the selected file's name:

Function Get-FileName
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

write-host $file

Open in new window

0
 
Steven HarrisPresidentCommented:
1.) A browse box so the user can look through the file system to find the file. Once they have selected the file I need it be assigned to a $variable so I can use it with the Import-Csv cmdlet.

Try the following: ($csvImport holds the file contents)

Function Get-FileName
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

$csvImport = import-csv $file

Open in new window


2.) 3 x Tick boxs linking to 3 Variables ($tick1, $tick2 & $tick3) and assigning a value of 1 if ticked and 0 if unticked

Have you played around with If/Else statements to set the value?

if($checkbox.checked -eq $true) {
$tick1 = 1
}
Else($checkbox.checked -eq $false {
$tick1 = 0
}

Open in new window


3.) ok and cancel buttons (when cancel is selected I need it to exit the whole script not just proceed)

Close the form and keep processing:

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,100)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$form.Close()})
$Form.Controls.Add($OKButton)

Open in new window


Cancel the entire operation:

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,100)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({[System.Windows.Forms.Application]::Exit($null)})
$Form.Controls.Add($CancelButton)

Open in new window




If we throw it all together in a simple form, we get the following:

$form = new-object System.Windows.Forms.Form
$form.width = 200
$form.Height = 200

$Checkbox1 = new-object System.Windows.Forms.Checkbox
$Checkbox1.Text = "CheckBox1"
$Checkbox1.Location = new-object System.Drawing.Size(10,10)
$Checkbox1.size = new-object System.Drawing.Size(100,20)
$Form.Controls.Add($Checkbox1)

$Checkbox2 = new-object System.Windows.Forms.Checkbox
$Checkbox2.Text = "CheckBox2"
$Checkbox2.Location = new-object System.Drawing.Size(10,30)
$Checkbox2.size = new-object System.Drawing.Size(100,20)
$Form.Controls.Add($Checkbox2)

$Checkbox3 = new-object System.Windows.Forms.Checkbox
$Checkbox3.Text = "CheckBox2"
$Checkbox3.Location = new-object System.Drawing.Size(10,50)
$Checkbox3.size = new-object System.Drawing.Size(100,20)
$Form.Controls.Add($Checkbox3)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(10,100)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$form.Close()})
$Form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(100,100)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({[System.Windows.Forms.Application]::Exit($null)})
$Form.Controls.Add($CancelButton)

$form.ShowDialog()

if($Checkbox1.Checked -eq $true){$tick1=1}Else{$tick1=0}
if($Checkbox2.Checked -eq $true){$tick2=1}Else{$tick2=0}
if($Checkbox3.Checked -eq $true){$tick3=1}Else{$tick3=0}


[System.Windows.Forms.MessageBox]::Show("Checkbox1 value is set to " + $tick1)
[System.Windows.Forms.MessageBox]::Show("Checkbox2 value is set to " + $tick2)
[System.Windows.Forms.MessageBox]::Show("Checkbox3 value is set to " + $tick3)

Open in new window

0
 
Dead_EyesAuthor Commented:
Hi sorry only just saw the post. thanks for the great explanations some of this code is really simplifying things. I can't get the browse bit right though. Setting it as a function seems like a good idea but the people I need to run this script would run a mile if they saw a cmd prompt so really need to give them a nice simple browse box (click browse button scroll though an explorer style interface and select the file). I can't seem to find anything under system.windows.forms that matches though.(thanks again for if / else bit I have never used else before).
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
Steven HarrisPresidentCommented:
the people I need to run this script would run a mile if they saw a cmd prompt so really need to give them a nice simple browse box (click browse button scroll though an explorer style interface and select the file).


I am not sure I follow you at this point.  When you run the code below:

Function Get-FileName
{   
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

$csvImport = import-csv $file

Open in new window


You will get the Windows File Browser to open (as you normally see when opening files).

Windows Browser
The contents of whatever file is selected is stored as the $csvImport variable.
0
 
Dead_EyesAuthor Commented:
I am running into this error
PS C:\Windows\system32> Function Get-FileName
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

$csvImport = import-csv $file
Import-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply
an argument that is not null or empty and then try the command again.
At line:14 char:25
+ $csvImport = import-csv $file
+                         ~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.Impo
   rtCsvCommand
0
 
Dead_EyesAuthor Commented:
I could not select a file it did not show an explorer menu. the updated code runs but again no explorer window :(
PS C:\Windows\system32> Function Get-FileName
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
 Out-Null

 $FileDialog = New-Object System.Windows.Forms.OpenFileDialog
 $FileDialog.filter = "All files (*.*)| *.*"
 $FileDialog.ShowDialog() | Out-Null
 $FileDialog.filename
} #end function Get-FileName

$file=Get-FileName

write-host $file


PS C:\Windows\system32>
0
 
Steven HarrisPresidentCommented:
What version of PowerShell?
0
 
Dead_EyesAuthor Commented:
v4
0
 
Dead_EyesAuthor Commented:
My bad I must have had something in the background running that was messing with the code. thanks for a great explanation and your patience
0
 
Steven HarrisPresidentCommented:
Glad you got it worked out!
0
All Courses

From novice to tech pro — start learning today.