I created a script for ping the some IP Address. So I am trying to drop down selection. But in the dropdown selection text file input data is showing. But I cannot get the selected value in drop down selection.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$Form1 = New-Object system.Windows.Forms.Form
$Form1.ClientSize = ‘600,450’
$Form1.text = “PowerShell GUI Example”
$Form1.BackColor = “#ffffff”
$List = New-Object system.Windows.Forms.ComboBox
$List.text = “”
$List.width = 170
$List.autosize = $true
# Add the items in the dropdown list
[array]$result = (Get-Content E:\IP.txt)
$result | ForEach-Object {[void] $List.Items.Add($_)}
# Select the default value
$List.SelectedIndex = 0
$List.location = New-Object System.Drawing.Point(70,100)
$List.Font = ‘Microsoft Sans Serif,10’
$List.add_SelectedIndexChanged({
$selected = $List.SelectedItem
write-host $selected
#$Description.text = “Selected index: $selected”
})
$Form1.Controls.Add($List)
# Add a Button which can be used to generate an action from our textboxes
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(10,10)
$Button.Size = New-Object System.Drawing.Size(100,40)
$Button.Text = "PING"
# Declare the action to occur when button clicked
$Button.Add_Click( { PING } )
# Initialize the button inside the Form
$Form1.Controls.Add($Button)
# Create a Function to make use of textboxes
function PING {
Ping.exe -t $selected | ForEach {"{0} - {1}" -f (Get-Date),$_} | Tee-Object E:\ping.txt -Append | Out-GridView -Title 'PING'
}
# Display the form
[void]$Form1.ShowDialog()
Related
I am trying to display the powershell outputs in outbox of powershell forms. But it showing blank. not output data is showing in outbox. but here I used for output variable as $result. In that output variable the excepted result is available. Only thing I cannot display the output in outbox of powershell forms.
# Load required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create Form to contain elements
$Form = New-Object System.Windows.Forms.Form
# Set Form Titlebar text
$Form.Text = "AD ACCOUNT LOCKOUT FINDER"
# Set size of form
# Size(<length>,<height>) in pixels
$Form.Size = New-Object System.Drawing.Size(600,600)
# Create an Input textbox
$inputBoxgmr = New-Object System.Windows.Forms.TextBox
$inputBoxgmr.Location = New-Object System.Drawing.Size(30,50)
$inputBoxgmr.Size = New-Object System.Drawing.Size(150,20)
# Initialize the textbox inside the Form
$Form.Controls.Add($inputBoxgmr)
# Create Instruction Label for inputBox
$Labelgmr = New-Object System.Windows.Forms.Label
$Labelgmr.Text = "Enter Domain User ID"
$Labelgmr.Location = New-Object System.Drawing.Size(30,30)
$Labelgmr.BackColor = "Transparent"
$Labelgmr.AutoSize = $true
# Add a Button which can be used to generate an action from our textboxes
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(200,45)
$Button.Size = New-Object System.Drawing.Size(80,30)
$Button.Text = "FIND"
# Declare the action to occur when button clicked
$Button.Add_Click( { find } )
# Initialize the button inside the Form
$Form.Controls.Add($Button)
# Create a Function to make use of textboxes
function find {
#$outputBox.Clear()
$Results = Import-Csv -Path "D:\Excel\Data\List.csv" -Header "EventID", "TimeCreated", "Computer", "TargetUserName", "SubjectUserName", "HOSTName", "Filename" |
Where-Object {$_.TargetUserName -eq "inputBoxgmr"} |
Select-Object "EventID", "TimeCreated", "Computer", "TargetUserName", "SubjectUserName", "HOSTName", "Filename"
# Assign Result to OutputBox
$outputBox.Text = $Results
# Clear OutputBox
$inputBoxgmr.Clear()
}
# Create an Output textbox, 10 pixels in from Form Boundary and 150 pixels down
# As we want a multiline output set textbox size to 565 px x 200 px
# .Multiline declares the textbox is multi-line
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,100)
$outputBox.Size = New-Object System.Drawing.Size(565,450)
$outputBox.MultiLine = $True
$outputBox.Scrollbars = "Vertical"
# Initialize the textbox inside the Form
$Form.Controls.Add($OutputBox)
# Initialize Label
$Form.Controls.Add($Labelgmr)
# Initialize form and show it
# [void] used to suppress other messages generated by Form actions
[void] $Form.ShowDialog()
Output forms
Excel input data
output variables
I had wrote a powershell script to ping a ip address and I want to display a ping result to powershell form textboxt. I tried the below scripts below script. Script is working but not clear output.
Herewith i had attached the powershell output and powershell form textbox outputs.
[PowerShell output][1]
# Load required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Create Form to contain elements
$Form = New-Object System.Windows.Forms.Form
# Set Form Titlebar text
$Form.Text = "PING"
# Set size of form
# Size(<length>,<height>) in pixels
$Form.Size = New-Object System.Drawing.Size(600,600)
# Create an Input textbox
$inputBoxui = New-Object System.Windows.Forms.TextBox
$inputBoxui.Location = New-Object System.Drawing.Size(20,50)
$inputBoxui.Size = New-Object System.Drawing.Size(100,20)
# Initialize the textbox inside the Form
$Form.Controls.Add($inputBoxui)
# Create Instruction Label for inputBox
$Labelui = New-Object System.Windows.Forms.Label
$Labelui.Text = "Enter IP Address"
$Labelui.Location = New-Object System.Drawing.Size(20,30)
$Labelui.BackColor = "Transparent"
$Labelui.AutoSize = $true
# Initialize Label
$Form.Controls.Add($Labelui)
# Create an Output textbox, 10 pixels in from Form Boundary and 150 pixels down
# As we want a multiline output set textbox size to 565 px x 200 px
# .Multiline declares the textbox is multi-line
$outputBox = New-Object System.Windows.Forms.TextBox
$outputBox.Location = New-Object System.Drawing.Size(10,250)
$outputBox.Size = New-Object System.Drawing.Size(565,300)
$outputBox.MultiLine = $True
$outputBox.Scrollbars = "Vertical"
# Initialize the textbox inside the Form
$Form.Controls.Add($OutputBox)
# Add a Button which can be used to generate an action from our textboxes
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(20,80)
$Button.Size = New-Object System.Drawing.Size(100,40)
$Button.Text = "PING Start"
# Declare the action to occur when button clicked
$Button.Add_Click( { pingstart } )
# Initialize the button inside the Form
$Form.Controls.Add($Button)
# Create a Function
function pingstart {
$outputBox.Clear()
# Variable to store what user types into Input textbox
$Inputui = $inputBoxui.Text
while ($true) {
$Computer = $Inputui
$Ping = Test-Connection -Count 3 -ComputerName $Computer
ForEach ($Result in $Ping) {
If ($Result.ResponseTime -lt 100) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Green
}
If ( ($Result.ResponseTime -ge 100) -and ($Result.ResponseTime -lt 200) ) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Yellow
}
If ($Result.ResponseTime -ge 200) {
$Result | Select-Object -Property Address,BufferSize,ResponseTime | Write-Host -BackgroundColor Red
}
}
# Assign Result to OutputBox
$outputBox.Text = $Ping
}
}
# Initialize form and show it
# [void] used to suppress other messages generated by Form actions
[void] $Form.ShowDialog()
unrelated but it seems there is a mistake with this
Initialize the textbox inside the Form
$Form.Controls.Add($OutputBox)
caps lock.
$outputBox = New-Object System.Windows.Forms.TextBox
I have the following PowerShell script which displays file dialog to select a txt file. If user cancels dialog then provide a multiline text box
function GetDetails() {
Add-Type -AssemblyName System.Windows.Forms;
$browser = New-Object System.Windows.Forms.OpenFileDialog;
$browser.Filter = "txt (*.txt)|*.txt";
$browser.InitialDirectory = "E:\";
$browser.Title = "select txt file";
$browserResult = $browser.ShowDialog();
if($browserResult -eq [System.Windows.Forms.DialogResult]::OK) {
$nfoFile = $browser.FileName;
if([string]::IsNullOrWhiteSpace($txtFile)) {
return GetFromForm;
}
$txtFile = [System.IO.Path]::ChangeExtension($nfoFile, ".dac");
$txtFile = $temp + [System.IO.Path]::GetFileName($txtFile);
$exeArgs = "-f -S `"$txtFile`" -O `"$txtFile`"";
Start-Process $anExe -ArgumentList $exeArgs -Wait;
$result = Get-Content $txtFile | Out-String;
$browser.Dispose();
return $result;
} else {
return GetFromForm;
}
}
function GetFromForm(){
Add-Type -AssemblyName System.Windows.Forms;
$form = New-Object System.Windows.Forms.Form;
$form.Width = 800;
$form.Height = 600;
$txtBox = New-Object System.Windows.Forms.TextBox;
$txtBox.Multiline = $true;
$txtBox.AcceptsReturn = $true;
$txtBox.AcceptsTab = $true;
$txtBox.Visible = $true;
$txtBox.Name = "txtName";
$txtBox.Width = 760;
$txtBox.Height = 660;
$form.Controls.Add($txtBox);
$form.ShowDialog();
$form.Dispose();
return $txtBox.Text;
}
$desc = GetDetails;
cls;
Write-Host $desc;
Here I have two issues:
In Write-Host $desc, prints also Cancel hereiswhateverstrimg string if user chose to cancel dialog. How to avoid that?
If I run script in ISE, the generated form (in second function) will be always behind ISE even I call ShowDialog(), I expected to behave as modal dialog. It’s normal or there is a fix for this ?
Ok, there are a few changes that I made for efficiency and a few for functionality. Read the comments in the script for the explanations.
# Just add types once. There is no need to add the types in each function.
Add-Type -AssemblyName System.Windows.Forms;
Add-Type -AssemblyName System.Drawing
function GetDetails() {
$browser = New-Object System.Windows.Forms.OpenFileDialog;
$browser.Filter = "txt (*.txt)|*.txt";
$browser.InitialDirectory = "E:\";
$browser.Title = "select txt file";
$browserResult = $browser.ShowDialog();
# Combined the if statements
if($browserResult -eq [System.Windows.Forms.DialogResult]::OK -and [string]::IsNullOrWhiteSpace($txtFile) -ne $true) {
$nfoFile = $browser.FileName;
$txtFile = [System.IO.Path]::ChangeExtension($nfoFile, ".dac");
$txtFile = $temp + [System.IO.Path]::GetFileName($txtFile);
$exeArgs = "-f -S `"$txtFile`" -O `"$txtFile`"";
Start-Process $anExe -ArgumentList $exeArgs -Wait;
# The Raw flag should return a string
$result = Get-Content $txtFile -Raw;
$browser.Dispose();
return $result;
}
# No need for else since the if statement returns
return GetFromForm;
}
function GetFromForm(){
$form = New-Object System.Windows.Forms.Form;
$form.Text = 'Adding Arguments'
$form.Size = New-Object System.Drawing.Size(816,600)
$form.StartPosition = 'CenterScreen'
# Added a button
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(585,523)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$txtBox = New-Object System.Windows.Forms.TextBox;
$txtBox.Multiline = $true;
$txtBox.AcceptsReturn = $true;
$txtBox.AcceptsTab = $true;
$txtBox.Visible = $true;
$txtBox.Name = "txtName";
$txtBox.Size = New-Object System.Drawing.Size(660,500)
$form.Controls.Add($txtBox);
# Needed to force it to show on top
$form.TopMost = $true
# Select the textbox and activate the form to make it show with focus
$form.Add_Shown({$txtBox.Select(), $form.Activate()})
# Finally show the form and assign the ShowDialog method to a variable (this keeps it from printing out Cancel)
$result = $form.ShowDialog();
# If the user hit the OK button return the text in the textbox
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
return $txtBox.Text
}
}
$desc = GetDetails;
cls;
Write-Host $desc;
You can see reference material here: https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-6
You need to suppress output of $form.ShowDialog() in GetFromForm:
$form.ShowDialog()|out-null
Powershell will add to a return value everything that was outputted to host within a function/commandlet.
Regarding your second issue - see this answer
And please do not use semi-colon at an end of line. This is not C# and will confuse you into thinking that the line is ended here but it's not quite true.
I have been working on a scrip that generates a password with specific characters with a specific length, but with random numbers at a specific length.
The script has an GUI (it's a work in progress, I will finish it eventually).
The issue that I'm facing, is that, whenever I press "Generate Password", it creates a password, but it does not give me a new one after generating it. It just gives the same password that it generates it the first time.
I was looking on the web on how to retrieve a new password each time the button is pressed, but I did not found anything.
Can someone help with some tips?
Thank you.
The script is:
Function Button_Click()
{
[System.Windows.Forms.MessageBox]::Show($DefinedLetters)
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Password Generator"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Generate Password"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click({Button_Click})
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
# Password generator #
Function DefinedLetters
{
$DefinedLetters = 'Summer'
$numbers = 0..5
$array = #()
$array += $DefinedLetters.Split(',') | Get-Random -Count 4
$DefinedLetters += $numbers | Get-Random -Count 4
($DefinedLetters | Get-Random -Count $DefinedLetters.Count) -join ""
}
#Call the Function
Generate-Form
This isn't a great method of generating passwords, but here is a version of your code that produces a 'random' password each time using 4 letters from 'summer' and 4 numbers from (0,1,2,3,4,5):
# Password generator #
Function DefinedLetters {
$DefinedLetters = 'Summer'
$numbers = 0..5
$array = #()
$array += $DefinedLetters.ToCharArray() | Get-Random -Count 4
$array += $numbers | Get-Random -Count 4
($array | Get-Random -Count $array.Count) -join ""
}
Function Button_Click() {
$DefinedLetters = DefinedLetters
[System.Windows.Forms.MessageBox]::Show($DefinedLetters)
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build Form
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Password Generator"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# Add Button
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Generate Password"
$Form.Controls.Add($Button)
#Add Button event
$Button.Add_Click({Button_Click})
#Show the Form
$form.ShowDialog()| Out-Null
} #End Function
#Call the Function
Generate-Form
In Button_Click() you just refer to variable, which doesn't call function. You need to assign the value form function to variable like:
Function Button_Click()
{
$PW = DefinedLetters
[System.Windows.Forms.MessageBox]::Show($PW)
}
Your function DefinedLetters doesn't/can't work as you intended
You don't use the function but a variable to show.
Function Button_Click(){
[System.Windows.Forms.MessageBox]::Show((DefinedLetters))
}
Function Generate-Form {
#... snipped for brevity ...
}
Function DefinedLetters{
(([char[]]'Summer' | Get-Random -Count 4) -join '')+
((0..5| Get-Random -Count 4) -join '')
}
#Call the Function
Generate-Form
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