1

Really struggling with WMI syntax any help would be much appreciated

I have 2 reg keys and I want to run a WMI query to determine if my PC is a laptop or a desktop. Then, I want to import a reg key based on the result. If desktop, import desktop.reg, If laptop, import laptop.reg.

The WMI query that I have been using is:

wmic path Win32_PhysicalMemory get FormFactor

and an ouput of 12 = SODIMM memory, so a laptop. How can implement the following?:

IF "formfactor"="12" ( do this ) else ( do that ) 

Point in the right direction much appreciated!

double-beep
  • 5,031
  • 17
  • 33
  • 41
rampengu
  • 51
  • 3
  • 2
    Possible duplicate of [How do I get the result of a command in a variable in windows?](https://stackoverflow.com/questions/108439/how-do-i-get-the-result-of-a-command-in-a-variable-in-windows) – Squashman Nov 26 '18 at 15:03
  • 2
    Possible duplicate of [Set Variable to value of command](https://stackoverflow.com/questions/38742917/set-variable-to-value-of-command) – JosefZ Nov 26 '18 at 15:18
  • 1
    I see no reason not to use, `Wmic Path Win32_PhysicalMemory Where "FormFactor='12'" Get...`, _although I doubt that this is the best way to determine a notebook from a desktop_. – Compo Nov 26 '18 at 15:50
  • 1
    @Compo you are right, i have since switched to wmic path Win32_ComputerSystem get PCSystemType where 1 is desktop, 2 is laptop. this is more consistent as we have some all in one system that have small memory type. but the code for script is the same just swap a small part – rampengu Nov 26 '18 at 15:56
  • 1
    For more accuracy you could also take a look at, `Win32_SystemEnclosure`. – Compo Nov 26 '18 at 16:36

2 Answers2

3

Although a for /f loop (as shown by aschipfl) is the correct method to get a commands output to a variable, it's not really needed here:

wmic path Win32_PhysicalMemory get FormFactor|find "12" >nul && goto :Laptop || goto :desktop
goto :eof
:desktop
  echo This is a desktop
  goto :eof
:laptop
  echo this is a laptop
  goto :eof
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I was about to implement this method (actually based on `findstr`) into my answer, but you were faster, obviously... – aschipfl Nov 26 '18 at 15:28
  • thank you so much! 2 different approaches, both approaches have improved my knowledge. many thanks – rampengu Nov 26 '18 at 15:54
2

Use a for /F loop loop to capture the output of your wmic command line:

for /F "skip=1" %%E in ('
    wmic path Win32_PhysicalMemory get FormFactor
') do for /F %%F in ("%%E") do set "FF=%%F"
if %FF% equ 12 (
    echo FormFactor is 12.
) else (
    echo FormFactor is not 12.
)

The second for /F loop in this example prevents artefacts (like orphaned carriage-return characters) from converting the Unicode output of wmic to ANSI by `for /F.

If there are more than one memory module, the for /F loop is iterating over all of them, so the interim variable FF actually contains the form factor of the lastly iterated one.

If you want to execute the code in a command prompt window rather than in a batch file, regard that you have to replace %%E and %%F by %E and %F, respectively.


You can let the wmic command do the filtering using a where clause:

wmic path Win32_PhysicalMemory where FormFactor=12 get FormFactor

Then use the find command to check whether or not there are matching items, like this:

2> nul wmic path Win32_PhysicalMemory where FormFactor=12 get FormFactor /VALUE | > nul find "=" && (
    echo FormFactor is 12.
) || (
    echo FormFactor is not 12.
)

The /VALUE switch changes the output of wmic to something like FormFactor=12; find is then used to find returned lines containing =. Due to the said filtering by where there is no matching output at all if there is no such expected form factor. The && and || operators are conditional operators that react on the returned exit code of find.


Anyway, determining the form factor of the memory modules is probably not the most reliable method to find out whether your computer is a laptop (mobile); so as you already mentioned in a comment, the Win32_ComputerSystem class is a more suitable one:

2> nul wmic path Win32_ComputerSystem where PCSystemType=2 get PCSystemType /VALUE | > nul find "=" && (
    echo The computer is a laptop.
) || (
    echo The computer is not a laptop.
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • thank you so much for your reply. I think for /F is exactly what i'm looking for with regards to your example, i can't seem to get it to work. should i just be able to run it as a batch script? – rampengu Nov 26 '18 at 15:11
  • Sorry, I forgot about skipping the header `FormFactor` of the output and the Unicode-to-ANSI conversion issue -- see my edit... – aschipfl Nov 26 '18 at 15:21
  • `if %%F equ 12 (` is out of any `FOR` scope. Should be `if %FF% equ 12 (` – JosefZ Nov 26 '18 at 15:24
  • Oh yes, @JosefZ, thank you; this was kind of a typo; originally I planned to place the `if` condition directly into the loop body, but I decided to change that, because it would be executed multiple times in case there are more than one memory module; I assume that they are all of the same form factor though... – aschipfl Nov 26 '18 at 15:26