comma separated wmic commands - batch-file

i made this script that uses wmic to get info from remote computer
set /p "user=write UserName="
set /p "password=write Password="
for /f "tokens=*" %%a in (ip.txt) do (
wmic /node:%%a /user:%user% /password:%password% computersystem get Name /format:table
wmic /node:%%a /user:%user% /password:%password% computersystem get Model /format:table
wmic /node:%%a /user:%user% /password:%password% computersystem get totalphysicalmemory /format:table
wmic /node:%%a /user:%user% /password:%password% cpu get Name /format:table
wmic /node:%%a /user:%user% /password:%password% path Win32_VideoController get Name /format:table
wmic /node:%%a /user:%user% /password:%password% os get Caption /format:table
wmic /node:%%a /user:%user% /password:%password% csproduct get identifyingnumber /format:table
wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenheight /format:table
wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenwidth /format:table
)>>a.csv
but the output is a bit weird it seperates with "enter" any idea how to comma separate?

You could try the following code snippet:
#echo off
setlocal EnableDelayedExpansion
set /P "user=write UserName="
set /P "password=write Password="
> "a.csv" (
for /F "usebackq tokens=*" %%a in ("ip.txt") do (
set "lineString="
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem get Name /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem get Model /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem get totalphysicalmemory /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% cpu get Name /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% path Win32_VideoController get Name /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% os get Caption /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% csproduct get identifyingnumber /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenheight /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenwidth /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
if defined lineString echo(!lineString:~1!
)
)
endlocal
exit /B
The wmic output is stored into a ,-separated string in the variable lineString. For capturing the output of a command a for /F loop is perfect. However, the wmic command produces Unicode output, so two nested for /F loops are needed to do a proper Unicode/ANSI conversion; a single loop would leave orphaned trailing carriage-return characters in the strings.
The output format of wmic has been changed from /format:table to /value to get an output like this: Caption=Microsoft Windows 7 Enterprise, so the for /F loop can take the = as a delimiter and get the desired value.
Besides all this, I also changed the following:
In your code, you redirected the output of every single loop iteration separately. To avoid this, () have been put around the (outer-most) for /F loop, so the whole data is redirected once into a.csv. Hence the redirection operator has been changed from append-type >> to (over-)write-type >.
Delayed expansion has been enabled which is required when modifying and reading a variable (lineString) within for loops or blocks of code in between ().

Related

getting errors in this batch script

i have applied this batch script to display all information of my computer here i have getting errors on hdd and sdd like this below
how to correct this script can anyone help me on this
HDD:
3 - Invalid alias verb.
3 - Invalid alias verb.
Missing operand.
Missing operand.
Size: GB
Free space: GB
SSD:
2 - Invalid alias verb.
2 - Invalid alias verb.
Missing operand.
Missing operand.
Size: GB
Free space: GB
this is batch script i have used
#echo off
echo Processor:
wmic cpu get name
echo.
echo RAM:
wmic memorychip get capacity /value
set /a ram=0
for /f "tokens=2 delims==" %%i in ('wmic memorychip get capacity /value ^| find "Capacity"') do set /a ram+=%%i
set /a ramgb=%ram%/1024/1024/1024
echo %ramgb% GB
echo.
echo Motherboard:
wmic baseboard get product,manufacturer
echo.
echo HDD:
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=3 get size /value ^| find "Size"') do set hddsize=%%a
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=3 get freespace /value ^| find "FreeSpace"') do set hddfree=%%a
set /a hddsizegb=%hddsize%/1024/1024/1024
set /a hddfreegb=%hddfree%/1024/1024/1024
echo Size: %hddsizegb% GB
echo Free space: %hddfreegb% GB
echo.
echo SSD:
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=2 get size /value ^| find "Size"') do set ssdsize=%%a
for /f "tokens=2 delims==" %%a in ('wmic logicaldisk where drivetype=2 get freespace /value ^| find "FreeSpace"') do set ssdfree=%%a
set /a ssdsizegb=%ssdsize%/1024/1024/1024
set /a ssdfreegb=%ssdfree%/1024/1024/1024
echo Size: %ssdsizegb% GB
echo Free space: %ssdfreegb% GB
echo.
pause
You don't consider having more than one disk in the system. Your for loops return the last found disk data only. Also, you shouldn't execute wmic for each desired data (wmic is quite slow, so you can improve performance by only executing it once)
Here is an example:
#ECHO off
setlocal enabledelayedexpansion
set i=0
for /f "skip=1 tokens=1-3" %%a in ('"wmic logicaldisk where drivetype=3 get freespace,name,size|findstr /v "^^$" "') do (
set /a i+=1
set "_Freespace[!i!]=%%a"
set "_Name[!i!]=%%b"
set "_Size[!i!]=%%c"
)
set _
Example output (on my system):
_Freespace[1]=21331169280
_Freespace[2]=1547910324224
_Freespace[3]=10803728384
_Freespace[4]=311614398464
_Name[1]=C:
_Name[2]=D:
_Name[3]=E:
_Name[4]=S:
_Size[1]=125791367168
_Size[2]=1973554245632
_Size[3]=26843541504
_Size[4]=960178941952
Also, be aware set /a works with INT32, which limits the range to 2GB. If the numbers for size or freespace are greater than that (very likely nowadays), you'll get an error message. You need another method for translating bytes into kB/MB/GB.

Remotely get serial number and model from list file and export it to arranged excel

I saw a nice script Here - this code get the Serial Number and Model from list of Remote Computers and export it to csv file.
The only problem is, that the export results have many separated lines instead of ONE line each computer.
I mean, i want to get this:
But i get that:
I also checked Here, but cant get it to work as described.
Here the Batch Code:
#echo off
del list.csv 2>nul
for /f %%i in (pclist.txt) do (
for /f "tokens=2 delims==" %%M in ('wmic /node:"%%i" csproduct get name /value') do for %%m in (%%M) do (
for /f "tokens=2 delims==" %%S in ('wmic /node:"%%i" bios get serialnumber /value') do for %%s in (%%S) do (
>>list.csv echo %%i, %%s, %%m
)
)
)
type list.csv
pause
How do i make the results to export to one line (display SN and model) to each computer?
You can have a go at this.
#echo off
break>list.csv
for /f "delims=" %%i in (pclist.txt) do call :method %%~i
type list.csv
goto :eof
:method
set "host_name=%~1"
for /f "tokens=2* delims==" %%N in ('wmic /node:"%~1" csproduct get name /value') do set "name=%%N"
for /f "tokens=2* delims==" %%S in ('wmic /node:"%~1" bios get serialnumber /value') do set "serial=%%S"
(echo %host_name%,%serial%,%name%)>>list.csv
We just get each of the values once, seeing as we do not incorporate for loops in for loops. Though we could have done this without setting variables, in this instance it makes it a little more readable.

Batch file to get current resolution on windows 10

I try to read the current resolution, when the resoltuitoon is not 4k a flag is that to false, when it changes back to 4K a video process is killed and restarted.
but I get always the maximal resolution in the screen, on different windows machines.
#echo off
set resolution=false
:loop
Set "WMIC_Command=wmic path Win32_VideoController get VideoModeDescription^,CurrentHorizontalResolution^,CurrentVerticalResolution^ /format:Value"
Set "H=CurrentHorizontalResolution"
Set "V=CurrentVerticalResolution"
Call :GetResolution %H% HorizontalResolution
Call :GetResolution %V% VerticalResolution
echo(
echo Screen Resolution is : %HorizontalResolution% x %VerticalResolution% = %resolution%
timeout /t 2
if %HorizontalResolution% == 3840 (
if %resolution% == false (
echo "hoppla die Auflösung wurde geaendert"
TASKKILL /F /FI "WINDOWTITLE eq SHH_Wandprojektion*"
echo "Video task gekillt"
timeout /t 2
start "E:\_projekte\STADTHOEFE_HAMBURG\PROJEKTION_VIDEO\mpc-hc.exe" "E:\_projekte\STADTHOEFE_HAMBURG\PROJEKTION_VIDEO\SHH_Wandprojektion_200123b_1.mp4"
echo "und wieder geoeffnet"
set resolution=true
)
) else (
set resolution=false
)
timeout /t 3
goto loop
::****************************************************
:GetResolution
FOR /F "tokens=2 delims==" %%I IN (
'%WMIC_Command% ^| find /I "%~1" 2^>^nul'
) DO FOR /F "delims=" %%A IN ("%%I") DO SET "%2=%%A"
Exit /b
::****************************************************
a alternative way also prints only the max resolution of the screen:
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"
if version lss 62 (
::set "wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value"
for /f "tokens=* delims=" %%# in ('wmic desktopmonitor get screenwidth /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "x=%%#"
)
for /f "tokens=* delims=" %%# in ('wmic desktopmonitor get screenheight /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "y=%%#"
)
) else (
::wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value
for /f "tokens=* delims=" %%# in ('wmic path Win32_VideoController get CurrentHorizontalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "x=%%#"
)
for /f "tokens=* delims=" %%# in ('wmic path Win32_VideoController get CurrentVerticalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "y=%%#"
)
)
echo Resolution %x%x%y%

Execute a script depending of the screen resolution

I tried to find the answer on some other threads, but i think that what i'm trying to do is a bit "specific". I'm not enough good with batch to adapt/concatenate parts of scripts i've found as well...
So, i'm trying to execute a command, depending of the running screen resolution.
The context is the following;
The command executed at the logon is placing the shortcuts on the desktop specifically, but it's not the same placement between the resolutions...
the idea is to define a variable, which is the answer of a wmic desktopmonitor get screenheight, screenwidth request. Then if the output contains 1080, so execute this cmd, else if it contains 720, execute another one, etc...
thats the cmd i use for win7 (working);
for /f "tokens=1-2 delims= " %%r in ('wmic desktopmonitor get screenheight^, screenwidth ^| findstr "1"') do set current_res=%%sx%%r
if "%current_res%" == "1920x1080" C:\Windows\kiosque\desktopok.exe /load /silent c:\windows\kiosque\dispo_icones_1080p.dok
i need to do the same with win10 with the wmic path Win32_VideoController get VideoModeDescription, but i didn't found how to define the output of this request properly as a variable...
You need different wmic queries depending on the windows version.Here's a resolution getter that depends on the version:
#echo off
::https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions
setlocal
for /f "tokens=4,5 delims=. " %%a in ('ver') do set "version=%%a%%b"
if version lss 62 (
::wmic_query=wmic desktopmonitor get screenheight, screenwidth /format:value"
for /f "tokens=* delims=" %%# in ('wmic desktopmonitor get screenwidth /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "x=%%#"
)
for /f "tokens=* delims=" %%# in ('wmic desktopmonitor get screenheight /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "y=%%#"
)
) else (
::wmic path Win32_VideoController get VideoModeDescription,CurrentVerticalResolution,CurrentHorizontalResolution /format:value
for /f "tokens=* delims=" %%# in ('wmic path Win32_VideoController get CurrentHorizontalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "x=%%#"
)
for /f "tokens=* delims=" %%# in ('wmic path Win32_VideoController get CurrentVerticalResolution /format:value') do (
for /f "tokens=2 delims==" %%# in ("%%#") do set "y=%%#"
)
)
echo Resolution %x%x%y%
::if "%x%x%y%" == "1920x1080" C:\Windows\kiosque\desktopok.exe /load /silent c:\windows\kiosque\dispo_icones_1080p.dok
endlocal
For windows 7 or earlier you need desktopmonitor class for the newer windows versions you need Win32_VideoController .you can try with dxdiag too:
#echo off
del ~.txt /q /f >nul 2>nul
start "" /w dxdiag /t ~
setlocal enableDelayedExpansion
set currmon=1
for /f "tokens=2 delims=:" %%a in ('find "Current Mode:" ~.txt') do (
echo Monitor !currmon! : %%a
set /a currmon=currmon+1
)
endlocal
del ~.txt /q /f >nul 2>nul
Because Win32_VideoController has been tested as working on my Windows 7 and Windows 10 systems, here are some Win32_VideoController examples:
Retrieving the horizontal resolution, as intimated in your question as the determining factor:
For /F "Delims=" %%A In (
'WMIC Path Win32_VideoController Get CurrentHorizontalResolution'
) Do For %%B In (%%A) Do Set "ResW=%%B"
Likewise if you wanted to check only the vertical resolution:
For /F "Delims=" %%A In (
'WMIC Path Win32_VideoController Get CurrentVerticalResolution'
) Do For %%B In (%%A) Do Set "ResH=%%B"
And if you wanted the resolution in WxH format:
#Echo Off
Set "WP=Path Win32_VideoController"
Set "WV=CurrentHorizontalResolution,CurrentVerticalResolution"
For /F "Skip=1 Tokens=*" %%A In ('"WMIC %WP% Get %WV%"'
) Do For /F "Tokens=1-2" %%B In ("%%A") Do Set ScRes=%%Bx%%C
Echo=%ScRes%
Pause
If you wanted a version which accounts for both DesktopMonitor and Win32_VideoController, then perhaps this will do, (from Vista onwards):
#Echo Off
Set "OV="
For /F "EOL=V" %%A In ('WMIc OS Get Version 2^>Nul'
) Do For /F "Tokens=1-2 Delims=." %%B In ("%%A") Do Set /A "OV=%%B%%C"
If Not Defined OV Exit /B
Set "ScRes=%%Cx%%B" & Set "WP=DesktopMonitor"
Set "WV=ScreenHeight,ScreenWidth"
If %OV% GEq 61 (Set "WP=Path Win32_VideoController" & Set "ScRes=%%Bx%%C"
Set "WV=CurrentHorizontalResolution,CurrentVerticalResolution")
For /F "Skip=1 Tokens=*" %%A In ('"WMIC %WP% Get %WV%"'
) Do For /F "Tokens=1-2" %%B In ("%%A") Do Set ScRes=%ScRes%
Echo=%ScRes%
Pause
I have left line 8 as GEq 61 for versions of at least Windows 7, because as I've stated, it works on my Windows 7 version.You could however change that to read Gtr 61 for Windows 8/Server 2012 and above, or even Gtr 63 if you want to limit it to anything above Windows 8.1/Server 2012 R2

Separate by semicolon wmic loop

Getting drives in variable
LogicalDisk = "c:\d:\"
now i want to get drives separated by semicolon;
like LogicalDisk = "c:\;d:\;e:\;f:\"
#echo on
setlocal enabledelayedexpansion
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=3" get name /format:value') do (
set "LogicalDisk=!LogicalDisk!%%d\"
)
echo %LogicalDisk%
endlocal
pause
either
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=3" get name /format:value') do (
set "LogicalDisk=!LogicalDisk!%%d\;"
)
set"LogicalDisk=%LogicalDisk:~0,-1%"
echo %LogicalDisk%
or
for /f "tokens=2 delims==" %%d in ('wmic logicaldisk where "drivetype=3" get name /format:value') do (
set "LogicalDisk=!LogicalDisk!%%d\"
)
set"LogicalDisk=%LogicalDisk:\=\;%"
set"LogicalDisk=%LogicalDisk:~0,-1%"
echo %LogicalDisk%

Resources