header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Find last logged on user of a computer
Last Post 08 Mar 2010 08:05 AM by PoSherLife. 4 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Terry2winUser is Offline
New Member
New Member
Posts:39
Avatar

--
04 Mar 2010 01:54 AM
    Hello everybody.

    I've got a script where I find out who is the logged on user for a specific PC. The script looks like this:

    PS> Gwmi Win32_ComputerSystem -Comp "pcname"

    This "script" works perfectly fine, but what if I want to (if there is no user logged on at the moment) find out which user that logged on last?

    This information is stored in the registry and it's possible to extract it from there using .VBS so it should be possible with PowerShell too.

    Any suggestions?
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    05 Mar 2010 12:21 PM
    This will give you the time and date of the last logon. It will show any successful logon including being unlocked. To show all records remove | select -First from the end

    $data = @()
    $profiles = GWMI Win32_NetworkLoginProfile -ComputerName COMPUTER_NAME
    foreach ($profile in $profiles){
    $date = $profile.LastLogon
    if ($date -ne $null -and $date -ne "**************.******+***") {
    $row = "" | Select User,LogonTime
    $year = $date.SubString(0,4)
    $month = $date.SubString(4,2)
    $day = $date.SubString(6,2)
    $hour = $date.SubString(8,2)
    $min = $date.SubString(10,2)
    $sec = $date.Substring(12,2)
    $row.User = $Profile.Name
    $row.LogonTime = Get-Date -Date ($month + "/" + $day + "/" + $year + " " + $hour + ":" + $min + ":" + $sec)
    $data += $row
    }
    }
    $data | Sort -Descending LogonTime | select -First
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    glnsizeUser is Offline
    Basic Member
    Basic Member
    Posts:193

    --
    05 Mar 2010 05:26 PM
    Cruisader03 nailed it already, but I'd offer a slightly more streamlined approach using the datetime parse method, and V2 new-object Property parameter.

    $name = '.' Get-WmiObject Win32_NetworkLoginProfile -ComputerName $name| #Filter out any non valid DTG Where-Object {$_.LastLogon -match "(?\d{14}\.\d{6}\S)(?\d+)$"}| #Foreach valid entry find the last logon dtg ForEach-Object { # Win32_NetworkLoginProfile formats the utc offset in seconds this # Breaks the DateTime parser. We reformat the string converting # the offset back to hours $CorrectedDTG = "{0}{1:00}" -f $matches.dtg, ($matches.offset/60) New-Object PSObject -Property @{ Name=$_.Name LogonTime=[datetime]::ParseExact($CorrectedDTG, "yyyyMMddhhmmss.ffffffzz", $null) } } | Sort-Object -Descending LogonTime | Select-Object -First 1

    ~Glenn
    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    07 Mar 2010 04:35 PM
    Adapting the .Net [DateTime] and cleaning up. This is now VERY clean. Thx glnsize:

    $data = @()

    $NetLogs = Get-WmiObject Win32_NetworkLoginProfile

    foreach ($NetLog in $NetLogs) {
    if ($NetLog.LastLogon -match "(\d{14})") {
    $row = "" | Select Name,LogonTime
    $row.Name = $NetLog.Name
    $row.LogonTime=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null)
    $data += $row
    }
    }

    $data
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    08 Mar 2010 08:05 AM
    I couldn't help myself and played with it just for fun.

    Get-WmiObject Win32_NetworkLoginProfile |
        Sort -Descending LastLogon |
        Select * -First 1 |
        ? {$_.LastLogon -match "(\d{14})"} |
            % {
                New-Object PSObject -Property @{
                    Name=$_.Name ;
                    LastLogon=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null)
                }
            }
    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer