Need a script to use in WUG to show which user is login to some devices

Hello guys, 

need a script to use in WUG to show which user is login to some devices. any ideas pls? I want a specific user to be login all the time otherwise the application wont work.. so WUG need to alert me if that specific user/account is logged off


  • Mike Rockwell (Progress Software)

    An easier way, you could do it with a Windows Event Passive Monitor and look for Event ID 4647 and put the user name / userID in the "match" on section.

  • Andrew Dolan (Progress Software)

    Hi

     

    I would try a PowerShell active monitor for this. Something like below should work

     

    $resultTextPass = "User logged in"

    $resultTextFail = "User not logged in"

     

     

    $loggedInUser = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty UserName

     

     

    if ($loggedInUser -eq "domain\user") {

     

      $Context.SetResult(0,$resultTextPass);

     

    } else {

     

      $Context.SetResult(1,$resultTextFail);

     

    }

  • Mike Rockwell (Progress Software)

    Ok, Here's one that should work on a remote computer after you create a PowerShell Active Monitor.

    1. #User to Look for
    2. $sUser = "Enter the User Name to Look for" #Enter the User Name to Look for here
    3.  
    4. #Get the device information
    5. $ip = $Context.GetProperty("Address");
    6. $DnsEntry = [System.Net.DNS]::GetHostByAddress($ip);
    7. $Device = [string]$DnsEntry.HostName;
    8.  
    9.  
    10. # Get the Windows credentials
    11. $WinUser = $Context.GetProperty("CredWindows:DomainAndUserid");
    12. $WinPass = $Context.GetProperty("CredWindows:Password");
    13. $sPwd = convertto-securestring $WinPass -asplaintext -force;
    14. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $WinUser,$sPwd;
    15.  
    16. #Setting the message to be logged
    17. $msg = "${sUser} Not Logged In"
    18. $nCode = 1
    19.  
    20. #You can use a comma delimited list for the machine name if you want
    21. function Get-LoggedOnUser
    22. {
    23. [CmdletBinding()]
    24. param
    25. (
    26. [Parameter()]
    27. [ValidateScript({ Test-Connection -ComputerName $_ -Quiet -Count 1 })]
    28. [ValidateNotNullOrEmpty()]
    29. [string[]]$ComputerName = $env:COMPUTERNAME,
    30. [PSCredential] $Credential
    31. )
    32. foreach ($comp in $ComputerName)
    33. {
    34. $output = @{ 'ComputerName' = $comp }
    35. $output.UserName = (Get-WmiObject -Class win32_computersystem -ComputerName $comp -Credential $Credential).name
    36. [PSCustomObject]$output
    37. }
    38. }
    39.  
    40. $result = Get-LoggedOnUser -ComputerName $Device -Credential $cred
    41.  
    42. if($result.UserName -LIke "*${sUser}*"){
    43. $msg = "User Logged In as $($result.UserName)"
    44. $nCode = 0
    45. }
    46.  
    47. $Context.SetResult($nCode, $msg)

    Please NOTE, and it is an important one. the Win32_ComputerSystem won't show users logged in via RDP.