1

I'm looking to use the 'wmic os get caption' command to return the OS type and compare it to a string. I've been at this for a few days.
Help is appreciated.

for %%G in ('wmic os get caption') do (
    if %%G == "%Server%"
        echo "Server"
    else
        echo "PC"
)
Dominique
  • 16,450
  • 15
  • 56
  • 112
Jaron B
  • 11
  • 1
  • 1
    We cannot really help without knowing how the environment variable `Server` is defined at all. But the __FOR__ command line cannot work at all if you want to process the Unicode encoded output of `wmic os get caption` because of missing option `/F`. Open a [command prompt](https://www.howtogeek.com/235101/), run `for /?` and read the output help carefully and completely from top of first to bottom of last page. Further a string on left side without `"` is never equal a string with `"` on right side as `if` compares the strings with the quotes. `if "%%G" == "%Server%"` could be more useful. – Mofi Sep 29 '20 at 13:30
  • 1
    `%SystemRoot%\System32\wbem\wmic.exe OS GET Caption /VALUE | %SystemRoot%\System32\find.exe /I "Server" && echo Server|| echo PC` runs `wmic` with full qualified file name to output the OS string in one non-empty UTF-16 LE encoded line which is redirected to `find` with full qualified file name searching case-insensitive for the string `Server` (without the quotes) resulting on a success to output the string `Server` and otherwise the string `PC`. See also [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). – Mofi Sep 29 '20 at 13:40
  • Mofi - that worked; thanks! – Jaron B Sep 29 '20 at 14:33

3 Answers3

0

From Mofi:

%SystemRoot%\System32\wbem\wmic.exe OS GET Caption /VALUE | %SystemRoot%\System32\find.exe /I "Server" && echo Server|| echo PC
Mofi
  • 46,139
  • 17
  • 80
  • 143
Jaron B
  • 11
  • 1
0

try like this:

@echo off
setlocal enableDelayedExpansion

set "v=Microsoft Windows 10 Pro"

for /f "tokens=* delims=" %%A  in ('wmic os get caption /format:value') do for /f "tokens=* delims="  %%# in ("%%A") do (
    set "%%#"
    if "!Caption!" == "%v%" (
        echo "Server"
    ) else (
        echo "PC"
    )
)

wmic is in unicode and with one additional for loop processing you can convert it to ascii. Your usage of if also needs some tuning - to use new lines in if you need brackets , also you should be aware that the quotes are also compared so both strings needs to be enclosed with double quotes.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

In my opinion, given the stated requirements, there's no need for a , and you could use a more realistic property too.

If you take a look at the value list for ProductType under Win32_OperatingSystem, there are only three, Work Station (1), Domain Controller (2), and Server (3). As a Domain Controller is technically a Server too, all you need to check for is a ProductType of 1, which for you, will be a PC.

"%SystemRoot%\System32\wbem\WMIC.exe" OS Where ProductType="1" Get Name 2> NUL | "%SystemRoot%\System32\find.exe" "|" 1> NUL && (Echo PC) || Echo Server
Compo
  • 36,585
  • 5
  • 27
  • 39