Modify date in description field from user account

Welcome Forums General PowerShell Q&A Modify date in description field from user account

Viewing 16 reply threads
  • Author
    Posts
    • #216840
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      In AD we disable users and we add to the description field

      disabled by Paul on 2020-04-09 last workdate 2020-04-08

      however there are people that disable an account and put in the dates like dd-mm-yyyy or mm-dd-yyyyy or yyyy-mm-dd

      I would like to standardize this so that every description field gets disabled by Paul 2020-apr-09 last workdate 2020-apr-08

      only I don’t know how to achieve this
      basically this would mean that I read each description field into a variable change the date and put the corrected description back in the description field for the same user

      example current situation is  disabled by Paul on 2020-04-09 last workdate 2020-04-08
      after running the script disabled by Paul on 2020-apr-09 last workdate 2020-apr-08

      thanks for your help

      Paul

    • #216843
      Participant
      Topics: 3
      Replies: 317
      Points: 1,042
      Helping Hand
      Rank: Community Hero

      If you give it a shot, post what you tried and then maybe we can help you get it going.

    • #216861
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      however there are people that disable an account and put in the dates like dd-mm-yyyy or mm-dd-yyyyy or yyyy-mm-dd

      Hmmm … that’s nearly impossible to automate I’m afraid. How would you distinguish the 1st of February from the 2nd of January then? … or to stay with your example the 8th of April from the 4th of August?

    • #216864
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      I now have something like this

      $user = “samaccountname”
      $texttobemod = (get-aduser -identity $user -properties description).Description
      $content = Get-Content $texttobemod
      $content =$content -replace ‘(19|20)\d\d[- /.]([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])’, [datetime]::Today.ToString(‘yyyy-MMM-dd’)
      
      

      you got a point there Olaf, would working with select case do the trick? or would it still be impossible to determine?

       

    • #216876
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      you got a point there Olaf, would working with select case do the trick? or would it still be impossible to determine?

      I don’t understand this question.

      When I present you a date lets say “01/12/2020“. Would you be able to tell if it’s the 1st of December or the 12th of January? There are more than 10 month in between …. choose wisely. 😉 😀

      I mean of course there are a lot of dates you could determine exactly but if there is the possibilty of ambiguity you’d need a second attribute to support the choice you need to do.

    • #216879
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      Olaf,
      you are absolutely right we are not able to determine the difference here.
      I guess I have to leave the past behind and don’t care about the crappy data from the past.

      unless we can get hold of the date in the users object tab and use the Modified date as reference?

    • #216882
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      unless we can get hold of the date in the users object tab and use the Modified date as reference?

      That’s what I meant with “a second attribute“. 😉 It’ll make your code a lot more complex but if that’s what you need. 😉

    • #216885
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      as mentioned previously I’m stuck with the code that I have what do I need to do to make it work?

    • #216888
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      as mentioned previously I’m stuck with the code that I have what do I need to do to make it work?

      Hmmmm … that’s gonna be a little more complex than you might think of, I’m afraid. 😉
      You told that there are different formattings possible, right? So you would need to determine first, what format you have. Therefor you need to get the month and the date string, maybe turn it into an integer and check whether it’s one of the ambiguous ones. If not you could replace it right away if not you’d need to process further.
      Let’s say you have the 13th and the 14th of March in the description you could start with something like this:

      $ExampleSourceStringList = @(
      'disabled by Paul on 2020-03-14 last workdate 2020-03-13',
      'disabled by Paul on 03-14-2020 last workdate 03-13-2020',
      'disabled by Paul on 14-03-2020 last workdate 13-03-2020'
      )
      foreach($ExampleSourceString in $ExampleSourceStringList){
          Select-String -InputObject $ExampleSourceString -Pattern '(?<=\s|-|$)\d{2}-\d{2}(?=\s|-|$)' -AllMatches | 
              Select-Object -ExpandProperty Matches |
                  Select-Object -ExpandProperty Value
      }

      There likely might be more sophisticated solutions but I don’t have a better idea at the moment.

    • #216903
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      I would just use the date-recognition capability already built into Get-Date, as in:

      function FindAndFormat-DateTime {
      
      # https://powershell.org/forums/topic/modify-date-in-description-field-from-user-account/
      
          [CmdletBinding(ConfirmImpact='Low')] 
          Param(
              [Parameter(Mandatory=$false)][String[]]$Description = 'disabled by Paul on 2020-04-09 last workdate 2020-04-08',
              [Parameter(Mandatory=$false)][String]$Delimiter = ' ',
              [Parameter(Mandatory=$false)][String]$DesiredDateFormat = 'yyyy-MMM-dd'
          )
      
          Begin { }
      
          Process {
      
              foreach ($InputString in $Description) {
                  Write-Verbose "Processing string '$InputString'"
                  $Result = foreach ($Word in ($InputString -split $Delimiter)) {
                      try {
                          $IdentifiedDate = Get-Date $Word -Format $DesiredDateFormat -EA 1 
                          Write-Verbose "    Identified Date '$Word', reformatting as '$IdentifiedDate'"
                          $IdentifiedDate
                      } catch {
                          $Word
                      }
                  } 
                  $Result -join $Delimiter
              }
      
          } 
      
          End { }
      }
      

      Sample output:

      FindAndFormat-DateTime -Description  @(
      'disabled by Paul on 2020-03-14 last workdate 2020-03-13',
      'disabled by Paul on 03-14-2020 last workdate 03-13-2020',
      'disabled by Paul on 14-03-2020 last workdate 13-03-2020'
      ) -Verbose
      
      VERBOSE: Processing string 'disabled by Paul on 2020-03-14 last workdate 2020-03-13'
      VERBOSE:     Identified Date '2020-03-14', reformatting as '2020-Mar-14'
      VERBOSE:     Identified Date '2020-03-13', reformatting as '2020-Mar-13'
      disabled by Paul on 2020-Mar-14 last workdate 2020-Mar-13
      VERBOSE: Processing string 'disabled by Paul on 03-14-2020 last workdate 03-13-2020'
      VERBOSE:     Identified Date '03-14-2020', reformatting as '2020-Mar-14'
      VERBOSE:     Identified Date '03-13-2020', reformatting as '2020-Mar-13'
      disabled by Paul on 2020-Mar-14 last workdate 2020-Mar-13
      VERBOSE: Processing string 'disabled by Paul on 14-03-2020 last workdate 13-03-2020'
      disabled by Paul on 14-03-2020 last workdate 13-03-2020
      

      This function will take one or more input strings, parse it by the $Delimiter, and examine each piece to see if it can be recognized as [DateTime]. If so, it will reformat it as $DesiredDateFormat. It will then reassemble and return the string.

    • #216906
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      @Sam Wow … great solution. 2 out of 3 on a German Windows as well. Probably the real world hit rate will be even higher.

      Thank you very much

    • #216960
      Participant
      Topics: 12
      Replies: 519
      Points: 1,194
      Helping Hand
      Rank: Community Hero

      @Sam Wow … great solution. 2 out of 3 on a German Windows as well. Probably the real world hit rate will be even higher.

      Thank you very much

      Thanks Olaf 🙂

    • #216975
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      @Sam @Olaf sweet thanks for your help

    • #217014
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      found it

       

      • This reply was modified 1 month, 2 weeks ago by acer460527.
    • #217035
      Participant
      Topics: 3
      Replies: 317
      Points: 1,042
      Helping Hand
      Rank: Community Hero

      Something like

      
      FindAndFormat-DateTime -Description @(
      (get-aduser -identity $user -properties description).Description
      ) -Verbose
      
      
    • #217038
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Take a look at this:

      Search-ADAccount –AccountDisabled –UsersOnly  |
          ForEach-Object {
              $User = Get-ADUser -Identity $_.sAMAccountName -Properties Description 
              $ChangedDescription = FindAndFormat-DateTime -Description $User.Description
              Set-ADUser -Identity $_.sAMAccountName -Description $ChangedDescription
          }
    • #217047
      Participant
      Topics: 28
      Replies: 67
      Points: 375
      Rank: Contributor

      thanks guys
      I started to experiment with your examples and came up with this

      
      function FindAndFormat-DateTime {
      
      # https://powershell.org/forums/topic/modify-date-in-description-field-from-user-account/
      
      [CmdletBinding(ConfirmImpact=’Low’)]
      Param(
      
      [Parameter(Mandatory=$false)][String[]]$Description = ‘disabled by Paul on 2020-04-09 last workdate 2020-04-08’,
      [Parameter(Mandatory=$false)][String]$Delimiter = ‘ ‘,
      [Parameter(Mandatory=$false)][String]$DesiredDateFormat = ‘yyyy-MMM-dd’
      )
      
      Begin { }
      
      Process {
      
      foreach ($InputString in $Description) {
      Write-Verbose “Processing string ‘$InputString'”
      $Result = foreach ($Word in ($InputString -split $Delimiter)) {
      try {
      $IdentifiedDate = Get-Date $Word -Format $DesiredDateFormat -EA 1
      Write-Verbose ” Identified Date ‘$Word’, reformatting as ‘$IdentifiedDate'”
      $IdentifiedDate
      } catch {
      $Word
      }
      }
      
      $Result -join $Delimiter
      }
      
      }
      
      End { }
      }
      
      $users = import-csv c:\temp\toRemove.csv
      
      foreach ($user in $users) {
      $User = Get-ADUser -Identity $user.samAccountName -Properties Description
      $ChangedDescription = FindAndFormat-DateTime -Description $User.Description
      Set-ADUser -Identity $user.samAccountName -Description $ChangedDescription
      }
      
      
      • This reply was modified 1 month, 2 weeks ago by acer460527.
      • This reply was modified 1 month, 2 weeks ago by acer460527.
Viewing 16 reply threads
  • You must be logged in to reply to this topic.