This repository has been archived by the owner on May 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathListUsers.ps1
61 lines (50 loc) · 1.85 KB
/
ListUsers.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
function Get-LogonSession
{
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline=$true)]
[String[]]$ComputerName = 'localhost')
Process {
$server = [PoshInternals.Wtsapi32]::WTSOpenServer($ComputerName )
if ($server -eq [IntPtr]::Zero)
{
throw new-Object System.ComponentModel.Win32Exception
}
$SessionInfoPtr = [IntPtr]::Zero
$sessionCount = 0
$retVal = [PoshInternals.Wtsapi32]::WTSEnumerateSessions($server, 0, 1, [ref] $SessionInfoPtr, [ref] $sessionCount)
if ($retVal)
{
$dataSize = Get-Size -Type ([PoshInternals.WTS_SESSION_INFO])
$currentSession = [long]$SessionInfoPtr
$bytes = 0
for ($i = 0; $i -lt $sessionCount; $i++)
{
$si = ConvertTo-Object ([System.IntPtr]$currentSession) ([PoshInternals.WTS_SESSION_INFO])
$currentSession += $dataSize;
$userPtr = [IntPtr]::Zero
$domainPtr = [IntPtr]::Zero
if (! [PoshInternals.Wtsapi32]::WTSQuerySessionInformation($server, $si.SessionID, [PoshInternals.WTS_INFO_CLASS]::WTSUserName, [ref] $userPtr, [ref] $bytes) )
{
throw new-Object System.ComponentModel.Win32Exception
}
if (! [PoshInternals.Wtsapi32]::WTSQuerySessionInformation($server, $si.SessionID, [PoshInternals.WTS_INFO_CLASS]::WTSDomainName, [ref] $domainPtr, [ref] $bytes))
{
throw new-Object System.ComponentModel.Win32Exception
}
[PSCustomObject]@{
Domain = (ConvertTo-String $domainPtr -Ansi);
UserName = (ConvertTo-String $userPtr -Ansi);
}
[PoshInternals.Wtsapi32]::WTSFreeMemory($userPtr) | Out-Null
[PoshInternals.Wtsapi32]::WTSFreeMemory($domainPtr) | Out-Null
}
[PoshInternals.Wtsapi32]::WTSFreeMemory($SessionInfoPtr) | Out-Null
[PoshInternals.Wtsapi32]::WTSCloseServer($Server) | Out-Null
}
else
{
throw new-Object System.ComponentModel.Win32Exception
}
}
}