Ok, 1st off, I'm guessing this probably isn't the best place for this post. If not, someone can move it to the best area - as long as it's not the trash bin!
I am having problems with a Windows batch script. Maybe someone out there can point out my error.
What I'm trying to do is to retrieve a list of remote desktop sessions on a server, then log each one off unless the login name matches a list of names to be ignored.
The list of sessions and logging off is not a problem. It's when I need to check against the list of names that I have a problem.
Here's my code:
SET serverlist=APPLE BANANA CHERRY
FOR %%s in (%serverlist%) DO (
FOR /f "tokens=1,2,3" %%a IN ('QUERY USER /server:%%s^"') DO (
:: Don't log off this list of users:
:: administrator, Zebra, Yankee
IF /I [%%a] EQU [administrator] (
ECHO Ignored user %%a
) ELSE (
IF /I [%%a] EQU [Zebra] (
ECHO Ignored user %%a
) ELSE (
IF /I [%%a] EQU [Yankee] (
ECHO Ignored user %%a
) ELSE (
IF %%c LSS 65536 (
ECHO Logging off ACTIVE user %%a on session %%c
LOGOFF /server:%%s %%c
) ELSE (
IF %%b LSS 65536 (
ECHO Logging off DISCONNECTED user %%a on session %%b
LOGOFF /server:%%s %%b
)
)
)
)
)
)
)
When I DON'T check for individual login names, all users are logged off. That part works great - has been working for years.
Checking for the individuals doesn't work - all users are logged off anyway with the message indicating whether they were active or disconnected.
PLUS, with username checking, the script stops after the 2nd server loop. Server APPLE and BANANA are checked and all users are logged off, but the loop stops without checking server CHERRY.
(Turns out the script only had APPLE and BANANA defined - no CHERRY on top!)
I figured I must have an unbalanced parenthesis, but I've gone over this until my head hurts and can't find it. So I turn to this group for help.