I want to query a remote machine and if the KB22334358 is found in the systeminfo output then exit, otherwise write hostname to failed.txt.
Why doesn't this work?
systeminfo /s "remotenamemachine" | find "KB22334358"
if %errorlevel% equ 1 goto nome else goto exit
:nome
systeminfo | find "Nome host" > C:\failed.txt
:exit
exit
Seems like you might need to use the /s switch as well when you connect the next time. Also, you might want to make the find command not case sensitive by using /i. You might also want to append to the failed.txt file, like so:
systeminfo /s "remotenamemachine" | find "KB22334358"
if %errorlevel% equ 1 goto nome else goto exit
:nome
systeminfo /s "remotenamemachine" | find /i "Nome host" >> C:\failed.txt
:exit
exit
Or you can use this triple for /f loop that gets it all in one shot:
KB_Checker.bat
#echo off
setlocal ENABLEDELAYEDEXPANSION
set comp_name=%*
for /f "usebackq tokens=1* delims=" %%I in (`
systeminfo /s "%comp_name%"
`) do (
for /f "usebackq tokens=2 delims=:" %%k in (`
echo %%I ^| findstr /v "^)" ^| findstr "KB22334358"
`) do (
for /f "tokens=1* delims=" %%f in ('
echo %%k ^| findstr /v "UTC"
') do (
set temp_KB=%%f
)
)
for /f "usebackq tokens=2 delims=:" %%h in (`
echo %%I ^| findstr /i /C:"Nome host"
`) do (
for /f "tokens=1* delims=" %%f in ('
echo %%h ^| findstr /v "UTC"
') do (
set hm=%%f
)
)
) 2> nul
set KB=!temp_KB!
if not defined KB echo !hm! >> C:\failed.txt
exit
UPDATE
I updated the script above to set the comp_name environment var. You can pass the computer name into the script like so: KB_Checker.bat computer_1 and it will process that
Related
I am developing a shell script that executes a command a returns a checksum string. This string is has each hexa separated with white spaces, something that I would like to remove and have, for example, 4AA512, instead of 4A A5 12 as command output but I am not able to find a solution that works. Here the script:
for /f "delims=" %%f in ('dir %~dp0*.zip /b') do (
echo %%~f:
for /f "delims=" %%a in ('certUtil -hashfile "%~dp0%%~f" SHA512 ^| find /i /v "SHA512" ^| find /i /v "certUtil"^') do (
echo %%a:' '=''%
)
set /a counter += 1
echo.
)
Anyone has a solution?
Thanks!
(answer moved from question/comment to - well - an answer)
Finally got a solution:
set counter=0
for /f "delims=" %%f in ('dir %~dp0*.zip /b') do (
echo %%~f:
for /f "delims=" %%a in ('certUtil -hashfile "%~dp0%%~f" SHA512 ^| find /i /v "SHA512" ^| find /i /v "certUtil"^') do (call :ShowChecksum "%%a")
set /a counter += 1
echo.
)
echo %counter% files(s) found.
pause
exit
:ShowChecksum
set "checksum=%~1"
set "checksum=%checksum: =%"
echo %checksum%
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
Codes as below:
im getting below error:
FINDSTR: No search strings
I have traced the error an its coming from here:
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "!FILENAME!" ^| findstr /B "%numbers%"') do (...
Script is working properly if Im replacing FILENAME variable with exact filename manually. But I need to put it in a loop to execute within multiple files..
for /r %%i in (LOG_FILE*.txt) do (
set FILENAME=%%~nxi
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
set FILENAME=!FILENAME:~0,-1!
echo !FILENAME!>>tmptmptmp.tmp
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "!FILENAME!" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
)
)
Working without the outer FOR loop
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
rem Assemble the list of line numbers
set numbers=
if exist "tmp" del "tmp"
if exist "tmp2" del "tmp2"
if exist "tmp.txt" del "tmp.txt"
REM for /r %%i in (LOG_FILE*.txt) do (
REM set FILENAME=%%~nxi
set FILENAME=LOG_FILE14012015.txt
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%FILENAME%" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
)
Thanks for the help guys. I did a workaround as im not able to force the findstr to work properly with my requirements. Did two separate batch script to handle the logic. Below is a copy in case anyone is interested.
bat file that will handle the outer for loop:
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
::Script that will loop for multiple files and will call search.bat
if exist "tmp.txt" del "tmp.txt"
if exist "multiple_search.log" del "multiple_search.log"
#echo Starting search... >> multiple_search.log
for /r %%i in (LOG_FILE*.txt) do (
#echo Searching %%i >> multiple_search.log
call search.bat %%i
)
#echo Search completed... >> multiple_search.log
endlocal
Main bat file:
#echo off
setlocal EnableDelayedExpansion
setlocal enableextensions
set numbers=
if exist "tmp" del "tmp"
if exist "tmp2" del "tmp2"
set FILENAME=%1
for /F "delims=:" %%a in ('findstr /I /N /C:"fin.700 " !FILENAME!') do (
set /A val1=%%a-3, val2=%%a+3, val3=%%a+4, val4=%%a+11, val5=%%a+13 , val6=%%a+29, val7=%%a+30
set "numbers=!numbers!!val1!: !val2!: !val3!: !val4!: !val5!: !val6!: !val7!: "
)
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%FILENAME%" ^| findstr /B "%numbers%"') do (
set linestr=%%b
echo !linestr!
echo !linestr!>>tmp
)
set delim==
for /f "tokens=1*" %%a in (tmp) do (
echo %%a|find "!delim!" >nul
if !errorlevel!==0 (echo %%a%%b >> tmp2) else (echo record=%%a%%b >> tmp2)
)
set counter=0
set var=
for /f "tokens=1* delims==" %%a in (tmp2) do (
set /a counter=!counter!+1
set var=!var!%%b
set var=!var: =!
set var=!var!,
if !counter! geq 7 (
echo !var! >> tmp.txt
set counter=0
set var=)
)
endlocal
I need to make a batch script that disable all local user accounts of a computer or a way to list all the users, one below another.
I try to generate a file with the command:
net user > %systemroot%\users.txt | findstr /I adm
but the command returns me all users in the same line.
If someone knows how to generate every user in a separate line, it helps me too.
Here's a working code snippet:
for /F "tokens=1* delims==" %%G in (
'wmic USERACCOUNT where "Name!='Administrator'" get Name /value'
) do for /F "tokens=*" %%x in ("%%H") do (
echo %%x
rem some further command(s) here
)
Where the for loops are
%%G to retrieve the Name value (in the second token, %%H);
%%x to remove the ending carriage return in the value returned (wmic behaviour: each output line ends with 0x0D0D0A instead of common 0x0D0A), see the example below.
Example (copy & paste from my command line window):
==>for /F "tokens=1* delims==" %G in ('wmic USERACCOUNT where "Name!='Administra
tor'" get Name /value') do #echo "%H"
""
""
"Guest
""
""
"Edgar
""
""
"Allan
""
""
"Poe
""
""
""
==>for /F "tokens=1* delims==" %G in ('wmic USERACCOUNT where "Name!='Administra
tor'" get Name /value') do #for /F "tokens=*" %x in ("%H") do #echo "%x"
"Guest"
"Edgar"
"Allan"
"Poe"
==>
I don't believe net.exe can output them as separate lines, but you can use a call statement with shift to break the line into separate variables.
for /f "delims=" %%a in ('net user ^| findstr /i adm') do set userlist=%%a
call :process %userlist%
goto :eof
:process
if {%1}=={} goto :eof
::-----------
rem Do something with %1 here
echo %1
::-----------
shift
goto :process
here is the code that solved to me:
for /f "delims=" %%a in ('net user ^| findstr -V "Contas de usu" ^| findstr -V "Comando conc" ^| findstr -V "\-" ^| findstr -V "User accounts" ^| findstr -V "The command"') do (
for %%b in (%%a) do (
if not "%%b" == "Administrador" (
if not "%%b" == "Administrator" (
if not "%%b" == "Convidado" (
Net user %%b /active:no
) else (
for /f "delims=" %%c in ('ver ^| findstr /i "5\.1\."') do (
set version=%%c
)
if not {"!version!"} == {""} (
Net user "Convidado" /active:yes
) else (
Net user "Convidado" /active:no
)
)
) else (
Net user "Administrator" /active:yes
)
) else (
Net user "Administrador" /active:yes
)
)
)
WMIC USERACCOUNT WHERE localaccount=true set disabled=true
run it with admin privileges.
I have been working on this script for a little and I am new to writing batch files. I know my syntaxs is wrong and need some help.
#echo off
setlocal enabledelayedexpansion
set "ports=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports"
for /f %%I in ( 'reg query "%ports%"')
do (
echo %%I | findstr /i "c:\\convertdoc\\output\\silentprinttemp\\.*\.ps" >NUL
IF ERRORLEVEL 1 reg delete "%ports%" /v "%%I" /f
)
#echo off
setlocal
set "ports=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports"
for /f %%I in (
'reg query "%ports%"'
) do (
echo %%I | findstr /i "c:\\convertdoc\\output\\silentprinttemp\\.*\.ps" >NUL
IF ERRORLEVEL 1 reg delete "%ports%" /v "%%I" /f
)
As the comments also point out, cmd wants "do" on the same line as the closing parenthesis of the previous expression. You're not using delayed expansion. It's not an error, but I see no reason to turn it on.
#echo off
setlocal
set "ports=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Ports"
for /f %%I in ( 'reg query "%ports%"' ) do (
echo %%I | findstr /i "c:\\convertdoc\\output\\silentprinttemp\\.*\.ps" >NUL
IF ERRORLEVEL 1 reg delete "%ports%" /v "%%I" /f
)
Curiously, this experiment worked:
FOR /F %%i IN (
'dir /b'
) DO (
#ECHO %%i
)