Get-LastLoggedOnUser.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<#PSScriptInfo
 
.Version
    2.0
.Guid
    f994a5fb-c978-4e1a-b549-e64c3fab097b
.Author
    Thomas J. Malkewitz @dotps1
.Tags
    Wmi, WmiObject, Cim, CimInstance, User
.ProjectUri
    https://github.com/dotps1/PSFunctions
.ReleaseNotes
    Full refactor and added CimInstance support.
 
#>


<#
 
.Synopsis
    Gets the last logged on user of a system.
.Description
    Gets the last not special user to have a loaded profile on a given system.
.Inputs
    System.String
    System.Object.CimSession
.Outputs
    System.Management.Automation.PSCustomObject
.Parameter Name
    System.String
    The The Computer Name.
.Parameter Credential
    System.Management.Automation.PSCredential
    Credential object to used for authentication.
.Parameter CimSession
    System.Object.CimSession
    Cim Session to run against.
.Example
    PS C:\> Get-LastLoggedOnUser
 
    PSComputerName LastUseTime UserName Loaded
    -------------- ----------- -------- ------
    localhost 5/5/2017 9:06:45 AM domain\username True
.Example
    PS C:\> Get-LastLoggedOnUser -Name Server1, Server2 -Credential (Get-Credential)
 
    PSComputerName LastUseTime UserName Loaded
    -------------- ----------- -------- ------
    Server1 5/5/2017 9:06:45 AM domain\username True
    Server1 5/5/2017 9:06:45 AM domain\username False
.Notes
    Using the ByComputerName parameter set uses WinRM.
.Link
    https://dotps1.github.io
.Link
    https://www.powershellgallery.com/packages/Get-LastLoggedOnUser
.Link
    https://grposh.github.io
 
#>



[CmdletBinding(
    DefaultParameterSetName = "ByComputerName"
)]
[OutputType(
    [PSCustomObject]
)]

param (
    [Parameter(
        ParameterSetName = "ByComputerName",
        ValueFromPipeline = $true
    )]
    [ValidateScript({
        try {
            Test-Connection -ComputerName $_ -Quiet -Count 1 -ErrorAction Stop
            return $true
        } catch {
            $_
            return $false
        }
    })]
    [Alias(
        "ComputerName"
    )]
    [String[]]
    $Name = $env:COMPUTERNAME,

    [Parameter(
        ParameterSetName = "ByComputerName"
    )]
    [PSCredential]
    $Credential = $null,

    [Parameter(
        ParameterSetName = "ByCimSession",
        ValueFromPipeline = $true
    )]
    [CimSession[]]
    $CimSession
)

begin {
    function Format-Object ($Object) {
        if ($Object.GetType().Name -eq "ManagementObject") {
            $lastUseTime = $Object.ConvertToDateTime(
                $Object.LastUseTime
            )
        } else {
            $lastUseTime = $Object.LastUseTime
        }

        $userName = ([System.Security.Principal.SecurityIdentifier]$Object.SID).Translate(
            [System.Security.Principal.NTAccount]
        ).Value  

        $output = [PSCustomObject]@{
            PSComputerName = $Object.PSComputerName
            LastUseTime = $lastUseTime
            UserName = $userName
            Loaded = $Object.Loaded
        }

        Write-Output -InputObject $output
    }
}

process {
    switch ($PSCmdlet.ParameterSetName) {
        "ByComputerName" {
            foreach ($nameValue in $Name) {
                try {
                    $userProfile = Get-WmiObject -ComputerName $nameValue -Class "Win32_UserProfile" -Namespace root\CimV2 -Filter "Special = 'False' and LastUseTime != NULL" -Credential $Credential -ErrorAction Stop |
                        Sort-Object -Property LastUseTime |
                            Select-Object -Last 1
                } catch {
                    Write-Error -Message $_.ToString()
                    continue
                }

                $output = Format-Object -Object $userProfile

                Write-Output -InputObject $output
            }
        }

        "ByCimSession" {
            foreach ($cimSessionValue in $CimSession) {
                try {
                    $userProfile = Get-CimInstance -CimSession $cimSessionValue -ClassName "Win32_UserProfile" -Namespace root\CimV2 -Filter "Special = 'False' and LastUseTime != NULL" -ErrorAction Stop |
                        Sort-Object -Property LastUseTime |
                            Select-Object -Last 1
                } catch {
                    Write-Error -Message $_.ToString()
                    continue
                }

                $output = Format-Object -Object $userProfile

                Write-Output -InputObject $output
            }
        }
    }
}