Password Generator - Generate password on demand - powershell

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

Related

Dropdown selection in powershell script

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()

Text parameter not working with Powershell Progress Bar

I've been looking around for the past half hour on how to show the text inside of a GUI ProgressBar in Powershell, and everything I've tried has failed. I've even been referencing MSoft docs on it.
Am I doing something wrong? How do I add in the text?
This isn't my full script or exactly how I'll be using it - I just made an example so I could try to get it working.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Size = '500,300'
$form.StartPosition = 'CenterScreen'
$form.Topmost = $true
$computerList = 'server01', 'server02', 'server03', 'server04', 'server05', 'server06', 'server07', 'server08', 'server09', 'server10'
$progressbar1 = New-Object System.Windows.Forms.ProgressBar
$progressbar1.Size = '300, 20'
$progressbar1.Location = '20,60'
$progressbar1.Text = "Processing..."
$progressbar1.Maximum = $computerList.Count
$progressbar1.Step = 1
$progressbar1.Value = 0
foreach ($computer in $computerList){
$progressbar1.PerformStep()
}
$form.Controls.Add($progressbar1)
$form.ShowDialog()
I guess the easiest way is to have a Label control above the progressbar and update the text in there:
$progressLabel = New-Object System.Windows.Forms.Label
$progressLabel.Size = '300, 20'
$progressLabel.Location = '20,40'
$progressLabel.Text = "Processing..."
$form.Controls.Add($progressLabel)
foreach ($computer in $computerList){
$progressLabel.Text = "Doing stuff on computer '$computer'.."
$progressbar1.PerformStep()
# perform your action on $computer here
}

How to Display output to PowerShell form textbox data from PowerShell output

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

Powershell output in color

I am using a Powershell form to output some data and I am wondering how I can get the output in color?
I am not using write-host. That is not what I am looking for. I know you can use -ForegroundColor for that.
It's for Get-ADUser -Filter "UserPrincipalName -like 'Username'" | Select Enabled
If output is False it needs to be in Red. If output is true just regular color.
Anyone who can help me?
Many thanks.
Ralph.
A follow-up to my comment
#region Begin functions and code behind
function RunCode {
$ProcessList = (Get-Process).Name
If ($ProcessList -ge 10)
{$DataSet.ForeColor = 'red'}
else {$DataSet.ForeColor = 'black'}
[void] $DataSet.Items.Addrange($ProcessList)
}
#endregion End functions and code behind
#region Begin GUI code
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '511,501'
$Form.text = "Form"
$Form.TopMost = $false
$RunCode = New-Object system.Windows.Forms.Button
$RunCode.text = "RunCode"
$RunCode.width = 90
$RunCode.height = 30
$RunCode.location = New-Object System.Drawing.Point(19,17)
$RunCode.Font = 'Microsoft Sans Serif,10'
$DataSet = New-Object system.Windows.Forms.ListBox
$DataSet.text = "listBox"
$DataSet.width = 204
$DataSet.height = 144
$DataSet.location = New-Object System.Drawing.Point(17,98)
$Form.controls.AddRange(#(
$RunCode,
$DataSet
))
$RunCode.Add_Click({ RunCode })
#endregion Begin GUI code
# Call the GUI
[void]$Form.ShowDialog()

Powershell Not saving Value

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