# Solved: WMI Script Question



## scrfix (May 3, 2009)

I went out to search out if it was possible via command line to determine what type of system someone was one, ie. Desktop, Laptop, etc etc etc.

I found out that WMI can do this and I have a script that can do this. However, the script opens a pop-up window. I want this returned to the command line so that I can save the actual number in a variable (objItem).

Want a laugh, check out what Microsoft has for defaults on this?
http://msdn.microsoft.com/en-us/library/aa394474(VS.85).aspx
This script will return the type of chasis on the computer.
Read the type of chasis returns that are programmed in:
5. Pizza Box
16. Lunch Box

*Here is the code.*
1. Anyone know how to write WMI and know how to accomplish outputting this to the command line?

2. Is there a way to utilize this variable in my current running bat file?
(I know that I can set variable=objItem). Just want to know if it is possible to utilize that variable in my current running bat file that will call the wmi script)


```
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & strComputer & "\root\cimv2")
Set colChassis = objWMIService.ExecQuery _
    ("Select * from Win32_SystemEnclosure")
For Each objChassis in colChassis
    For Each objItem in objChassis.ChassisTypes
        Wscript.Echo "Chassis Type: " & objItem
    Next
Next
```


----------



## ghostdog74 (Dec 7, 2005)

save it as myscript.vbs, and in batch, use a for loop to get the result back to batch


```
for ..... ( 'cscript /nologo myscript.vbs' ) do (
...
)
```
but, why use batch, when everything you need can be done with vbscript....


----------



## scrfix (May 3, 2009)

I currently have over 600 lines of batch code for automating everything on the systems that I maintain.

Of course this would come back to the dreaded FOR loop. Dun dun dun... I have grown to very much dislike those things.

This is just one more item that will make my life easier.

Thanks


----------



## Squashman (Apr 4, 2003)

From RobVan's website.
http://www.robvanderwoude.com/files/hardware_xp.txt

```
FOR /F "tokens=2 delims=={}" %%A IN ('WMIC /Node:%Node% SystemEnclosure Get ChassisTypes /format:list') DO (
	IF %%A EQU  1 SET csvChassis=,Maybe Virtual Machine
	IF %%A EQU  2 SET csvChassis=,??
	IF %%A EQU  3 SET csvChassis=,Desktop
	IF %%A EQU  4 SET csvChassis=,Thin Desktop
	IF %%A EQU  5 SET csvChassis=,Pizza Box
	IF %%A EQU  6 SET csvChassis=,Mini Tower
	IF %%A EQU  7 SET csvChassis=,Full Tower
	IF %%A EQU  8 SET csvChassis=,Portable
	IF %%A EQU  9 SET csvChassis=,Laptop
	IF %%A EQU 10 SET csvChassis=,Notebook
	IF %%A EQU 11 SET csvChassis=,Hand Held
	IF %%A EQU 12 SET csvChassis=,Docking Station
	IF %%A EQU 13 SET csvChassis=,All in One
	IF %%A EQU 14 SET csvChassis=,Sub Notebook
	IF %%A EQU 15 SET csvChassis=,Space-Saving
	IF %%A EQU 16 SET csvChassis=,Lunch Box
	IF %%A EQU 17 SET csvChassis=,Main System Chassis
	IF %%A EQU 18 SET csvChassis=,Lunch Box
	IF %%A EQU 19 SET csvChassis=,SubChassis
	IF %%A EQU 20 SET csvChassis=,Bus Expansion Chassis
	IF %%A EQU 21 SET csvChassis=,Peripheral Chassis
	IF %%A EQU 22 SET csvChassis=,Storage Chassis
	IF %%A EQU 23 SET csvChassis=,Rack Mount Unit
	IF %%A EQU 24 SET csvChassis=,Sealed-Case PC
)
```


----------



## scrfix (May 3, 2009)

The one above caused an error on my Vista system however I did find one that Carlos wrote on Google Groups but this one has a slight error that I am not sure why it is being caused.


```
@echo off 
setlocal 
set /a "chassNum=0" 
set "command=2^>nul WMIC SystemEnclosure Get ChassisTypes /value" 
for /f "tokens=2 delims=={}" %%A IN ('%command%') do ( 
2>nul set /a "chassNum=%%A" 
) 
if %chassNum%==0 (set /a "chassNum=2") 
set _=;Other;Unknown;Desktop;Low Profile Desktop; 
set _=%_%;Pizza Box;Mini Tower;Tower;Portable;Laptop;Notebook; 
set _=%_%;Hand Held;Docking Station;All in One;Sub Notebook; 
set _=%_%;Space-Saving;Lunch Box;Main System Chassis;Expansion Chassis; 
set _=%_%;SubChassis;Bus Expansion Chassis;Peripheral Chassis; 
set _=%_%;Storage Chassis;Rack Mount Chassis;Sealed-Case PC; 
for /f "tokens=%chassNum% delims=; eol=" %%A in ("%_%") do ( 
echo Chassis type is : %ChassNum% : %%A 
) 
pause
```
*The Error:*
I have an Inspiron 9400 with a Chassis and it returns chassis number 8 which is portable however the programming returns Tower.

I did modify the names to be correct names from what Carlos had however the error was there prior to changing those names?

I am not sure how this even works because of how it is programmed. Any ideas why it is doing that?

I am going to continue working with it in the meantime.


----------



## Squashman (Apr 4, 2003)

Can't help you solve the error unless you tell me what happened!
I just tested the For Loop on my Vista Laptop from Rob's script and it ran just fine. Came back as portable for the chassis type.


----------



## scrfix (May 3, 2009)

*The Error:*
I have an Inspiron 9400 with a Chassis and it returns chassis number 8 which is portable however the programming returns Tower.

This is supposed to return portable. It does not. It returns tower. It is off by one number.

If you go here:
http://msdn.microsoft.com/en-us/library/aa394474(VS.85).aspx

About 1/4 the way down you will see the chassistype returns.
When you run that script, it returns the correct number, ie 8 which is for portable according to that webpage however the script returns back Tower because of how it is programmed. It thinks #8 is Tower. It is portable.

*Updated*
If you utilize this code, you will see what I am talking about.


```
@echo off 
setlocal 
set /a "chassNum=0" 
set "command=2^>nul WMIC SystemEnclosure Get ChassisTypes /value" 
for /f "tokens=2 delims=={}" %%A IN ('%command%') do ( 
2>nul set /a "chassNum=%%A" 
) 
if %chassNum%==0 (set /a "chassNum=2") 
set _=;Other1;Unknown2;Desktop3;Low Profile Desktop4; 
set _=%_%;Pizza Box5;Mini Tower6;Tower7;Portable8;Laptop9;Notebook10; 
set _=%_%;Hand Held;Docking Station;All in One;Sub Notebook; 
set _=%_%;Space-Saving;Lunch Box;Main System Chassis;Expansion Chassis; 
set _=%_%;SubChassis;Bus Expansion Chassis;Peripheral Chassis; 
set _=%_%;Storage Chassis;Rack Mount Chassis;Sealed-Case PC; 
for /f "tokens=%chassNum% delims=; eol=" %%A in ("%_%") do ( 
echo Chassis type is : %ChassNum% : %%A 
) 
pause
```
It returns Tower7 even though the chassis returns 8. It should return Portable8.


----------



## Squashman (Apr 4, 2003)

I was asking for the error you got from Rob's code!


----------



## Squashman (Apr 4, 2003)

He ends each set statement with a semi-colon and begins each set statement with a semi-colon. I don't know why you want to use this convoluted code when Rob's works just fine. The last for loop is using the Chassis number as the Tokens. Since he is adding in extra delimiters it doesn't find the right description for the Chassis type. WMIC does find the correct chassis type though, so you just need to clean up his code by removing the extra delimiters.


----------



## TheOutcaste (Aug 8, 2007)

Squashman said:


> Since he is adding in extra delimiters it doesn't find the right description for the Chassis type. WMIC does find the correct chassis type though, so you just need to clean up his code by removing the extra delimiters.


Exactly right, the extra ; is causing the problem.
You end up with this for the _ variable (partial shown):

```
[COLOR=DarkRed]Low Profile Desktop4[/COLOR][COLOR=Red];;[/COLOR][COLOR=Blue]Pizza Box5[/COLOR];Mini Tower6;Tower7;Portable8;Laptop9;Notebook10;
```
4 is Low Profile Desktop4, 5 is null (token between the ;;)
6 is Pizza Box5
11-14 will be off by 2, 15-18 off by 3, and so on


----------



## scrfix (May 3, 2009)

The error from the original code:
*Get - Alias not found.*

Okay, I cleaned up the code I got from the guy on Google groups.

I removed the "; " at the end of each line and the ; before other on the first line and it works great or seems to now.

It now looks like:

```
@echo off 
setlocal 
set /a "chassNum=0" 
set "command=2^>nul WMIC SystemEnclosure Get ChassisTypes /value" 
for /f "tokens=2 delims=={}" %%A IN ('%command%') do ( 
2>nul set /a "chassNum=%%A" 
) 
if %chassNum%==0 (set /a "chassNum=2")
set _=Other;Unknown;Desktop;Low Profile Desktop
set _=%_%;Pizza Box;Mini Tower;Tower;Portable;Laptop;Notebook
set _=%_%;Hand Held;Docking Station;All in One;Sub Notebook
set _=%_%;Space-Saving;Lunch Box;Main System Chassis;Expansion Chassis
set _=%_%;SubChassis;Bus Expansion Chassis;Peripheral Chassis
set _=%_%;Storage Chassis;Rack Mount Chassis;Sealed-Case PC
for /f "tokens=%chassNum% delims=; eol=" %%A in ("%_%") do ( 
echo Chassis type is : %ChassNum% : %%A 
) 
pause
```
Well, I was going to write something similar to the original persons code with If If If If If If If and then you posted it and I thought wow, thanks for finding that... what a time saver from testing. Well, then I just went to utilize it and received the Get error above.

I went and researched and found the original code. I must say dayum when I looked at that guys website and he has about a gazillion premade scripts that do everything you could possibly want. That is a website worth keeping. The original code that the above script came out of was way more than I was looking to get into so I started some more research on writing my own and ran across Google Groups code where people were testing. The guy came out with the above code. His seemed to work with that very minor issue however now that we have that issue apparently resolved it seems to be the only one that works on my system if that answers your question on utilizing the code vs the other code.


----------



## TheOutcaste (Aug 8, 2007)

scrfix said:


> The error from the original code:
> *Get - Alias not found.*


If you don't define the node variable you'll get that error. Just need to remove the */Node:%node%* part. Or add the 

```
IF "%~1"=="" (SET Node=%ComputerName%) ELSE (SET Node=%~1)
```
 line back in from the complete code Squashman linked to.
The csvChassis variable will start with a comma. If you don't need the comma, you can remove it from each of the Set statements.


----------



## Squashman (Apr 4, 2003)

I guess I assumed he knew what the /node switch was doing. That is why I linked to the orginal code. I guess I just continue to make an @$$ of myself for assuming.


----------



## scrfix (May 3, 2009)

I still am pretty much a beginner at most of this but learning extremely fast. I can do things I never even knew existed and have learned how to read and understand most of these /? output screens which I could not do before I started. I also have a great amount of references that I utilize thanks to you guys. You guys however are still my best reference. There is no replacement for experience and knowledge.

I should have seen that %node%. Didn't catch it. I did finally figure out what that other Carlos guy was doing with his code.

Is there any downfall to using his code. I like it because it is all together and now that I know what it is doing, easier for me to read and handle. Are there bugs that I cannot see that will screw me up later on that you guys can spot that I may not be able to see?

Thanks for all of your help on this.


----------



## Squashman (Apr 4, 2003)

As far as what Jerry and I were discussing last night, Carlos's code will just run slightly slower because you have two For Loops. He could have just done one set statement, but I suppose he kept adding it to it for readability of the script.

I personally don't think Carlos' code is very clean. Too convoluted to me. Rob's code is a single For Loop. You can't get much tighter code than that. Sure you got a lot of if statements but I think it is more readable and more understandable.


----------



## TheOutcaste (Aug 8, 2007)

I would change the delimiter from semicolon to a comma, then you could get rid of the *eol=* part in the 2nd loop. But then I just prefer a comma to a semicolon as a delimiter.

And be aware that this redirects all errors to Nul (2>Nul), so if there is an error, you won't see it. It will instead return a Chassis type of 2.


----------



## scrfix (May 3, 2009)

Ok,

The error redirecting I don't like. I will look at that. The comma I will change.


----------

