Powershell -minimum似乎不起作用

时间:2014-02-24 18:07:52

标签: powershell

让我作为序言,注意这只是我与powershell的第三天,对不起,如果这是常识。

这是创建一个随机生成的密码,其中包含要求。我已经为每个值设置了最小值为2,但是在生成密码时,每个值的最小值都不是2。我想这可能是因为Get-Random -count 10正在从池中拉出字符串的其余部分。我不确定如何强制它创建一个10-12个字符的密码,并且每个要求的最小值都是如此。可以/我应该在创建密码后验证密码吗?

$ErrorActionPreference = "silentlycontinue"
function OnApplicationLoad {
    #Note: This function is not called in Projects
    #Note: This function runs before the form is created
    #Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path
    #Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)
    #Important: Form controls cannot be accessed in this func
    else{Stop-Process -name powershell.exe}
    return $true #return true for success or false for failure
}

function OnApplicationExit {
    #Note: This function is not called in Projects
    #Note: This function runs after the form is closed
    $script:ExitCode = 0 #Set the exit code for the Packager
}

#######Static Password Resources######

$caps = [char[]] "ABCDEFGHJKMNPQRSTUVWXY"
$lows = [char[]] "abcdefghjkmnpqrstuvwxy" 
$nums = [char[]] "2346789"
$spl = [char[]] "#%$+<=>?"
$ofs = ""

####################Starts code############################
function Call-test_pff {
    $form1 = New-Object 'System.Windows.Forms.Form'
    $form1.ClientSize = '514, 640'
    $form1.Name = "form1"
    $form1.Text = "Password tool"
    $form1.add_Load($form1_Load)

#Add Icon to window
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$form1.icon = $Icon

#form1: Button1 "Generate password" 
$button1 = New-Object system.windows.forms.button
$button1.location = "10, 25"
$button1.size = "125, 35"
$button1.text = "Generate password"
$button1.add_click({
    $first = Get-Random -Minimum 2 
    $second = Get-Random -Minimum 2
    $third = Get-Random -Minimum 2 
    $fourth = Get-Random -Minimum 2
    $pwd = [string](@($nums | Get-Random -Count $first) + @($lows | Get-Random -Count $second) + @($caps | Get-Random -Count $third) + @($spl | Get-Random -Count $fourth) | Get-Random -Count 10)
    $textbox1.text = $pwd
})
$form1.controls.add($button1)

#form1: Label4: "Password"
$label4 = New-Object system.windows.forms.label
$label4.location = "10, 475"
$label4.size = "200, 20"
$label4.text = "Password:"
$form1.controls.add($label4)

#form1: password box "Password"
$textbox1 = New-Object system.windows.forms.textbox
$textbox1.location = "10, 500"
$textbox1.size = "200, 30"
$textbox1.multiline = $true
$textbox1.readonly = $true
$form1.controls.add($textbox1)

#####################Stop Code Here#####################

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Clean up the control events
$form1.add_FormClosed($Form_Cleanup_FormClosed)
#Show the Form
return $form1.ShowDialog()

} #End Function

#Call OnApplicationLoad to initialize
if((OnApplicationLoad) -eq $true)
{
    #Call the form
    Call-test_pff | Out-Null
    #Perform cleanup
    OnApplicationExit
}

1 个答案:

答案 0 :(得分:6)

假设您的问题出在此代码中:

$first = Get-Random -Minimum 2 
$second = Get-Random -Minimum 2
$third = Get-Random -Minimum 2 
$fourth = Get-Random -Minimum 2
$pwd = [string](@($nums | Get-Random -Count $first) + @($lows | Get-Random -Count $second) + @($caps | Get-Random -Count $third) + @($spl | Get-Random -Count $fourth) | Get-Random -Count 10)

您有两个相关问题:

  1. 您没有指定get-random的最大值,因此在大多数情况下您获得的数字非常大。因此,$nums|get-random -count $first总是会返回整个数组(但是以随机顺序) - 您要求从少于10的列表中随机收集(例如)1000个选项。
  2. 由于上述情况,您的跟踪get-random -count 10正在从整个搜索空间中挑选10个随机字符($caps$lows$lows$spl
  3. 所以,你需要做两件事:

    1. 通过将get-random限制为最小最大值,获取每种字符类型的子集。
    2. 确保在最后一行中获得正确的字符分布。目前的最后一行无法做到这一点,因为从理论上讲,如果随机分布下降,你可能会得到10个大写字符
    3. 要解决第一个问题,请将计数限制为这些源数组的长度:

      $caps = [char[]] "ABCDEFGHJKMNPQRSTUVWXY"
      $lows = [char[]] "abcdefghjkmnpqrstuvwxy" 
      $nums = [char[]] "2346789"
      $spl = [char[]] "#%$+<=>?"
      $ofs = ""
      $first = Get-Random -Minimum 2 -Maximum $nums.Length;
      $second = Get-Random -Minimum 2 -Maximum $lows.Length;
      $third = Get-Random -Minimum 2 -Maximum $caps.Length;
      $fourth = Get-Random -Minimum 2 -Maximum $spl.Length;
      

      但这仍然无法解决第二个问题,因为您将超过50个字符传递到最终get-random -count 10`,非常容易排除一个或多个必需的字符类型。

      如果您可以处理每种类型字符的设定数量(在这种情况下,2个数字,3个上部,3个较低,2个特殊),请执行以下操作:

      $caps = [char[]] "ABCDEFGHJKMNPQRSTUVWXY"
      $lows = [char[]] "abcdefghjkmnpqrstuvwxy" 
      $nums = [char[]] "2346789"
      $spl = [char[]] "#%$+<=>?"
      $ofs = ""
      
      $first = $nums | Get-Random -count 2;
      $second = $caps | Get-Random -count 3;
      $third = $lows | Get-Random -count 3;
      $fourth = $spl | Get-Random -count 2;
      
      $pwd = [string](@($first) + @($second) + @($third) + @($fourth) | Get-Random -Count 10)
      $pwd
      

      这会从每个组中选择正确数量的随机项目,然后随机化这些字符的顺序。