Bearing in mind I haven't got a Domain setup to test this on:
I get the following return from
QUERY USER /server:8x8x64 (8x8x64 is localhost):
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
4wd console 1 Active none 6/05/2015 10:31
A multi-line return, you seem to be interested in only the first three tokens returned, which would be: USERNAME, SESSIONNAME, ID
If it's a different for a Domain server, can you post an example?
Otherwise, here's a short version that might work, (no output regarding who's logged on/off - but you should be able to add it under the IF statement by parsing the text file for the info):
SET serverlist=APPLE BANANA CHERRY
SET userlist=administrator zebra yankee
SET tempfile=%TEMP%\quser.txt
FOR %%s in (%serverlist%) DO (
QUERY USER /server:%%s >%tempfile%
FOR %%u in (%userlist%) DO (
FIND /C /I %%u %tempfile%
IF %ERRORLEVEL% NEQ 0 LOGOFF /server:%%s
)
)
Regarding your command file:
'QUERY USER /server:%%s
^"' - seems to escaping a single quotation mark, there's no matching quote, although quotes shouldn't be needed anyway since there's no spaces in any of the arguments.
While it runs, it could be written as:
FOR /f "tokens=1,2,3 usebackq" %%a IN (`QUERY USER /server:%%s`) DO (