远程计算机上的活动登录用户 [英] Active logged in Users on Remote machine

查看:57
本文介绍了远程计算机上的活动登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本在远程机器上获取登录用户.它工作正常,但我需要让用户处于活动"状态

I am using below script to get logged on user on remote machine . It works fine but I need to get the users those status "active"

如何在远程机器上获取那些活跃的登录用户?

How Can I get those active logged in users on remote machine ?

function Global:Get-LoggedOnUser {
    #Requires -Version 2.0            
    [CmdletBinding()]            
     Param             
       (                       
        [Parameter(Mandatory=$false,
                   Position=0,                          
                   ValueFromPipeline=$true,            
                   ValueFromPipelineByPropertyName=$true)]            
        [String[]]$ComputerName = $env:COMPUTERNAME
       )#End Param

    Begin            
    {            
     Write-Host "`n Checking Users . . . "
     $i = 0
     $MyParams = @{
         Class       = "Win32_process" 
         Filter      = "Name='Explorer.exe'" 
         ErrorAction = "Stop"
        }
    }#Begin          
    Process            
    {
        $ComputerName | Foreach-object {
        $Computer = $_

        $MyParams["ComputerName"] = $Computer
        try
            {
                $processinfo = @(Get-WmiObject @MyParams)
                if ($Processinfo)
                    {    
                        $Processinfo | ForEach-Object { 
                            New-Object PSObject -Property @{
                                ComputerName=$Computer
                                LoggedOn    =$_.GetOwner().User
                                SID         =$_.GetOwnerSid().sid} } | 
                        Select-Object ComputerName,LoggedOn,SID
                    }#If
            }
        catch
            {
                "Cannot find any processes running on $computer" | Out-Host
            }
         }#Forech-object(ComputerName)       

    }#Process
    End
    {

    }#End

    }#Get-LoggedOnUsers

推荐答案

为 Win32_ComputerSystem 类添加查询:

Add a query for the Win32_ComputerSystem class:

Get-WMIObject -Class Win32_ComputerSystem -Computername $Computer | Select UserName

这将获取活动"用户,然后您可以使用活动"布尔值构建一个对象.

That'll grab the 'active' user, then you can build an object with an 'Active' boolean value.

这是我的实现:

function Get-LoggedOnUser
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true,
                   Position=0,
                   ValueFromPipeline=$true,
                   ValueFromPipelineByPropertyName=$true)]
        [String[]]$ComputerName
    )
    Begin            
    {            
        $users = $null
        $return = @()
    }
    Process
    {
        ForEach($Computer in $ComputerName)
        {
            $activeUser = Get-WMIObject -class Win32_ComputerSystem -ComputerName $Computer -EA stop | select UserName
            Try
            {
                $processinfo = @(Get-WmiObject -class win32_process -ComputerName $Computer -EA "Stop")
                If ($processinfo)
                {    
                    ForEach($process in $processinfo)
                    {
                        [string[]]$users += $process.GetOwner().user| Where{($_ -ne "NETWORK SERVICE") -and ($_ -ne "LOCAL SERVICE") -and ($_ -ne "SYSTEM")}
                    }
                    If($Users)
                    { 
                        ForEach($user in ($Users | Select -unique))
                        {
                            If($ActiveUser.username -like "*$user")
                            {
                                $Return += New-Object PSObject -Property @{
                                            "User" = $user
                                            "Active" = $true
                                            "Computer" = $Computer
                                }
                            }
                            Else
                            {
                                $Return += New-Object PSObject -Property @{
                                            "User" = $user
                                            "Active" = $false
                                            "Computer" = $Computer
                                }
                            }
                        }
                    }
                    Else
                    {
                        "There are no users logged onto $computer" | Out-Host
                    }
                }
            }
            Catch
            {
                "Cannot find any processes running on $computer" | Out-Host
            }
        }
    }
    End
    {
        $Return
    }
}

值得指出的是,Win32_ComputerSystem 用户名仅在用户本地登录时才会填充,因此任何通过远程桌面登录的人都不会显示为活动".

It is worth it to point out that the Win32_ComputerSystem username is only populated if the user is logged in locally, so anyone logged in through remote desktop won't show as 'Active'.

这篇关于远程计算机上的活动登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆