Passing a variable back from WMI

C

Chris S

Hi there - I have never used WMI before but have been thrown in the deep
end. I need to determine the PC Model via WMI.

I am running a batch file that calls a WMI script (below). When I run the
script below (with a .vbs extension) alone I get a prompt giving me the PC
model, but I need to pass this back into the original batch file that called
this .vbs file. Does anybody have any suggestions?

<BEGIN SCRIPT>
set CSset = GetObject("winmgmts:").InstancesOf ("Win32_ComputerSystem")
for each CS in CSset
outtext = CS.Model
Next

wscript.echo outtext

wscript.quit 0
<ENDSCRIPT>

Thanks
Chris
 
P

Peter Falz

Hi Chris,

Chris S said:
Hi there - I have never used WMI before but have been thrown in the deep
end. I need to determine the PC Model via WMI. OK.

I am running a batch file that calls a WMI script (below). When I run the
script below (with a .vbs extension) alone I get a prompt giving me the PC
model, but I need to pass this back into the original batch file that called
this .vbs file. Does anybody have any suggestions?
What do you mean with "pass back into the original ... ..."?
Do you mean, that want to use, belongs to your script, the content
of outtext?
<BEGIN SCRIPT>
set CSset = GetObject("winmgmts:").InstancesOf ("Win32_ComputerSystem")
for each CS in CSset
outtext = CS.Model
Next
wscript.echo outtext
wscript.quit 0
<ENDSCRIPT>

What kind of script is the original script? Also VBScript?
If yes, program a function in your original script:


Public Function GetCSModel()
Dim objCSset
Dim objCS

Set objCSset = GetObject( "winmgmts:" ).InstancesOf ( "Win32_ComputerSystem" )

For Each objCS In objCSset
GetCSModel = objCS.Model
Exit For
Next
End Function


After this you can call the function.
.... ... = GetCSModel()

But this is IMO not problem with WMI,
IMO, your problem is VBScript, isn't it?

The code, i've listed before is not tested.

HTH

Bye
Peter
 
C

Chris S

Hi Peter - thanks for your response.
I am running a .cmd file originally (I need to determine what model PC and
run the appropriate graphics driver)
So am running a simple .cmd file that I was hoping to use to call a VBS file
(that takes care of the WMI component) and is able to send back a value to
my original .cmd file that I can use.
Yes I need to use the output of the VBS part into my .cmd
Chris
 
P

Peter Falz

Hi Chris,

that is not a problem of WMI and IMO OT here.
Sorry, but i've no idea how to use a output of VBS-file
in CMD-File.

Maybe this is a starting-point:

If you run this line in the VBS-file
wscript.echo outtext

and if you call the script in your CMD-File with
cscript myScript.vbs > output.txt

you have the output of the script in a TEXT-File
and so, you can use this in your CMD-File.

File, file, file, ... ;-)

HTH

Bye
Peter
 
T

Torgeir Bakken \(MVP\)

Chris said:
Hi there - I have never used WMI before but have been thrown in the deep
end. I need to determine the PC Model via WMI.

I am running a batch file that calls a WMI script (below). When I run the
script below (with a .vbs extension) alone I get a prompt giving me the PC
model, but I need to pass this back into the original batch file that called
this .vbs file. Does anybody have any suggestions?

<BEGIN SCRIPT>
set CSset = GetObject("winmgmts:").InstancesOf ("Win32_ComputerSystem")
for each CS in CSset
outtext = CS.Model
Next

wscript.echo outtext

wscript.quit 0
<ENDSCRIPT>
Hi

Use "for /f" to retrieve the value.

Here is a batch file example that even creates the VBScript on the
fly from inside the batch file:

::--------------------8<----------------------
@echo off
setlocal
set tmpfile=%tmp%\CS.vbs
echo Set CSset = GetObject("winmgmts:")._ >%tmpfile%
echo InstancesOf("Win32_ComputerSystem") >>%tmpfile%
echo For Each CS In CSset >>%tmpfile%
echo WScript.Echo CS.Model >>%tmpfile%
echo Next >>%tmpfile%

for /f "tokens=*" %%a in (
'cscript.exe //Nologo %tmpfile%') do set model=%%a

del %tmpfile%
echo Model is: %model%
endlocal

::--------------------8<----------------------
 
C

Chris S

That works for me - thanks for your help
Chris



Torgeir Bakken (MVP) said:
Hi

Use "for /f" to retrieve the value.

Here is a batch file example that even creates the VBScript on the
fly from inside the batch file:

::--------------------8<----------------------
@echo off
setlocal
set tmpfile=%tmp%\CS.vbs
echo Set CSset = GetObject("winmgmts:")._ >%tmpfile%
echo InstancesOf("Win32_ComputerSystem") >>%tmpfile%
echo For Each CS In CSset >>%tmpfile%
echo WScript.Echo CS.Model >>%tmpfile%
echo Next >>%tmpfile%

for /f "tokens=*" %%a in (
'cscript.exe //Nologo %tmpfile%') do set model=%%a

del %tmpfile%
echo Model is: %model%
endlocal

::--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top