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%