Solved

powershell system.windows.forms

Posted on 2014-01-04
10
2,080 Views
Last Modified: 2014-01-04
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
0
Comment
Question by:Dead_Eyes
  • 5
  • 5
10 Comments
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39756322
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
 

Author Comment

by:Dead_Eyes
ID: 39756463
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
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39756503
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
 

Author Comment

by:Dead_Eyes
ID: 39756592
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
 
LVL 18

Accepted Solution

by:
Steven Harris earned 500 total points
ID: 39756614
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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:Dead_Eyes
ID: 39756622
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
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39756649
What version of PowerShell?
0
 

Author Comment

by:Dead_Eyes
ID: 39756677
v4
0
 

Author Closing Comment

by:Dead_Eyes
ID: 39756702
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
 
LVL 18

Expert Comment

by:Steven Harris
ID: 39756786
Glad you got it worked out!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
powershell - out of memory issues on process.exe 4 50
Help with PS script 7 30
ADFS / 365 : Multi domain single forest 5 55
simple AD powershell script 8 76
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now