具有IF / Else元素的PowerShell GUI可以改变结果

时间:2018-03-16 18:39:49

标签: function powershell user-interface if-statement checkbox

我正在开发一个密码生成器,我所做的团队是为了请求一个GUI,其中包含分别包含或排除特殊字符和数字的选项。我已完成大部分工作,一切正常,但似乎并不关心是否选中了复选框。这很重要,因为我选择决定密码是否包含特殊字符或数字。

我错过了什么吗?我的逻辑错了吗?

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


$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 Gen-Password () {
If  ($Num.Checked -eq $False -and $Char.Checked -eq $False ) {MakeUp-String1}
elseif ($Num.Checked -eq $True -and $Char.Checked -eq $False) {MakeUp-String2}
elseif ($Char.Checked -eq $True -and $Num.Checked -eq $False ) {MakeUp-String3}
else {MakeUp-String4}

};

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
};

1 个答案:

答案 0 :(得分:1)

首先,您必须将$Num.Checked$Char.Checked定义为$true

我还发现我必须先把所有的功能都放在一边。

编辑代码:

Function Gen-Password () {
If  ($Num.Checked -eq $False -and $Char.Checked -eq $False ) {MakeUp-String1}
elseif ($Num.Checked -eq $True -and $Char.Checked -eq $False) {MakeUp-String2}
elseif ($Char.Checked -eq $True -and $Num.Checked -eq $False ) {MakeUp-String3}
else {MakeUp-String4}

};

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
};

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


$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'
$Char.Checked                    = 'true'

$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'
$Num.Checked                     = 'true'

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