some properties are null inside parallel foreach block

Lan, John 81 Reputation points
2023-03-25T02:36:18.07+00:00

For below sample code, $_.lastLogonTimestamp is always Null. Any idea? Most other properties can be retrieved OK.

$users=get-aduser -filter * -properties lastLogonTimestamp
$users|foreach -parallel{
    $_.lastLogonTimestamp                     # this will not show
    $null -eq $_.lastLogonTimestamp            # this will show as True, even though where it is NOT null
    $_.samAccountName                         # this always shows correctly
}

If I remove keyword "parallel", then above code shows every properties properly, including lastLogonTimestamp

$users=get-aduser -filter * -properties lastLogonTimestamp
$users|foreach {
    $_.lastLogonTimestamp                     # this will show
    $null -eq $_.lastLogonTimestamp            # this will show as False
    $_.samAccountName                         # this always shows correctly
}

Edit: corrected code snippet where I missed attribute list for -properties parameter

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,364 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Rich Matheisen 44,776 Reputation points
    2023-03-26T15:04:32.1933333+00:00

    I don't think it "can return null due to the way it is stored in Active Directory". It can return "0" if the property has not yet been replicated to other DCs. The replication interval from the time the property is updated is 14 days +/- a random percentage of 5 days. https://learn.microsoft.com/en-us/windows/win32/adschema/a-lastlogontimestamp

    For a new user that would mean that only the DC that managed the logon would have that information for at least two weeks. For accurate information you have to query each DC in the users' domain and retain the most recent date.

    @Sedat SALMAN is correct about the need to specify the LastLogonDate in the Get-ADUser cmdlet. The default set of 10 properties returned by that cmdlet are:

    • DistinguishedName
    • Enabled
    • GivenName
    • Name
    • ObjectClass
    • ObjectGUID
    • SamAccountName
    • SID
    • Surname
    • UserPrincipalName
    1 person found this answer helpful.

  2. Sedat SALMAN 13,160 Reputation points
    2023-03-25T20:21:21.98+00:00

    In PowerShell, when using the parallel foreach loop, it creates separate threads to run the code for each item in the collection. This can cause issues when accessing certain properties, such as lastLogonTimestamp, which can return null due to the way it is stored in Active Directory.

    To work around this issue, you can use the -Properties parameter of the Get-ADUser cmdlet to specify the properties you need and force PowerShell to retrieve them. For example:

    
    $users = Get-ADUser -Filter * -Properties lastLogonTimestamp, samAccountName -ResultSetSize $null
    $users | ForEach-Object -Parallel {
        $_.lastLogonTimestamp
        $null -eq $_.lastLogonTimestamp
        $_.samAccountName
    }
    
    

  3. Lan, John 81 Reputation points
    2023-03-27T11:50:55.38+00:00

    for anyone have same problem, I also asked same question on Stackoverflow.

    https://stackoverflow.com/questions/75851412/powershell-foreach-object-parallel-not-all-properties-of-piped-in-object-are-a

    The adapter used by the Active Directory Module is not loaded automatically in the parallel runspaces which is why the property values are displayed as null by default. The solutions are either to force evaluation of all properties prior to passing the objects to the parallel runspaces

    0 comments No comments