Reputation: 1
I have a script for running USMT scanstate and then another for loadstate we are getting ready to get a bunch of computers to replace the oldest ones on the network. I am wanting to use this script/batch/code in PDQ Deploy so I can make a list of the computers we are replacing, and then run the pdq package against them just before I go to swap them out that way all their stuff is backed up as recently as possible, and running on all the machines at once as opposed to going to each one individually. I can't do %username% as it would just return the data for the PDQ account running the command, and I can't set it to run as logged on user as they would not have sufficient privileges to run the necessary commands
I've tried tinkering with using Tokens and Delims, but nothing I try seems to work, I am sure I am getting syntax wrong.
I am using this command, though not opposed to a different command that could yield same results:
query user /server:%computername%
It outputs this (username changed to "***"):
USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME *** console 1 Active none 7/11/2019 6:22 PM
I would like to get what is under the USERNAME part as a variable.
I am thinking like using a FOR /F command, something like:
for /f "tokens=2*" %%a in ('net user "%Username%" /domain ^| find /i "Full Name"') do set DisplayName=%%b
Of course though that gives me their name instead of user name.
Upvotes: 0
Views: 2592
Reputation: 16246
A list of users on the system can be produced by:
(Get-CimInstance -ClassName Win32_SystemUsers).PartComponent.name
If it must run from cmd.exe:
@ECHO OFF
FOR /F %%A IN ('powershell -NoLogo -NoProfile -Command ^
"(Get-CimInstance -ClassName Win32_SystemUsers).PartComponent.Name"') DO (
ECHO user is %%A
)
Upvotes: 0
Reputation: 3264
FWIW, Qwinsta will also give you the username of the user on the console
@(
SetLocal
Echo Off
)
FOR /F "Tokens=2" %%A IN ('
QWinSta ^|Find /I "Console"
') Do (
SET "_UN=%%A"
)
Upvotes: 1
Reputation: 80033
for /f %%a in (`query user /server:%computername%`) do set "uname=%%a"
echo User=%uname%
Should get the desired results if I've scried your question correctly.
The query to be run inside single-inverted-commas, read each line of output from query...
abd the first token is assigned to %%a
by for
and then assigned to the variable uname
by the set
, so the last line of output is the one used to deliver the result. If you were to use delims=v"
then token1 is the string up to the first v
, disregarding any leading v
s on the line.
This code uses the default "tokens=and
delimsas the standard separator-character set, of which <kbd>space</kbd> is a member. You could use
"tokens=1 delims= "`for the same result, the first token in the line is required, where the delimiter used is just the space character.
If you wanted to extract console
then tokens=2
in this case.
Upvotes: 0