wmic useraccount SID blank spaces/empty lines - windows

I have this two commands to get SID useraccount
wmic useraccount where name='%username%' get sid | findstr /b /C:"S-1" > file.txt
or
for /F "tokens=2 delims=," %f in ('whoami /user /FO CSV /NH') do echo %~f > file.txt
both cases (file.txt) contain blank spaces/empty lines. I try to remove with
findstr /r /v "^$"
But it is impossible. How can remove all blank spaces/empty lines to get only SID number

#echo off
setlocal enableextensions disabledelayedexpansion
for /f "skip=2 tokens=2 delims=," %%a in ('
wmic path win32_useraccount
where name^="%username%"
get sid^,status /format:csv
') do (
>"file.txt" <nul set /p"=%%a"
)
That will skip headers, include an additional field so the sid is the second field in the record (in csv format the node field gets included) so we can avoid an ending carriage return in the output of the wmic command (each line ends in CR+CR+LF) and the output is sent to the file using a set /p command with the input stream reading from nul device. This way the output to the file will not include the ending CR+LF that is included with echo
The same code for in the whoami version
#echo off
setlocal enableextensions disabledelayedexpansion
for /f "tokens=2 delims=," %%a in ('
whoami /user /FO CSV /NH
') do (
>"file.txt" <nul set /p"=%%~a"
)

This batch file can did the trick :
#echo off
for /f "delims= " %%a in ('"wmic path win32_useraccount where name='%UserName%' get sid"') do (
if not "%%a"=="SID" (
set myvar=%%a
goto :loop_end
)
)
:loop_end
echo SID=%myvar%
echo %myvar% > SID_%username%.txt
pause>nul
Start "" SID_%username%.txt
For the second method of whoami /user /FO CSV /NH ,you can take a look at this :
Get SID for current logged in domain user

Here's how I'd do it, WMIC:
for /f "skip=1" %a in ('"wmic useraccount where name='%username%' get sid"') do #for %b in (%a) do #(>file.txt echo(%b)
and with WhoAmI
for /f tokens^=3^ delims^=^" %a in ('whoami /user /FO CSV /NH') do #(>file.txt echo(%a)
both in the command line to match the question (ignoring the batch-file tag).

Related

Batch "For" loop outputs in two lines instead of one in TXT

I want this following for-loop to output the findings of these two searches in one line in the All_Metadata.txt
for %%a in (*.txt) do (
findstr /B "589" %%a
findstr /X /C:"1 1,96 " %%a
) >> All_Metadata.txt
This For-Loop output looks like this:
58998545
1 1,96
Instead of (this is what it should look like):
58998545 1 1,96
Operating System: Windows 10
Capture the output of the both findstr commands into variables with for /f loops and echo them together (no need for a variable with the second one; just use the for variable):
#echo off
setlocal ENABLEDELAYEDEXPANSION
for %%a in (*.txt) do (
set "b="
set "c="
for /f "delims=" %%b in ('findstr /B "589" "%%a"') do set "b=%%b"
for /f "delims=" %%c in ('findstr /i /X /C:"1 1,96 " "%%a"') do set "c=%%c"
echo(!b! !c!
)
Another way is, to use the set /p trick to write the first findstr result without a line feed and just add the result from the second findstr (doesn't need delayed expansion):
#echo off
setlocal
for %%a in (*.txt) do (
set "b="
for /f "delims=" %%b in ('findstr /B "589" "%%a"') do <nul set /p "=%%b "
findstr /i /X /C:"1 1,96 " "%%a"
)

How would I isolate or just store the second element in a for loop inside a batch file?

I'm using a loop inside a Windows batch file to return PID numbers and I only want to use the second PID number. How do I set the second element, the second PID number as a variable or just use it in a command in the loop instead of echo?
The code..
for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I cmd.exe') do (
echo PID:%%a
)
I tried indexing the element like this PID:%%a[1]. It didn't work.
Perhaps you want deal like this code ?
#echo off
SetLocal EnableDelayedExpansion
#for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I cmd.exe') do (
set /A count+=1
Set "PID[!count!]=%%~a"
)
#For /L %%i in (1,1,%count%) do echo PID[%%i] = !PID[%%i]!
echo( I want this PID = !PID[2]!
pause

Echo is not working correctly in my Windows Batch script

Main Problem:
The ECHO command ignores all characters before the token argument and inserts all text after the token argument before the token in the output:
Here's the script:
#ECHO OFF
FOR /F "USEBACKQ TOKENS=2 DELIMS==" %%a IN (`
WMIC NIC WHERE
"MACAddress IS NOT NULL"
GET
MACAddress
/FORMAT:LIST ^|
FINDSTR /V /R "^%"
`) DO (
ECHO [%%a]
ECHO 0123456789%%a9876543210
)
PAUSE > NUL
Here's the expected output:
[AA:BB:CC:11:22:33]
0123456789AA:BB:CC:11:22:339876543210
Here's the actual output:
]AA:BB:CC:11:22:33
9876543210AA:BB:CC:11:22:33
Why do I care?
My intention is to replace the DO () block in the example above with this block:
:: ...FOR LOOP STUFF...
`) DO (
WMIC NIC WHERE ^
"MACAddress=%%a" ^
GET ^
NetConnectionID ^
/FORMAT:LIST | FINDSTR /V /R "^$"
WMIC NICCONFIG WHERE ^
"MACAddress=%%a" ^
GET ^
DefaultIPGateway,^
DNSServerSearchOrder,^
IPAddress,^
IPSubnet,^
MacAddress ^
/FORMAT:LIST | FINDSTR /V /R "^$"
)
As described above, all characters before %%a get cut off and all characters after %%a show up before %%a when the script is processed. Somebody please help, I'm figuratively crying here.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "TOKENS=2 DELIMS==" %%a IN ('
WMIC NIC WHERE "MACAddress IS NOT NULL" GET MACAddress
/FORMAT:LIST
') DO (
SET "macaddr=%%a"
SET "macaddr=!macaddr:~0,-1!"
ECHO [!macaddr!]
ECHO 0123456789!macaddr!9876543210
)
GOTO :EOF
I could find no reason for the findstr - as posted, the code gave me no output at all. Removing the findstr provided the reported output.
The wmic quirk can be observed by redirecting wmic output to a file and examining the result with a hex-editor. The cure is to simply mechanically remove the last character of the value assigned to the token.
I simply use Getmac command like in this script :
#echo off
mode 80,5 & Color 9E
Title Get (LAN ,Public) (IP) and MAC Addresses by Hackoo 2017
cls
echo( & echo(
echo Please Wait a While ... Searching for (LAN ,Public)(IP) and MAC addresses ...
#for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
set "LAN_IP=%%a"
)
for /f "tokens=2 delims=: " %%A in (
'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A
#For /f %%a in ('getmac /NH /FO Table') do (
#For /f %%b in ('echo %%a') do (
If /I NOT "%%b"=="N/A" (
Set "MY_MAC=%%b"
)
)
)
Cls
echo(
echo My Private LAN IP : %LAN_IP%
echo My External Public IP : %ExtIP%
echo MAC Address : %MY_MAC%
Timeout /T 5 /NoBreak>nul & Exit

Using FOR Loop and SC Query in CMD Script

Suppose this instruction:
C:\>SC query state= all | find /I "adobe"
The result is:
SERVICE_NAME: AdobeARMservice
DISPLAY_NAME: Adobe Acrobat Update Service
Now I want to obtain the exact argument in this case is "AdobeARMservice"
Here my Script TestLineArgument.bat:
#echo off
cls
REM
REM for /f "delims=" %%# in ('SC query state= all ^| find /I "adobe"') do ( IS NOT WORKING
REM
set "SCQueryState=SC query state ^= all"
set "QueryAdobe=%SCQueryState%^^^| find /I ^"adobe^""
for /f "delims=" %%# in ("%QueryAdobe%") do (
set "THERSERVICE_NAME=%%#"
echo %%#
goto :END_FORSCQUERY
)
:END_FORSCQUERY
REM show the Line "SERVICE_NAME: AdobeARMservice"
echo THERSERVICE_NAME:%THERSERVICE_NAME%
FOR /f "tokens=2" %%# IN ("%THERSERVICE_NAME%") DO (
set "THESERVICE=%%#"
)
REM show the second part "AdobeARMservice"
echo THESERVICE:%THESERVICE%
My Script is not working, I don't know how use FOR with SC Command
Some clue?
#echo off
setlocal enableextensions disabledelayedexpansion
rem Initialize variables
set "serviceName="
set "displayName="
rem Execute the query and retrieve all the SERVICE_NAME lines and
rem the line with the desired display name
for /f "tokens=1,*" %%a in ('
sc query state^= all ^| findstr /i /l /c:"SERVICE_NAME" /c:"Adobe"
') do if not defined displayName if "%%a"=="SERVICE_NAME:" (
set "serviceName=%%b"
) else set "displayName=%%b"
rem Show retrieved data
if defined displayName (
echo %serviceName%
echo %displayName%
) else echo Not found
This method does not work in the manner you were trying to implement, as #MCND answer does, but if "AdobeARMservice" is only returned by sc query state when it is running, this may be useful.
It simply looks for the string "AdobeARMservice" and sets THESERVICE var to the result: If "AdobeARMservice" not found, THESERVICE remains empty. If found it gets value=AdobeARMservice
#echo off
Set "THESERVICE="
For /f %%A in ('sc query state^= all ^| find /i "AdobeARMservice"') do set THESERVICE=%%A
If "%THESERVICE%"=="" echo AdobeARMservice not found
If NOT "%THESERVICE%"=="" echo THESERVICE: %THESERVICE%
pause
My problem was simply...
#echo off
set "SERVICE=Adobe"
for /f "delims=" %%s in ('sc query state^= all ^| find /I "%SERVICE%"') do (
for /f "tokens=2" %%# in ("%%s") do (
echo THESERVICE:%%#
)
)
I tryng to avoid to change the code according to version something like : "Adobe3.2_1" or "Adobe3.2_5" or "Adobe4.5", this snippet of code only...

Redirecting output for unknown result in a batch file

I have the following code that runs through a list of servers which then outputs to a file. Often it comes across a server it can't find the details for and displays the output Request to dc1.abc.local timed-out. How do i capture the servers that returned the result in a separate file?
FOR /F %%i in (servers.txt) do FOR /F "delims=: tokens=2" %%j in (
'nslookup %%i ^| find "Name:"'
) do #echo %%j >> dns.txt 2>> output.txt
Assuming the "timed-out" text is in %%j then test this:
FOR /F %%i in (servers.txt) do FOR /F "delims=: tokens=2" %%j in (
'nslookup %%i ^| find "Name:"'
) do echo %%j |find "timed-out" >> output.txt || echo %%j >> dns.txt

Resources