Syed Jahanzaib Personal Blog to Share Knowledge !

January 26, 2017

Check remote windows logged-in user/lock status via BASH

Filed under: Microsoft Related — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 10:42 AM

locked

Scenario:

We have Active Directory environment in our office. Clients OS are mixed starting from windows 2000/2003/2008 and Win7.
For some specific reasons/policy, our helpdesk staff is often required to inquire if the employee is working on his workstation or if his/her windows status is locked.

Solution:

Since I am using my Ubuntu box to manage most of the Active Directory functions using Webmin/BASH scripts, therefore I made a small bash script which queries for remote windows logged in user session and windows locked/unlocked status.

The bash script does the following …

  • Check for remote PC PING Status, if ping fails, exit with error
  • Get remote windows IP via NSLOOKUP using local DNS
  • Current Logged-in user and their status
  • Current status of windows either its locked/unlocked.
  • TRIM the results and display according to our taste

the Script!

> root@linux:/temp# cat winuserstatus.sh

#!/bin/bash
# Script to check remote windwos status, like Loggedin + Windows Lock/Unlock status
# More functions can be added/removed as required.
# I attached this script to webmin for our Support dept.
# Syed Jahanzaib / aacable.wordpress.com / aacable @ hotmail . com
# Created: 25-JAN-2017

# set -x
QUSER_HOLDER="/tmp/$1.quser"
LOCK_HOLDER="/tmp/$1.lockstatus"
REMOTE_PC="$1"
PING_ATEMPTS="1"
PING_STATUS="/tmp/$1.ping.status"
LOCAL_DNS_IP="10.0.0.1"

# Domain credentials details so that winexe can execute commands on all domain clients
DOMAIN="domain.local"
DOMAIN_ADMIN="admin"
ADMIN_PASS="password"

# Empty All Holders
> $QUSER_HOLDER
> $LOCK_HOLDER
> $PING_STATUS

# Check if remote PC is accessibel or not,
## IF PING FAILS then inform accordingly and EXIT
ping -q -c $PING_ATEMPTS $REMOTE_PC &>/dev/null > $PING_STATUS
PING_RESULT=`cat $PING_STATUS`
if [ "$PING_RESULT" = "" ]; then
echo "ERROR: Unknown HOST. Exiting"
exit 1
fi

# Print PC NAME (from $1 variable)
echo "
Remote PC = $1"

# Print IP of remote PC via nslookp using local DNS
echo "IP Details =
`nslookup $1 | grep Address | sed /$LOCAL_DNS_IP/d`"

# If ping failed, then print Error and EXIT
if [[ $(ping -q -c $PING_ATEMPTS $REMOTE_PC) == @(*100% packet loss*) ]]; then
echo "$1 not responding to ping request, probably system is not UP"
exit 1
fi

# Query remote windows Logged in user using Linux WINEXE tool
winexe -U $DOMAIN/$DOMAIN_ADMIN%"$ADMIN_PASS" //$1 "quser" > $QUSER_HOLDER
QUSER_RESULT=`cat $QUSER_HOLDER |grep Active`
if [ "$QUSER_RESULT" = "" ]; then
echo "
User Status = No user is active"
else
echo "
User Status = Logged in User found ... details as below ...
$QUSER_RESULT
"
fi

# Query remote windows TASK list to find if windows is locked/unlocked
winexe -U $DOMAIN/$DOMAIN_ADMIN%"$ADMIN_PASS" //$1 "tasklist" > $LOCK_HOLDER
LOCK_RESULT=`cat $LOCK_HOLDER |grep -E "LogonUI.exe|logon.scr"`
if [ "$LOCK_RESULT" = "" ]; then
echo "
Windows Status = Windows is UN-LOCKED!"
else
echo "
Windows Status = Windows is LOCKED"
fi

# Script function ends here
# Thank you


Result:

When User is logged in and windows is LOCKED!

root@linux:/temp# /temp/winuserstatus.sh WORKSTAION-1

Remote PC = WORKSTAION-1
IP Details =
Address: 10.0.0.20
Address: 10.0.0.21

User Status = Logged in User found ... details as below ...
jahan.zaib console 13 Active 1+00:53 1/23/2017 1:57 PM
Windows Status = Windows is LOCKED

When User is logged in and windows is UN-LOCKED!

root@linux:/temp# /temp/winuserstatus.sh WORKSTAION-1

Remote PC = WORKSTAION-1
IP Details =
Address: 10.0.0.21
Address: 10.0.0.20

User Status = Logged in User found ... details as below ...
jahan.zaib console 13 Active 1+00:53 1/23/2017 1:57 PM
Windows Status = Windows is UN-LOCKED!

When User is NOT logged in and windows is LOCKED as well !


root@linux:/temp# /temp/winuserstatus.sh SERVER-2

Remote PC = SERVER-2
IP Details =
Address: 101.11.11.2
No User exists for *

User Status = No user is active

Windows Status = Windows is LOCKED

Regard’s
Syed Jahanzaib

%d bloggers like this: