0

Good day,

I'm trying to create a default password base of the date that is the user is starting. For example, if the user starts on "11/10/2018", the password should be "Sep10th2018!"

this is what I have. It "works" but I'm sure there should be a better way to doing this. I just don't know how. Just in case, this is not so much of an issue. Is more for learning sake. I'm trying to be better at 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 
Yaboy
  • 29
  • 3
  • Where are you getting `$StartDate` from - an AD user property or somewhere else? Knowing this might help with discerning a more elegant solution – trebleCode Nov 15 '18 at 16:07
  • I put in manually into a variable like; $StartDate = Read-Host "StartDate?" – Yaboy Nov 15 '18 at 16:12

3 Answers3

2

Repurposing the elegant C# solution here, you can do it like this in 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)!"

In this case $password is set to May23rd2007!.

boxdog
  • 7,894
  • 2
  • 18
  • 27
2

Disclaimer: you probably already know, but I have to state that that's a very bad password, if you can even call it that


Most of this can be offloaded to the DateTime type, all except for the ordinal suffixes for the day of the month.

I would break it down into 3 steps:

  1. Parse input date
  2. Generate first half
  3. Calculate the suffix
  4. Append the year

# Parse the input date
$inputDate = Read-Host "Input date in the format MM/dd/yyyy"
$date = [datetime]::ParseExact($inputDate,'MM/dd/yyyy',$null)

# Generate first half 
$dateString = $date.ToString('MMMdd')

# Calculate the ordinal suffix
$day = $date.Day
if($day -in 1,21,31) {
    $dateString += 'st'
}
else if($day -in 2,22) {
    $dateString += 'nd'
}
else if($day -in 3,23) {
    $dateString += 'rd'
}
else {
    $dateString += 'th'
}

# Append the year:
$dateString += $date.ToString('yyyy!')
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
1

Using regex to get the day suffix, and format operator to build the password:

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
henrycarteruk
  • 12,708
  • 2
  • 36
  • 40