none
Check if User is Logged In RRS feed

  • Question

  • I need to check if a Domain User is logged on a server.  The following can't seem to match the pattern for the user but the variables seems to be okay.   The result is always false.  I've tried using match, like, contains.

    $server = "ServerA"
    $ADUser = "CONTOSO\UserA"

    $Loggedin = Get-WmiObject win32_computersystem -computer $server | select username

    echo $server

    echo $ADUser

    echo $Loggedin

    If ($Loggedin -match $ADUser) {write-host "yes"} ELSE {write-host "no"}

    Wednesday, May 7, 2014 5:03 PM

Answers

  • Hi,

    $Loggedin has a username property, you'll need to test against that.

    Also, you can use -eq instead of -match.


    EDIT: Here's an example:

    $server = 'ServerA'
    $ADUser = 'DOMAIN\username'
    
    $loggedIn = Get-WmiObject Win32_ComputerSystem -ComputerName $server
    
    If ($loggedIn.Username -eq $ADUser) {
    
        Write-Host "$ADUser is logged into $server"
    
    } Else {
    
        Write-Host "$ADUser is NOT logged into $server" -ForegroundColor Red
    
    }



    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    • Edited by Mike Laughlin Wednesday, May 7, 2014 5:22 PM
    • Marked as answer by JT_CP Thursday, May 8, 2014 6:45 PM
    Wednesday, May 7, 2014 5:19 PM
  • Copied pasted your whole script and its working, not sure what's wrong with my line....

    If($Loggedin.Username -eq$ADUser) {write-host"yes"} ELSE{write-host"no"}

    That should work just fine. Here's both of the if statements in a single script to demonstrate:

    $server = 'ServerA'
    $ADUser = 'DOMAIN\username'
    
    $loggedIn = Get-WmiObject Win32_ComputerSystem -ComputerName $server
    
    If ($loggedIn.Username -eq $ADUser) {
    
        Write-Host "$ADUser is logged into $server"
    
    } Else {
    
        Write-Host "$ADUser is NOT logged into $server" -ForegroundColor Red
    
    }
    
    
    If ($Loggedin.Username -eq $ADUser) {write-host "yes"} ELSE {write-host "no"} 


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    • Marked as answer by JT_CP Thursday, May 8, 2014 6:45 PM
    Wednesday, May 7, 2014 6:22 PM

All replies

  • Hi,

    $Loggedin has a username property, you'll need to test against that.

    Also, you can use -eq instead of -match.


    EDIT: Here's an example:

    $server = 'ServerA'
    $ADUser = 'DOMAIN\username'
    
    $loggedIn = Get-WmiObject Win32_ComputerSystem -ComputerName $server
    
    If ($loggedIn.Username -eq $ADUser) {
    
        Write-Host "$ADUser is logged into $server"
    
    } Else {
    
        Write-Host "$ADUser is NOT logged into $server" -ForegroundColor Red
    
    }



    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    • Edited by Mike Laughlin Wednesday, May 7, 2014 5:22 PM
    • Marked as answer by JT_CP Thursday, May 8, 2014 6:45 PM
    Wednesday, May 7, 2014 5:19 PM
  • That still returns false where it should be true....
    Wednesday, May 7, 2014 5:45 PM
  • That still returns false where it should be true....

    Not in my testing it doesn't.


    EDIT: You realize that this only works for users logged in at the console, right?

    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Wednesday, May 7, 2014 5:48 PM
  • No, I didn't realize it.  I need to see if either one is logged on, it's a service account.

    When I echo the variables, I do get the correct accoutns shown.   But I can't get the script to return True.


    • Edited by JT_CP Wednesday, May 7, 2014 6:11 PM typo
    Wednesday, May 7, 2014 6:11 PM
  • Copied pasted your whole script and its working, not sure what's wrong with my line....

    If ($Loggedin.Username -eq $ADUser) {write-host "yes"} ELSE {write-host "no"}


    SHould i use "query user -server <servername>"  and search for $ADUser ?  would this be more full proof?
    • Edited by JT_CP Wednesday, May 7, 2014 6:23 PM revised
    Wednesday, May 7, 2014 6:18 PM
  • Copied pasted your whole script and its working, not sure what's wrong with my line....

    If($Loggedin.Username -eq$ADUser) {write-host"yes"} ELSE{write-host"no"}

    That should work just fine. Here's both of the if statements in a single script to demonstrate:

    $server = 'ServerA'
    $ADUser = 'DOMAIN\username'
    
    $loggedIn = Get-WmiObject Win32_ComputerSystem -ComputerName $server
    
    If ($loggedIn.Username -eq $ADUser) {
    
        Write-Host "$ADUser is logged into $server"
    
    } Else {
    
        Write-Host "$ADUser is NOT logged into $server" -ForegroundColor Red
    
    }
    
    
    If ($Loggedin.Username -eq $ADUser) {write-host "yes"} ELSE {write-host "no"} 


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    • Marked as answer by JT_CP Thursday, May 8, 2014 6:45 PM
    Wednesday, May 7, 2014 6:22 PM
  • Now I'll add e-mail function for alert.

    Thanks!

    Wednesday, May 7, 2014 6:29 PM
  • Cheers, you're welcome.

    If you need further help, just start up a new thread.

    Good luck.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Wednesday, May 7, 2014 6:34 PM