locked
Powershell script to see currently logged in users from specified system(domain\user.name) + status (active, idle, away) RRS feed

  • General discussion

  • hello MS Scripting Guys..............!

    I am new in power shell scripting. :P 

    i found this script. for see who logged in system with this i can see who logged in he is active or not & his host name. but i this is not enough for me.......... :(

    i love to see also his logged in time , in this script i cant find if user's status as per my requirement like user is active, idle,away i got when user idle then output was Active : False instead idle or away.

    in this script every time i need to type following command should we can use "$computername = Read-Host 'Computer Name' " so i just need to put computer name instead  of "  Get-LoggedOnUser -ComputerName punw0030 "

    please help me .................that would help me a lot

    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
        }
    }


    • Edited by Rohan Jannu Thursday, November 5, 2015 7:16 PM
    • Changed type Rohan Jannu Friday, November 6, 2015 4:20 PM
    Thursday, November 5, 2015 7:15 PM

All replies