Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 密码的字符串操作_Powershell - Fatal编程技术网

Powershell 密码的字符串操作

Powershell 密码的字符串操作,powershell,Powershell,你好 我正在尝试创建用户开始日期的默认密码基础。例如,如果用户从“2018年10月11日”开始,则密码应为 “2018年9月10日!” 这就是我所拥有的。它“有效”,但我相信应该有更好的方法来做到这一点。我只是不知道怎么做。以防万一,这不是什么大问题。更多的是为了学习。我想在PowerShell做得更好 # Generate Default Password IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "1") { $Month

你好

我正在尝试创建用户开始日期的默认密码基础。例如,如果用户从“2018年10月11日”开始,则密码应为 “2018年9月10日!”

这就是我所拥有的。它“有效”,但我相信应该有更好的方法来做到这一点。我只是不知道怎么做。以防万一,这不是什么大问题。更多的是为了学习。我想在PowerShell做得更好

#   Generate Default Password
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "1") {
    $Month = "Jan"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "2") {
    $Month = "Feb"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "3") {
    $Month = "Mar"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "4") {
    $Month = "Apr"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "5") {
    $Month = "May"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "6") {
    $Month = "Jun"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "7") {
    $Month = "Jul"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "8") {
    $Month = "Aug"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "0" -and $StartDate[1] -eq "9") {
    $Month = "Sep"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "1" -and $StartDate[1] -eq "0") {
    $Month = "Oct"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}    
IF ($StartDate[0] -eq "1" -and $StartDate[1] -eq "1") {
    $Month = "Nov"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}   
IF ($StartDate[0] -eq "1" -and $StartDate[1] -eq "2") {
    $Month = "Dec"
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "1") {$Day = "1st"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "2") {$Day = "2nd"}
    IF ($StartDate[3] -eq "0" -eq $StartDate[4] -eq "3") {$Day = "3rd"} 
    Else {$Day = @($StartDate[3] + $StartDate[4] + "th")}
    $Year = @("20" + $StartDate[8] + $StartDate[9] + "!")
}

[string]$password2 = @($Month + $Day + $Year)
$password = $password2 | ConvertTo-SecureString -AsPlainText -Force
$password 
重新调整优雅的C#解决方案的用途,您可以在PowerShell中这样做:

    function Get-DaySuffix($day)
    {
        switch ($day)
        {
            {$day -in (1,21,31)} {"st" }
            {$day -in (2,22)} { "nd" }
            {$day -in (3,23)} { "rd" }
            default { "th" }
        }
    }

    # Change to some other means of getting date - e.g. Read-Host
    $startDate = Get-Date "23/05/2007"

    # Get a collection of the month names (e.g. Jan, Feb, mar, etc)
    $months = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.AbbreviatedMonthNames

    # Build the password from the date parts
    $password = "$($months[$startDate.Month-1])$($startDate.Day)$(Get-DaySuffix $startDate.Day)$($startDate.Year)!"
在这种情况下,
$password
设置为2007年5月23日

重新利用优雅的C#解决方案,您可以在PowerShell中这样做:

    function Get-DaySuffix($day)
    {
        switch ($day)
        {
            {$day -in (1,21,31)} {"st" }
            {$day -in (2,22)} { "nd" }
            {$day -in (3,23)} { "rd" }
            default { "th" }
        }
    }

    # Change to some other means of getting date - e.g. Read-Host
    $startDate = Get-Date "23/05/2007"

    # Get a collection of the month names (e.g. Jan, Feb, mar, etc)
    $months = [System.Globalization.CultureInfo]::CurrentCulture.DateTimeFormat.AbbreviatedMonthNames

    # Build the password from the date parts
    $password = "$($months[$startDate.Month-1])$($startDate.Day)$(Get-DaySuffix $startDate.Day)$($startDate.Year)!"
在这种情况下,
$password
设置为2007年5月23日

免责声明:您可能已经知道了,但我必须声明这是一个非常糟糕的密码,如果您可以这样称呼它的话


其中大部分都可以卸载到,除了月日的序号后缀

我将其分为三个步骤:

  • 解析输入日期
  • 生成上半部分
  • 计算后缀
  • 追加年份

  • 免责声明:您可能已经知道了,但我必须声明这是一个非常糟糕的密码,如果您可以这样称呼它的话


    其中大部分都可以卸载到,除了月日的序号后缀

    我将其分为三个步骤:

  • 解析输入日期
  • 生成上半部分
  • 计算后缀
  • 追加年份


  • 使用正则表达式获取日期后缀并生成密码:

    function Get-DateSuffix([datetime]$Date) {
        switch -regex ($Date.Day.ToString()) {
            '1(1|2|3)$' { 'th'; break }
            '.?1$'      { 'st'}
            '.?2$'      { 'nd'}
            '.?3$'      { 'rd'}
            default     { 'th'}
        }
    }
    
    [datetime]$StartDate = "11/10/2018"
    
    $password = "{0:MMM}{1}{2}{3}!" -f $StartDate, $StartDate.Day, (Get-DateSuffix $StartDate), $StartDate.Year
    

    使用正则表达式获取日期后缀并生成密码:

    function Get-DateSuffix([datetime]$Date) {
        switch -regex ($Date.Day.ToString()) {
            '1(1|2|3)$' { 'th'; break }
            '.?1$'      { 'st'}
            '.?2$'      { 'nd'}
            '.?3$'      { 'rd'}
            default     { 'th'}
        }
    }
    
    [datetime]$StartDate = "11/10/2018"
    
    $password = "{0:MMM}{1}{2}{3}!" -f $StartDate, $StartDate.Day, (Get-DateSuffix $StartDate), $StartDate.Year
    

    您从何处获得
    $StartDate
    -广告用户财产或其他地方?了解这一点可能有助于识别一个更优雅的解决方案,我手动将其放入变量中,如$StartDate=读取主机“StartDate?”您从哪里获得
    $StartDate
    来自广告用户属性或其他地方?了解这一点可能有助于识别一个更优雅的解决方案,我手动将其放入变量中,如$StartDate=读取主机“StartDate”?
    '1(1 | 2 | 3)$'{'th}
    是否应该中断以避免数组?是否应该
    '1(1 | 2 | 3)$'{'th}
    中断以避免数组?