PowerShell now has the following query:
query user /server:$server
Returns the output:
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
svc_chthost 2 Disc 1:05 8/16/2016 12:01 PM
myusername rdp-tcp 3 Active . 8/29/2016 11:29 AM
I am currently using @(query user /server:$server).Count - 1as a value to represent the number of registered users (this is not very, I know). However, now I would like to receive information such as USERNAME, IDand LOGON TIMEfor use in other parts of my script.
My question is a more convenient way of analyzing the above information, or maybe a more effective solution to my problem: counting and collecting information related to registered users.
I found other solutions that seem to be more efficient, but I am sure to do an easier way for this:
$ComputerName | Foreach-object {
$Computer = $_
try
{
$processinfo = @(Get-WmiObject -class win32_process -ComputerName $Computer -EA "Stop")
if ($processinfo)
{
$processinfo | Foreach-Object {$_.GetOwner().User} |
Where-Object {$_ -ne "NETWORK SERVICE" -and $_ -ne "LOCAL SERVICE" -and $_ -ne "SYSTEM"} |
Sort-Object -Unique |
ForEach-Object { New-Object psobject -Property @{Computer=$Computer;LoggedOn=$_} } |
Select-Object Computer,LoggedOn
}
}
catch
{
}