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
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?
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.
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?
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.
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.
[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’
)