I received a call requesting assistance one of our users is unable to remote into an SQL server. He is receiving the following error:
I tried to remote as well but received the same error. Then I started researching on how to find the users that are logged in, so that he may communicate with them and ask them to log off, but this brought a very limited amount of information. I found out that the class Win32_LoggedOnUsers doesn't really give you that information, but then I found out that there is a way to bypass the maximum number of connections by using the following command:
%systemroot%\system32\mstsc.exe /admin
Once I logged in I was able to go through the Task Manager and find and log off the extra user, but I was not happy with this solution. I feel like it might not always work. After further looking into this issue I found in Learn-PowerShell that I was able to use the WMI Class Win32_Process to get the Explorer.exe process and get the Owner, thus revealing the logged on users.
Script found in sauce:
Function Get-WMIComputerSessions {
[cmdletbinding(
DefaultParameterSetName = 'session',
ConfirmImpact = 'low'
)]
Param(
[Parameter(
Mandatory = $True,
Position = 0,
ValueFromPipeline = $True)]
[string[]]$computer
)
Begin {
$report = @()
}
Process {
ForEach ($c in $computer) {
$proc = gwmi win32_process -computer $c -Filter "Name = 'explorer.exe'"
ForEach ($p in $proc) {
$temp = "" | Select Computer, Domain, User
$temp.computer = $c
$temp.user = ($p.GetOwner()).User
$temp.domain = ($p.GetOwner()).Domain
$report += $temp
}
}
}
End {
$report
}
}
But I didn't quite need all this so I simply made a one line :
(gwmi
win32_process -computer
SERVERNAME -Filter
"Name = 'explorer.exe'").GetOwner().User
Now I am able to find out the users that are logged in without having to remote into the server.
Hope this might help other people out there!