Having trouble storing output of "wmic csproduct get vendor /format:list | findstr/c=" as a variable

Viewed 393

Thank you for checking out my question. I am trying to:

  • get the output of a wmic query into a variable

Here is some background information for why I need to get this accomplished. The policy at my job requires that laptops and tablets equipped with webcams have them disabled in the BIOS.

Now that we are teleworking we have a need to enable the webcams in the BIOS.

We have tools that can be used to enable BIOS features while the system is running.

I want to write a script that will psexec into a machine, fetch the vendor information and store it in a variable that can be accessed later in the script once psexec exits.

I can write the rest of my script without issue I think, however I'm having trouble figuring out how to store the results of the following command in a variable:

wmic csproduct get vendor /format:list | findstr/c=

For example, the output of that command on my machine is:

Vendor=Dell Inc.



I want to capture the output and store it in a variable for later use in the script I'll be writing so that I can process vendor specific BIOS manipulation.

I have tried
set sysvend=wmic csproduct get vendor /format:list | findstr/c=
But when I echo %sysvend% to see if it worked I get the following instead of what I wanted:
wmic csproduct get vendor /format:list | findstr/c=

If anyone knows how to do this I would greatly appreciate the help. I am open to other alternatives for determining the vendor.

4 Answers

Even easier than using find or findstr and without the problematic 0x0D0D0A line endings:

@Echo Off & SetLocal EnableExtensions
For /F Tokens^=6Delims^=^" %%G In (
    '%__APPDIR__%wbem\WMIC.exe CSProduct Get Vendor /Format:MOF 2^>NUL'
) Do Set "vendor=%%G"

This solution also prevents unwanted trailing characters with the vendor name string, which can be a common occurrence.

There is a known wmic bug, so you need to parse wmic output using two nested loops, e.g. as follows:

FOR /F "tokens=1* delims==" %%s IN ('
    wmic csproduct get vendor /value ^| find "="
') DO for /F "tokens=*" %%i in ("%%t") do SET "vendor=%%i"

Here the for loops are

  • %%s to retrieve the vendor value;
  • %%i to remove the ending carriage return in the value returned (wmic behaviour): each output line ends with 0x0D0D0A (CR+CR+LF) instead of common 0x0D0A (CR+LF).

See Dave Benham's WMIC and FOR /F: A fix for the trailing <CR> problem

Use for %%i in (wmic csproduct get vendor /format:list ^| findstr/c=) do (set var=%%~I) What this does is that the for loop variable %%i becomes the output of the command in the for loop which allows you to set another variable the for loop variable. You need the caret (^) before the | in the for loop since if you didn't, the command would pipe out of the for loop and not execute in the for loop. You could also just surround the command with double quotes (") and you wouldn't need to escape the pipe. As a result of the command, %var% would become equal to the output of your wmic command.

The final command will be:

@echo off
for /f "tokens=* delims==" %%i in (' wmic csproduct get vendor /format:list ^| findstr /c:"=" ') do (set var=%%~i)
echo %var%
rem echoing var to make sure the variable is what you wanted

Output for me:

Vendor=Microsoft Corporation

If you want the output to be Microsoft Corporation specifically, you can add:

set var=%var:~7%

to remove the first 7 characters. You can also change tokens to 2* instead of *. :

for /f "tokens=2* delims==" %%j in (' wmic csproduct get vendor /format:list ^| findstr /c:"=" ') do (set var=%%~j)

Outputs:

Microsoft Corporation

Notes:

In batch-files, unlike powershell, you cannot set a variable as a command and have it become the output which is why the conventional way to set output of a command to a variable is using the for loop. Also, just preference, I would suggest using the /value switch rather than /format:list.

Tested and works for me

Answer posted to demonstrate the command can be assigned and executed as a variable, Contrary to Neko's answer, as per the OP's question.

@Echo Off
Set Get.Vendor=^>"%TEMP%\Vendor.log" (wmic csproduct get vendor /value ^| 
findstr /c:=) ^&^& (For /F "UsebackQ Tokens=2* Delims==^EOL" %%A In ("%TEMP%\Vendor.log") Do For /F "Delims=" %%T In ("%%A") Do Set "Vendor=%%~T") ^&^& Echo.!Vendor!
Setlocal EnableDelayedExpansion
%Get.Vendor%
pause
Related