Hey Guys and Gals. I’m needing a little help. I’m working on a scrip with a colleague for work, and we get it to export to CSV from a text and everything except when exporting to CSV, it will not give me the user name last logged on, to the computer name in text. Any help would be appreciated.

#GET USER LAST LOGON TO REMOTE COMPUTERS

New-Item -ItemType file -path “$HOME\Workstations.txt”

Invoke-Item “$HOME\Workstations.txt”

$Shell = New-Object -ComObject “WScript.Shell”

$Button = $Shell.Popup(“Enter the Workstations names to check and click OK to continue.”, 0, “Script Paused”, 0)

Get-Content “$HOME\Workstations.txt” | %{ Get-WMIObject Win32_ComputerSystem -ComputerName $_ -ErrorVariable WMIError -ErrorAction SilentlyContinue

If WMIError has a value an error occurred.

Create an object holding the Computer Name and an empty Username property for the output

If ($WMIError) { $_ | Select-Object @{n=‘Name’;e={ $_ }}, Username }} | Select-Object Name, @{n=‘Username’;e={ $_.Username -Replace “.*\” }} |

Export-CSV “$HOME\LastLogon.csv”

Invoke-Item “$HOME\LastLogon.csv”

Remove-Item “$HOME\Workstations.txt”

We know there is an issue, but we cant seem to get it to execute the Last Logon User Name in the CSV. can anyone check it for us and see where we are missing something? we are still learners in PS, so anyone would be appreciated.

4 Spice ups

If you post code, please use the ‘Insert Code’ button. Please and thank you!

codebutton_small.png

check this out

https://community.spiceworks.com/scripts/show/4408-get-logged-in-users-remote-computers-or-local

1 Spice up

Thanks guys. But it seems to be working now. not sure what happened, but it is pulling user names. now all i need is to just figure out how to add date/time stamp.

just add another calculated value to your select object

Select-Object Name, @{n='Username';e={ $_.Username -Replace ".*\\" }},@{n='TimeStamp';e={get-date -format g}}

Neally,

I did do that earlier, but for some reason (even trying your script) it is only resulting in PS, it is not exporting the CSV file anymore.

Did you add the pipe ( | ) symbol after it? and then piped it to export-csv ?

AH, Thank you. I did forget the Pipe Symbol. everything is now showing on the CSV file. Its showing Current Date/Time Stamp though, not last Date/Time of last user. I think I know what to put on there, I beleive I can run:

Select CN,@{n="lastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}}

@alexw

yeah you asked for timestamp,. so i thought you meant current one, you are right on.

Sorry, that’s wrong I believe its,

@{n="lastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}}

I will keep playing with it. It does come up with date/time stamp, but it is showing from Dec 31st 1600 @ 7:00M… woops.

@alexw

@alexw ​ Thanks for your Help. You have been awesome.

I’m sorry I keep coming back (haha) but it looks like i’m def stuck with this last part. I cant seem to get it to show my the last login Date / Time Stamp. every way i do it, i keep getting the date above. if you have thoughts, would appreciate it.

What is the lastLogonTimestamp string that you are getting?

@alexw

everything seems to work fine when converting to the csv, but when csv shows up, the date is


12/31/1600 7:00:00 PM

here is the full thing: (and only thing i added was what we previously said.)

If ($WMIError) { $_ | Select-Object @{n='Name';e={ $_ }}, Username }} | Select-Object Name, @{n='Username';e={ $_.Username -Replace ".*\\" }} | Select-Object Name, @{n='Username';e={ $_.Username -Replace ".*\\" }},@{n="LastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}} |

That means the lastLogonTimestamp attribute is null (empty).

@alexw

OKay. Somewhere im missing something, I know when i had the:

"Select-Object Name, @{n='Username';e={ $_.Username -Replace ".*\\" }},@{n='TimeStamp';e={get-date -format g}}"

It worked, but was giving me the current/modified time of the user logged in at the time. so im trying to work on getting the last known logon user with the last known logon date and time. I may have to keep researching and go back to see whats missing/wrong. you h ave been very helpful to me, thank you.

please stop tagging me, there is no need for it as I’m in the conversation.

Select-Object Name, @{n='Username';e={ $_.Username -Replace ".*\\" }},@{n='TimeStamp';e={get-date -format g}}

this works as it is getting the current timedate information (as in the moment you run the script)

12/31/1600 7:00:00 PM 

this is a valid results and means the user never logged in , as there is no value, there is no timestamp in AD indicating that it did.
Where are you pulling that information from?
“Get-WMIObject Win32_ComputerSystem” does not have a timestamp in it, is there something else you are running that is not in your post?

No, nothing else. We are not running through AD because it is no accurate. it is delayed by days. I think we are going to redo a new script for this. but as example. in AD it is delayed a few days, telling me my last login was the 5th of August. but when i run

$computer = 'insertname'
 
Get-ChildItem "\\$computer\c$\Users" | Sort-Object LastWriteTime -Descending | Select-Object Name, LastWriteTime -first 1

It works in real time, stating I was the last person logged in to my machine specifically, at the current time.

oh the last logon timestamp is the assumption of the last write time of the user profile folder?

Exactly. Yes.