vendredi 16 mars 2018

PowerShell GUI with IF/Else elements to change outcome

I was working on a password generator and the team I made it for requested a GUI with options to either include or exclude special characters and number separately. I have most of the work done and everything works except it does not seem to care if a checkbox is checked or not. This is important since this is what I chose to decide if the password would include special characters or numbers.

Am I missing something? Did I make a mistake in my logic? Any help on getting this functioning would be great. I included the PowerShell script below.

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()



$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = '374,312'
$Form.text                       = "DNR Password Generator"
$Form.BackColor                  = "#6873a1"
$Form.TopMost                    = $false
$Form.icon                       = '\\central\CAES\TS_ALL\Standard Operating Procedures - BTS\Security\Accounts Administration\Password-Formats-Policies\Password Gen\if_simpline_55_2305607.ico'

$Groupbox1                       = New-Object system.Windows.Forms.Groupbox
$Groupbox1.height                = 81
$Groupbox1.width                 = 310
$Groupbox1.location              = New-Object System.Drawing.Point(28,52)
$Groupbox1.Text                  = "Password Complexity"
$Groupbox1.Font                  = 'Calibri,12,style=Bold'

$btn                             = New-Object system.Windows.Forms.Button
$btn.text                        = "Generate Password"
$btn.width                       = 311
$btn.height                      = 37
$btn.location                    = New-Object System.Drawing.Point(28,245)
$btn.Font                        = 'Calibri,12,style=Bold'
$btn.add_click({$OutputBox.Text = Gen-Password}) 

$Char                            = New-Object system.Windows.Forms.CheckBox
$Char.text                       = "Special Characters"
$Char.AutoSize                   = $false
$Char.width                      = 150
$Char.height                     = 20
$Char.location                   = New-Object System.Drawing.Point(15,40)
$Char.Font                       = 'Calibri,12,style=Bold'

$Num                             = New-Object system.Windows.Forms.CheckBox
$Num.text                        = "Numbers"
$Num.AutoSize                    = $false
$Num.width                       = 95
$Num.height                      = 20
$Num.location                    = New-Object System.Drawing.Point(189,40)
$Num.Font                        = 'Calibri,12,style=Bold'

$OutputBox                       = New-Object system.Windows.Forms.TextBox
$OutputBox.multiline             = $false
$OutputBox.width                 = 310
$OutputBox.height                = 20
$OutputBox.location              = New-Object System.Drawing.Point(28,204)
$OutputBox.Font                  = 'Calibri,12,style=Bold'
$OutputBox.ForeColor             = "#b22b2b"

$Label1                          = New-Object system.Windows.Forms.Label
$Label1.text                     = "Number of Characters"
$Label1.AutoSize                 = $true
$Label1.width                    = 25
$Label1.height                   = 10
$Label1.location                 = New-Object System.Drawing.Point(28,166)
$Label1.Font                     = 'Calibri,12,style=Bold'

$lenpass                         = New-Object system.Windows.Forms.TextBox
$lenpass.multiline               = $false
$lenpass.text                    = "10"
$lenpass.width                   = 29
$lenpass.height                  = 20
$lenpass.location                = New-Object System.Drawing.Point(307,160)
$lenpass.Font                    = 'Calibri,12,style=Bold'


$Form.controls.AddRange(@($Groupbox1,$btn,$OutputBox,$Label1,$lenpass))
$Groupbox1.controls.AddRange(@($Char,$Num))
$drc = $form.ShowDialog()



Function MakeUp-String1([Int]$Size = $lenpass.text, [Char[]]$CharSets = "UL"){
    $Chars = @(); $TokenSet = @()
    If (!$TokenSets) {$Global:TokenSets = @{
        U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                #Upper case
        L = [Char[]]'abcdefghijklmnopqrstuvwxyz'                                #Lower case

    }}
    $CharSets | ForEach {
        $Tokens = $TokenSets."$_" | ForEach {If ($Exclude -cNotContains $_) {$_}}
        If ($Tokens) {
            $TokensSet += $Tokens
            If ($_ -cle [Char]"Z") {$Chars += $Tokens | Get-Random}             #Character sets defined in upper case are mandatory
        }
    }
    While ($Chars.Count -lt $Size) {$Chars += $TokensSet | Get-Random}
    ($Chars | Sort-Object {Get-Random}) -Join ""                                #Mix the (mandatory) characters and output string
}; 

Function MakeUp-String2([Int]$Size = $lenpass.text, [Char[]]$CharSets = "ULN"){
    $Chars = @(); $TokenSet = @()
    If (!$TokenSets) {$Global:TokenSets = @{
        U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                #Upper case
        L = [Char[]]'abcdefghijklmnopqrstuvwxyz'                                #Lower case
        N = [Char[]]'0123456789'                                                #Numerals
    }}
    $CharSets | ForEach {
        $Tokens = $TokenSets."$_" | ForEach {If ($Exclude -cNotContains $_) {$_}}
        If ($Tokens) {
            $TokensSet += $Tokens
            If ($_ -cle [Char]"Z") {$Chars += $Tokens | Get-Random}             #Character sets defined in upper case are mandatory
        }
    }
    While ($Chars.Count -lt $Size) {$Chars += $TokensSet | Get-Random}
    ($Chars | Sort-Object {Get-Random}) -Join ""                                #Mix the (mandatory) characters and output string
}; 

Function MakeUp-String3([Int]$Size = $lenpass.text, [Char[]]$CharSets = "ULS", [Char[]]$Exclude =('^*()_-+={}[]\|;:`~''''",<>./?@#$')){
    $Chars = @(); $TokenSet = @()
    If (!$TokenSets) {$Global:TokenSets = @{
        U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                #Upper case
        L = [Char[]]'abcdefghijklmnopqrstuvwxyz'                                #Lower case
        S = [Char[]]'!%&?'                                                      #Symbols
    }}
    $CharSets | ForEach {
        $Tokens = $TokenSets."$_" | ForEach {If ($Exclude -cNotContains $_) {$_}}
        If ($Tokens) {
            $TokensSet += $Tokens
            If ($_ -cle [Char]"Z") {$Chars += $Tokens | Get-Random}             #Character sets defined in upper case are mandatory
        }
    }
    While ($Chars.Count -lt $Size) {$Chars += $TokensSet | Get-Random}
    ($Chars | Sort-Object {Get-Random}) -Join ""                                #Mix the (mandatory) characters and output string
};

Function MakeUp-String4([Int]$Size = $lenpass.text, [Char[]]$CharSets = "ULNS", [Char[]]$Exclude =('^*()_-+={}[]\|;:`~''''",<>./?@#$')){
    $Chars = @(); $TokenSet = @()
    If (!$TokenSets) {$Global:TokenSets = @{
        U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'                                #Upper case
        L = [Char[]]'abcdefghijklmnopqrstuvwxyz'                                #Lower case
        N = [Char[]]'0123456789'                                                #Numerals
        S = [Char[]]'!%&?'                                                      #Symbols
    }}
    $CharSets | ForEach {
        $Tokens = $TokenSets."$_" | ForEach {If ($Exclude -cNotContains $_) {$_}}
        If ($Tokens) {
            $TokensSet += $Tokens
            If ($_ -cle [Char]"Z") {$Chars += $Tokens | Get-Random}             #Character sets defined in upper case are mandatory
        }
    }
    While ($Chars.Count -lt $Size) {$Chars += $TokensSet | Get-Random}
    ($Chars | Sort-Object {Get-Random}) -Join ""                                #Mix the (mandatory) characters and output string
};



Aucun commentaire:

Enregistrer un commentaire