\"Select-Object Name, @{n='Username';e={ $_.Username -Replace \".*\\\\\" }},@{n='TimeStamp';e={get-date -format g}}\"\n<\/code><\/pre>\nIt 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.<\/p>","upvoteCount":0,"datePublished":"2019-08-07T17:05:44.000Z","url":"https://community.spiceworks.com/t/need-help-with-this-powershell-script/724456/16","author":{"@type":"Person","name":"tonyearle","url":"https://community.spiceworks.com/u/tonyearle"}},{"@type":"Answer","text":"
please stop tagging me, there is no need for it as I’m in the conversation.<\/p>\n
Select-Object Name, @{n='Username';e={ $_.Username -Replace \".*\\\\\" }},@{n='TimeStamp';e={get-date -format g}}\n<\/code><\/pre>\nthis works as it is getting the current timedate information (as in the moment you run the script)<\/p>\n
12/31/1600 7:00:00 PM \n<\/code><\/pre>\nthis 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. \nWhere are you pulling that information from? \n“Get-WMIObject Win32_ComputerSystem” does not have a timestamp in it, is there something else you are running that is not in your post?<\/p>","upvoteCount":0,"datePublished":"2019-08-07T17:16:16.000Z","url":"https://community.spiceworks.com/t/need-help-with-this-powershell-script/724456/17","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
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<\/p>\n
$computer = 'insertname'\n \nGet-ChildItem \"\\\\$computer\\c$\\Users\" | Sort-Object LastWriteTime -Descending | Select-Object Name, LastWriteTime -first 1\n<\/code><\/pre>\nIt works in real time, stating I was the last person logged in to my machine specifically, at the current time.<\/p>","upvoteCount":0,"datePublished":"2019-08-07T17:26:37.000Z","url":"https://community.spiceworks.com/t/need-help-with-this-powershell-script/724456/18","author":{"@type":"Person","name":"tonyearle","url":"https://community.spiceworks.com/u/tonyearle"}},{"@type":"Answer","text":"
oh the last logon timestamp is the assumption of the last write time of the user profile folder?<\/p>","upvoteCount":0,"datePublished":"2019-08-07T18:00:22.000Z","url":"https://community.spiceworks.com/t/need-help-with-this-powershell-script/724456/19","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
Exactly. Yes.<\/p>","upvoteCount":0,"datePublished":"2019-08-07T18:26:29.000Z","url":"https://community.spiceworks.com/t/need-help-with-this-powershell-script/724456/20","author":{"@type":"Person","name":"tonyearle","url":"https://community.spiceworks.com/u/tonyearle"}}]}}
tonyearle
(tonyearle)
August 7, 2019, 12:59pm
1
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
Neally
(Neally)
August 7, 2019, 1:08pm
2
If you post code, please use the ‘Insert Code’ button. Please and thank you!
Hi, and welcome to the PowerShell forum!
Don’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!
Use a descriptive subject. Don’t say “Need help” or “PowerShell Help”, actually summarize what the problem is. It helps the rest of us keep track of which problem is which.
Don’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy…
jitensh
(JitenSh)
August 7, 2019, 1:08pm
3
1 Spice up
tonyearle
(tonyearle)
August 7, 2019, 1:56pm
4
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.
Neally
(Neally)
August 7, 2019, 2:01pm
5
just add another calculated value to your select object
Select-Object Name, @{n='Username';e={ $_.Username -Replace ".*\\" }},@{n='TimeStamp';e={get-date -format g}}
tonyearle
(tonyearle)
August 7, 2019, 2:38pm
6
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.
Neally
(Neally)
August 7, 2019, 2:40pm
7
Did you add the pipe ( | ) symbol after it? and then piped it to export-csv ?
tonyearle
(tonyearle)
August 7, 2019, 2:50pm
8
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
Neally
(Neally)
August 7, 2019, 2:58pm
9
yeah you asked for timestamp,. so i thought you meant current one, you are right on.
tonyearle
(tonyearle)
August 7, 2019, 3:01pm
10
Sorry, that’s wrong I believe its,
@{n="lastLogonDate";e={[datetime]::FromFileTime($_.lastLogonTimestamp)}}
tonyearle
(tonyearle)
August 7, 2019, 3:11pm
11
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
tonyearle
(tonyearle)
August 7, 2019, 4:18pm
12
@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.
Neally
(Neally)
August 7, 2019, 4:26pm
13
What is the lastLogonTimestamp string that you are getting?
tonyearle
(tonyearle)
August 7, 2019, 4:34pm
14
@alexw
everything seems to work fine when converting to the csv, but when csv shows up, the date is
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)}} |
Neally
(Neally)
August 7, 2019, 4:55pm
15
tonyearle:
12/31/1600 7:00:00 PM
That means the lastLogonTimestamp attribute is null (empty).
tonyearle
(tonyearle)
August 7, 2019, 5:05pm
16
@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.
Neally
(Neally)
August 7, 2019, 5:16pm
17
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?
tonyearle
(tonyearle)
August 7, 2019, 5:26pm
18
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.
Neally
(Neally)
August 7, 2019, 6:00pm
19
oh the last logon timestamp is the assumption of the last write time of the user profile folder?
tonyearle
(tonyearle)
August 7, 2019, 6:26pm
20