Specifically with a Finnish locale, the casts to [datetime] fail with the following error:
InvalidArgument: Cannot convert value "2021-11-18 13.19.27" to type "System.DateTime". Error: "String '2021-11-18 13.19.27' was not recognized as a valid DateTime."
This is a strange issue in PowerShell itself. If you have a date object and convert it to a string like this:
$date_str = $date_object.ToString("yyyy-MM-dd HH:mm:ss")
Then when you attempt to cast it back to an object without specifying the format:
$date_value = [datetime]$date_str
You will get the same error when the locale settings use a period for time separator.
A quick fix is to use ParseExact everywhere, for example like this:
$roleAssignmentsCreated = ($rbacAll | Sort-Object -Property RoleAssignmentId, ObjectId -Unique).where( { -not [string]::IsNullOrEmpty($_.CreatedOn) -and [datetime]::ParseExact($_.CreatedOn, "yyyy-MM-dd HH:mm:ss", $null) -gt $xdaysAgo })
But that's not very clean and is causing a lot of converting in loops. Instead, it would be better to not convert the DateTime values to strings in the first place, for example around line 9420 this:
$createdOn = $customPolicySet.Json.properties.metadata.createdOn.ToString("yyyy-MM-dd HH:mm:ss")
could be left as an object, and then the value converted to string whenever needed for output.