0

I am trying to see if there's an active session on the self hosted VMs by using the following script:

@echo off
echo Current User: %USERNAME%
echo.

echo Sessions Active ------------
query user %USERNAME%
echo.

for /f "skip=1 tokens=4" %%s in ('query user %USERNAME%') do ( 
    set status=%%s
    echo Status: %status%
    if not "%status%"=="Active" goto error
    color A
    echo Session Active!
    goto end
)

:error
color C
echo Warning!!! Session not active as expected!
exit /b 1

:end
color 7
exit /b 0

The script when run locally on both administrator & non-administrator windows prints the following:

c:\C#>session 
Current User: Administrator

Sessions Active ------------  
USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
>administrator         console             1  Active      none   4/26/2021 9:05 PM

Status: Active Session Active!

However, when I run the same using cmd /c $(Build.SourcesDirectory\SessionTest.bat from the Azure DevOps pipeline, I get the following output:

Starting: Are Agents Active?
==============================================================================
Task         : Command line
Description  : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version      : 2.182.0
Author       : Microsoft Corporation
Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
==============================================================================
Generating script.
Script contents:
cmd /c "C:\agent\_work\2\s\SessionTest.bat"
========================== Starting Command Output ===========================
"C:\windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "C:\agent\_work\_temp\058a0887-9ca1-4620-8ff7-220151fdf56f.cmd""
Current User: ehpadmin

Sessions Active ------------
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
>ehpadmin              console             1  Active      none   5/11/2021 8:08 PM

Status: 
Warning!!! Session not active as expected!
##[error]Cmd.exe exited with code '1'.
Finishing: Are Agents Active?

Apparently, the below statements are not getting the STATE and assigning the variable propertly. What am I doing wrong?

for /f "skip=1 tokens=4" %%s in ('query user %USERNAME%') do ( 
    set status=%%s
    echo Status: %status%
Vyas Bharghava
  • 6,372
  • 9
  • 39
  • 59
  • There is no way the batch file works correctly in either environment because you are not using delayed expansion. – Squashman May 13 '21 at 13:48
  • 2
    to avoid `delayedepansion` why not just skip the `set` all together? `for /f "skip=1 tokens=4" %%s in ('query user %USERNAME%') do echo Status: %%~s` – Gerhard May 13 '21 at 13:49

1 Answers1

1

Just skip the parenthesized block all together and use the metavariables that the for` loop already defined:

@echo off
echo Current User: %USERNAME%
echo(

echo Sessions Active ------------
query user %USERNAME%
echo(

for /f "skip=1 tokens=4" %%s in ('query user %USERNAME%') do if /i not "%%~s" == "Active" goto :error
Color A & echo session Active & timeout /t 2 >nul
goto :end

:error
color C
echo Warning!!! Session not active as expected!
exit /b 1

:end
color 7
exit /b 0

and If you are using Windows 10, we can make it fancier by using escape codes:

@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "cl=%%a"

cls & echo Current User: %cl%[35m%USERNAME%%cl%[0m
echo(
for /f "skip=1 tokens=4" %%s in ('query user %USERNAME%') do if /i not "%%~s" == "Active" (
    echo %cl%[31mWarning!!! Session not active as expected!%cl%[0m
    exit /b && goto :eof
)
echo %cl%[92mSession Active%cl%[0m
exit /b 0
Gerhard
  • 22,678
  • 7
  • 27
  • 43