question

RisingFlight-7863 avatar image
0 Votes"
RisingFlight-7863 asked RichMatheisen-8856 commented

Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

Hi All i am using the below syntax to export all my users from an OU i am not getting output at last login. i am using below syntax and at last login i am getting output as Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

 Get-ADUser -Filter * -SearchBase "OU=OU1,DC=contoso,DC=com" -properties * | Select-Object Name,Description, UserprincipalName,SamAccountName,LastLogin | export-csv C:\temp\output.csv -Encoding UTF8 -NoTypeInformation


windows-serverwindows-server-powershellwindows-active-directorywindows-server-2019windows-server-2016
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

No that won't work. The lastLogon property isn't in a format that is understood by PowerSell in its raw format.

This should, though:

 Get-ADUser -Filter * -SearchBase "OU=OU1,DC=contoso,DC=com" -properties * | Select-Object Name,Description,UserprincipalName,SamAccountName,@{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}} | 
  export-csv C:\temp\output.csv -Encoding UTF8 -NoTypeInformation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

First, the property name is "lastlogOn", not "lastlogIn".
Second, the value in that propertry is not a PowerShell DateTime object. It requires conversion (like this, assuming $_ represents the AD user object):

 $lastLogon = [datetime]::FromFileTime($_.lastLogon)

Third, the lastlogon property is not replicated. It represents the last logon time on THIS domain controller. If you want the most recent logon time you'll have to query each DC and select the most recent value.
Fourth, if you have only a single DC you can avoid the need to convert the value and use the LastLogonDate property instead. Do NOT use this property if you have more than one DC because although it's replicated, the replication interval is a random period between 9 and 14 days -- so you'd still have to look at each DC and select the most recent time.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RisingFlight-7863 avatar image
0 Votes"
RisingFlight-7863 answered
 Get-ADUser -Filter * -SearchBase "OU=OU1,DC=contoso,DC=com" -properties * | Select-Object Name,Description, UserprincipalName,SamAccountName,lastLogon | export-csv C:\temp\output.csv -Encoding UTF8 -NoTypeInformation

i believe above syntax should work.?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RisingFlight-7863 avatar image
0 Votes"
RisingFlight-7863 answered RichMatheisen-8856 commented

i am getting hash error, not sure what mistake i am making

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry about that. That's what comes from coding on the fly!

I corrected the code sample. Try it again.

0 Votes 0 ·
RisingFlight-7863 avatar image
0 Votes"
RisingFlight-7863 answered RichMatheisen-8856 commented

i am getting below error

Select-Object : A positional parameter cannot be found that accepts argument 'System.Object[]'.
At line:1 char:126
+ ... perties * | Select-Object Name,Description,UserprincipalName,SamAccou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry! That was a cut-and-paste error in correcting the code sample. The correct "Select-Object" was added to the end of the existing Select-Object instead of replacing the bad one. I fixed that (I hope!).

0 Votes 0 ·