• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1811
  • Last Modified:

Creating an evironment variable using an batch script with WMI

I've been googling for hours now and just can't find any information. I'm not really formiliar with batch programming and don't know how to some things.
First of all I want to set a variable with a wmi command. This is the command for looking up a serial key of an computer: wmic csproduct get IdentifyingNumber

I want this in a variable because I want to write this to an environment variable on a computer. Does anybody know how to set a variable with the value from a dos commandline function and how do I create an environment variable with the value from the dos command.

I hope somebody can help me because I've been searching for so long and can't find it. this i the code so far with some tests:

@echo off
rem clear screen
cls
rem Variables

rem Start
echo #####################################################
echo #################-=][ WMI BATCH ][=-#################
echo #####################################################

rem Display the value from the command
wmic csproduct get IdentifyingNumber /value

rem Declare the variable
set _IDNumber = wmic csproduct get IdentifyingNumber /value

rem Display the value of the variable
echo %_IDNumber%

echo on


Thanx in advance.
0
Sander Stad
Asked:
Sander Stad
1 Solution
 
InteractiveMindCommented:
Hey sstad.

You can capture the output of a command, in MS-DOS, like so:

for /f "delims=" %%O in ('wmic csproduct get IdentifyingNumber /value') do set _IDNumber=%%O

Now, you can just do:

echo %_IDNumber%

As for assigning it as an Environment Variable, it differs with OS... What Operating System are you using for this?

Regards,
>> IM
0
 
sr75Commented:
I have never done it, but environmental variables reside in the HKCU>Environment and HKCU>Volatile Environment keys.  Since you are using WMI, you should be able to access the registry and add the necessary values to these keys.  I however would do it in a vbscript and not in a batch script.  Here are some examples from MS:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/wmi_tasks__connecting_to_the_wmi_service.asp 
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now