0

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
excedentH7
  • 15
  • 2
  • 5
  • `$DefinedLetters` is a single valued `String`, you need to provide an collection if you use the `-count` parameter. For a password generator example see: https://stackoverflow.com/a/37275209/1701026 – iRon Apr 15 '19 at 13:33
  • You should look into designing GUIs with XAML, it's much easier. :) – Jacob Colvin Apr 15 '19 at 15:29

3 Answers3

0

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)
}
nemze
  • 111
  • 4
0

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
boxdog
  • 7,894
  • 2
  • 18
  • 27
0
  1. Your function DefinedLetters doesn't/can't work as you intended
  2. 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

enter image description here