Have a script in .bat for view last logon name from list:
#echo off
setlocal enabledelayedexpansion
set cnt=0
for /f "usebackq tokens=*" %%A IN ("m_names.txt") DO (
echo [%%A]
wmic /node: %%A COMPUTERSYSTEM GET dnshostname,USERNAME /all
)
In m_names.txt :
lazar2
counter683
machine
localm
etc
May be another way to call wmic service ?
Related
I am facing a strange situation inside a Windows batch script that I currently work on.
In this section I extract the PID of a parent process, using its child's name (%PROCESS_NAME%). It work well and I can echo PARENT_PID without problems :
for /f "usebackq tokens=2 delims==" %%a in (`wmic process where ^(name^='%PROCESS_NAME%'^) get parentprocessid /value`) do (
set PARENT_PID=%%a
)
echo !PARENT_PID!
Result show the expected value :
16392
The problem is in next line :
taskkill /f /t /pid !PARENT_PID!
The process doesn't get killed and the script showing a strange text (I am working in a french environment) :
" est introuvable.sus "16392
If I replace !PARENT_PID! with the hardcoded PID value like this :
taskkill /f /t /pid 16392
Everything works fine and the parent process and its child are killed.
I tried many thing but cannot figure out what is wrong.
If anyone has an idea, I'm interested to kow, thanks !
Try like this and you'll see the problem:
#echo off
setlocal enableDelayedExpansion
for /f "usebackq tokens=2 delims==" %%a in (`wmic process where "name='explorer.exe'" get parentprocessid /value`) do (
set PARENT_PID=%%a
)
echo -!PARENT_PID!-
WMIC output sets one additional carriage return at the end. Here you can find a workaround (with an additional for loop):
#echo off
setlocal enableDelayedExpansion
for /f "usebackq tokens=2 delims==" %%a in (`wmic process where "name='explorer.exe'" get parentprocessid /value`) do (
for /f "tokens=* delims=" %%# in ("%%~a") do set "PARENT_PID=%%#"
)
echo -!PARENT_PID!-
I've always opted to try to prevent an issue from happening, rather than fix it later. For that reason, here are some alternative suggestions for trying to overcome the unwelcome line feed issue.
If you can include a property before and after the target property, (in this case ParentProcessID), and be sure that those 'pre' and 'post' properties will not be NULL, you can retrieve your value without the problematic carriage return.
Example:
#Set "PPID="
#For /F "EOL=H Tokens=2" %%A In ('""%__AppDir__%wbem\WMIC.exe" Process Where Name="%PROCESS_NAME%" Get Handle,ParentProcessID,SessionID 2>NUL"')Do #Set "PPID=%%A"
#Set PPID 2>NUL
#Pause
Alternatively, you could use wmic's /Format option.
Example:
#Set "PPID="
#For /F "Skip=1 Tokens=2 Delims=," %%A In ('""%__AppDir__%wbem\WMIC.exe" Process Where Name="%PROCESS_NAME%" Get ParentProcessID /Format:CSV 2>NUL"') Do #Set "PPID=%%A"
#Set PPID 2>NUL
#Pause
Note: There's an issue that I'm aware of in windows-7, whereby the location of required .xsl files is not correct. One fix is to move those files into the correct directory, \Windows\System32\wbem. Alternatively find its location and specify it directly within the value. On the systems I've used, I found csv.xsl in \Windows\System32\wbem\en-US, so you could use, (adjusting as necessary):
#Set "PPID="
#For /F "Skip=1 Tokens=2 Delims=," %%A In ('""%__AppDir__%wbem\WMIC.exe" Process Where Name="%PROCESS_NAME%" Get ParentProcessID /Format:"%__AppDir__%wbem\en-US\csv.xsl" 2>NUL"') Do #Set "PPID=%%A"
#Set PPID 2>NUL
#Pause
Hello dear people and others,
Today i wanted to create a simple script, thought it would be easy to store the outcome to var of the following command:
wmic bios get serialnumber | findstr /N /V SerialNumber
Outcome:
2:H3GK4S1
3:
The problem is when i try to get the serial with wmic, it returns the string as expected but also an empty string/line. When i try to store the serial to a variable it stores it and then directly overwrites it with the empty string. This is the function i nearly got working now:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
And this gives the following output:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber ^| FINDSTR /N /V SerialNumber') DO (SET serial=%g & ECHO %g)
2:H3GK4S1
3:
As can be seen above, the loop overwrites the serial var, if someone can help me towards the right directon to get this working, would be mad.
At the Command Prompt:
For /F "Tokens=1* Delims==" %g In ('WMIC BIOS Get SerialNumber /Value') Do #For /F "Tokens=*" %i In ("%h") Do #Set "serial=%i" & Echo %i
Or in a batch file:
#For /F "Tokens=1* Delims==" %%g In ('WMIC BIOS Get SerialNumber /Value'
) Do #For /F "Tokens=*" %%i In ("%%h") Do #Set "serial=%%i" & Echo %%i
#Pause
EditIf you're happy to use a labelled section in your batch file:
#Echo Off
Set "serial="
For /F "Skip=1 Delims=" %%A In ('WMIC BIOS Get SerialNumber') Do If Not Defined serial Call :Sub %%A
Set serial 2>Nul
Pause
GoTo :EOF
:Sub
Set "serial=%*"
GoTo :EOF
Try like this:
FOR /F "tokens=*" %g IN ('Wmic Bios Get SerialNumber /format:value') DO for /f "tokens=* delims=" %# in ("%g") do set "serial=%#"
echo %serial%
Mind that's a command that should be executed in the command prompt directly.For a batch file you'll need to double the % in the for loop tokens.
In a batch file, you can also use a goto to end the loop after the first iteration :
#echo off
for /f "tokens=2 delims=:" %%a in ('wmic bios get serialnumber ^| findstr /N /V SerialNumber') do (
set "$var=%%a"
goto:next
)
exit/b
:next
echo Result=^> [%$var: =%]
I want to get list of services with their display name and their status.
This is what I have tried:
for /f "tokens=2" %s in ('SC query state^= all ^| find "DISPLAY_NAME"') do #(for /f "tokens=4" %t in ('SC query %s ^| find "STATE"') do #echo %s is %t)
But this returns only limited services such as disk, etc.
This is a perfect task for the built-in WMI command line executable, WMIC.exe.
From the cmd.exe prompt:
For /F "Skip=1 Delims=" %A In ('"WMIC Service Get DisplayName, Name, State"') Do #For /F "Delims=" %B In ("%A") Do #Echo(%B
From a batch file:
#For /F "Skip=1 Delims=" %%A In ('"WMIC Service Get DisplayName, Name, State"'
) Do #For /F "Delims=" %%B In ("%%A") Do #Echo(%%B
#Pause
Try getting help from powershell directly in your batch file, like this for example (save as .bat and run it). In the example i type it to the screen and give you ALL services, RUNNING ONLY, and STOPPED ONLY but you could do basically what you want with the content of that txt file (search it with a loop, save a part to a variable, etc.).
#echo off&cls
pushd %~dp0
echo.
echo List ALL services
pause
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "powershell Get-Service | Out-File services.txt"
type services.txt
echo.
echo List services that are RUNNING only
pause
find "Running" services.txt
echo.
echo List services that are STOPPED only
pause
find "Stopped" services.txt
pause
:cleanup
del /f services.txt
I am trying to write a batch script that saves the result of a command in a variable. so I can use it later.
For example I am tryin to run this on the script:
sc queryex "Service" |find /i "pid"
but I want to save this result in a variable.
set PIDRS=sc queryex "Themes" |find /i "pid"
ECHO "%PIDRS%
Any Ideas?
for /f "tokens=* delims=" %%# in ('sc queryex "Themes" ^|find /i "pid"') do set "PIDRS=%%#"
echo %PIDRS%
This will set the entire line to PIDRS
here's how to get only the pid:
#echo off
set "rspid="
for /f "skip=9 tokens=2 delims=:" %%# in ('sc queryex "Themes"') do (
if not defined rspid set /a rspid=%%#
)
the second does not use additional FIND which in theory should make it faster.
Hello having my first go with a BATCH script, I'm getting the size of the HDD as follow:
wmic diskdrive get size
Which works fine but I'd like to store this value into a variable for later use, such as using ECHO to display the value.
I'm not sure how I set the output of the above command to a variable. I went with:
SET hddbytes=wmic diskdrive get size
But this just sets the variable to the above text string and not the output.
For usage in batch file. From command line, replace %% with %
for /f "tokens=*" %%f in ('wmic diskdrive get size /value ^| find "="') do set "%%f"
echo %size%
Or, if you want to use you prefered variable
for /f "tokens=2 delims==" %%f in ('wmic diskdrive get size /value ^| find "="') do set "myVar=%%f"
echo %myVar%
You want:
for /f %%a in ('wmic diskdrive get size^|findstr [0-9]') do echo %%a
Updated:
WMIC's output could get a trailing Carriage Return in some environment:
Size <CR>
<CR><LF>
256052966400<CR>
<CR><LF>
500105249280<CR>
<CR><LF>
15496427520 <CR>
<CR><LF>
use csv format, and use FOR loop to truncate the wanted value from the wmic output:
For /F "tokens=2,3 delims=," %%a in ('"wmic diskdrive get size,Status
/format:csv |findstr "[0-9]" "') do (
echo "%%a"
)
Another Batch Script Example:
setlocal ENABLEDELAYEDEXPANSION
:: You can change active code page as well::
#chcp 936 >NUL
:: [example] Remove all network printers:
for /F "tokens=2,3 delims=," %%a in ('"wmic printer where 'local=FALSE' get Name,PrinterStatus /format:csv |findstr "." "') do (
echo "%%a"
rundll32 printui.dll,PrintUIEntry /n "%%a" /dn /q
)
for /f "delims=" %%w in ('wmic diskdrive get size /format:Textvaluelist.xsl') do for /f "usebackq delims=" %%a in ('%%w') do set %%a
echo %size%