PSU API; Having trouble with the output returned in a PowerShell console?

Product: PowerShell Universal
Version: 2.12.4

Hi everyone,

I have a script I put together that I in turn created an API from in our PSU server. The script returns data from Active Directory concerning users whos passwords are going to expire within 7 days. I’ve noticed when running the script in say, the ISE, the data returned looks good. But when I run the Invok-RestMethod one of the keys in my hashtable that gets returned is coming out a bit funky:

I have an ExpiryDate property created by doing the following for each AD user:

@{Name=“ExpiryDate”;Expression={[datetime]::FromFileTime($_.“msDS-UserPasswordExpiryTimeComputed”)}}

When running the script in PowerShell or the ISE the property is returned as:

ExpiryDate : 9/5/2022 11:32:35 AM

When I invoke that same script as an API, I get:

ExpiryDate : @{value=2022-09-04T19:06:30.9027519-04:00; DateTime=Sunday, September 4, 2022 7:06:30 PM}

Is there a way I can call the DateTime= section of this key?

Ugh sorry, I should have simply looked at it a bit further! I got it to work by doing the following in the hashtable that is created:

foreach ($user in $userList) {
$CN = $user | Select-Object -ExpandProperty CanonicalName
$parentOU = Split-Path -Path $CN -Parent

$eachUser.Description    = $user.Description
$eachUser.Email          = $user.UserPrincipalName
$eachUser.ExpiryDate     = $user.ExpiryDate.datetime
$eachUser.Name           = $user.Name
$eachUser.OU             = ($parentOU.Replace("\","/")).Split("/")[2]
$eachUser.Sam            = $user.SamAccountName

$importDetails = New-Object -TypeName PSObject -Property $eachUser

$allUsers += $importDetails

}

By adding the .datetime to the $eachUser.ExpiryDate = $user.ExpiryDate key/value it fixed the issue.

1 Like