Welcome to ServerForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Script and request for comments: Creating shell-level vari..

 
   Windows Server (Home) -> Windows Server Scripting RSS
Next:  WMI Script Modification  
Author Message
Alex K. Angelopoulos [MVP

External


Since: Jun 27, 2004
Posts: 50



(Msg. 1) Posted: Tue Aug 26, 2003 8:29 am
Post subject: Script and request for comments: Creating shell-level variables from WMI data
Archived from groups: microsoft>public>scripting>vbscript, others (more info?)

(Follow-up set to microsoft.public.scripting.wsh).
Yet another script written in connection with the Windows Deployment
Resource Kit coming soon from Microsoft Press.
=====================================================================

One of the minor annoyances for WMI and other data into semi-persistent
variables that can be used easily from batch scripts. There are several ways
to do this type of thing; we were after something fairly simple to extend
and replicate by different people, with name scoping to make it easier to
see what goes where and avoid collisions with prior variables, and with
limited persistence to prevent possible problems with additions to the
environment.

A technique we're using is to read an XML file containing some instructions
about a WMI namespace, properties, and variable scope, and a special prefix
to make the variables "look" scoped. The following script does this and
creates VOLATILE variables from the items in the XML file. Feel free to use
this; and of course comments and ideas are appreciated.

Note that the script is followed by an example XML file.


===================================================

' note that you must supply the path to the xml file as an argument!
src = wscript.arguments(0)
' C:\data\windeploy\working\wmi2env\win32_operatingsystem.xml
set xmldoc = createobject("MSXML2.FreeThreadedDOMDocument")

die_if not xmldoc.load(src), "Could not load " & src, 3

set root = xmldoc.documentElement
die_if not ucase(root.nodeName) = "ENVIRONMENT", _
"Could not find Environment root in " & src, 4

wmiclass = GetAttributeOrDefault(root, "wmiclass", false)
die_if wmiclass = false, "could not find wmiclass attribute for " _
& root.nodeName & " in " & src, 5


set sh = createobject("WScript.Shell")
set Env = sh.Environment(GetAttributeOrDefault(root, "scope", "VOLATILE"))
prefix = GetAttributeOrDefault(root, "prefix", "_") _
& GetAttributeOrDefault(root, "delimiter", ".")
set properties = root.selectNodes("property")

computer = "."
set WmiSvc = GetObject("winmgmts:\\" & computer & "\root\CIMV2")
set instances = WmiSvc.ExecQuery("SELECT * FROM " & wmiclass, "WQL", 4Cool
For Each instance in instances
For Each prop in properties
with instance.Properties_.Item(AttributeValue(prop, "name"))
Env.Item(ucase(prefix & .Name)) = .Value
end with
Next
wscript.quit
Next

function GetAttributeOrDefault(node, attributeName, defaultValue)
if hasAttribute(node, attributeName) then
GetAttributeOrDefault = AttributeValue(root, attributeName)
else
GetAttributeOrDefault = defaultValue
end if
end function

function hasAttribute(node, attributeName)
dim attribute: hasAttribute = false
for each attribute in node.attributes
if attributeName = attribute.baseName then
hasAttribute = true
exit function
end if
next
end function

function AttributeValue(node, attributeName)
AttributeValue = node.attributes.getNamedItem(attributeName).text
end function

sub die_if(expression, errorText, exitcode)
if expression then
WScript.StdErr.WriteLine errorText
WScript.Quit exitCode
end if
end sub


===================================================


<?xml version="1.0"?>
<Environment prefix="OS" wmiclass="Win32_OperatingSystem">
<property name="BootDevice"/>
<property name="Buildnumber"/>
<property name="BuildType"/>
<property name="CodeSet"/>
<property name="CSDVersion"/>
<property name="CurrentTimeZone"/>
<property name="Debug"/>
<property name="Distributed"/>
<property name="EncryptionLevel"/>
<property name="ForegroundApplicationBoost"/>
<property name="InstallDate"/>
<property name="LastBootUpTime"/>
<property name="Locale"/>
<property name="OSLanguage"/>
<property name="OSType"/>
<property name="ProductType"/>
<property name="SerialNumber"/>
<property name="ServicePackMajorVersion"/>
<property name="ServicePackMinorVersion"/>
<property name="SuiteMask"/>
<property name="Version"/>
</Environment>

 >> Stay informed about: Script and request for comments: Creating shell-level vari.. 
Back to top
Login to vote
jesseh

External


Since: Aug 26, 2003
Posts: 4



(Msg. 2) Posted: Tue Aug 26, 2003 11:58 am
Post subject: Script and request for comments: Creating shell-level variables from WMI data [Login to view extended thread Info.]
Archived from groups: microsoft>public>windows>server>scripting (more info?)

Just throwing this out here...

I think WMIC has some kind of conversion process that is
used to create xml files... am I thinking of the same
thing you are or is this something different?


>-----Original Message-----
>(Follow-up set to microsoft.public.scripting.wsh).
>Yet another script written in connection with the
Windows Deployment
>Resource Kit coming soon from Microsoft Press.
>=========================================================
============
>
>One of the minor annoyances for WMI and other data into
semi-persistent
>variables that can be used easily from batch scripts.
There are several ways
>to do this type of thing; we were after something fairly
simple to extend
>and replicate by different people, with name scoping to
make it easier to
>see what goes where and avoid collisions with prior
variables, and with
>limited persistence to prevent possible problems with
additions to the
>environment.
>
>A technique we're using is to read an XML file
containing some instructions
>about a WMI namespace, properties, and variable scope,
and a special prefix
>to make the variables "look" scoped. The following
script does this and
>creates VOLATILE variables from the items in the XML
file. Feel free to use
>this; and of course comments and ideas are appreciated.
>
>Note that the script is followed by an example XML file.
>
>
>===================================================
>
>' note that you must supply the path to the xml file as
an argument!
>src = wscript.arguments(0)
>'
C:\data\windeploy\working\wmi2env\win32_operatingsystem.xm
l
>set xmldoc = createobject
("MSXML2.FreeThreadedDOMDocument")
>
>die_if not xmldoc.load(src), "Could not load " & src, 3
>
>set root = xmldoc.documentElement
>die_if not ucase(root.nodeName) = "ENVIRONMENT", _
> "Could not find Environment root in " & src, 4
>
>wmiclass = GetAttributeOrDefault(root, "wmiclass", false)
>die_if wmiclass = false, "could not find wmiclass
attribute for " _
> & root.nodeName & " in " & src, 5
>
>
>set sh = createobject("WScript.Shell")
>set Env = sh.Environment(GetAttributeOrDefault
(root, "scope", "VOLATILE"))
>prefix = GetAttributeOrDefault(root, "prefix", "_") _
> & GetAttributeOrDefault(root, "delimiter", ".")
>set properties = root.selectNodes("property")
>
>computer = "."
>set WmiSvc = GetObject("winmgmts:\\" & computer
& "\root\CIMV2")
>set instances = WmiSvc.ExecQuery("SELECT * FROM " &
wmiclass, "WQL", 4Cool
>For Each instance in instances
> For Each prop in properties
> with instance.Properties_.Item(AttributeValue
(prop, "name"))
> Env.Item(ucase(prefix & .Name)) = .Value
> end with
> Next
> wscript.quit
>Next
>
>function GetAttributeOrDefault(node, attributeName,
defaultValue)
> if hasAttribute(node, attributeName) then
> GetAttributeOrDefault = AttributeValue(root,
attributeName)
> else
> GetAttributeOrDefault = defaultValue
> end if
>end function
>
>function hasAttribute(node, attributeName)
> dim attribute: hasAttribute = false
> for each attribute in node.attributes
> if attributeName = attribute.baseName then
> hasAttribute = true
> exit function
> end if
> next
>end function
>
>function AttributeValue(node, attributeName)
> AttributeValue = node.attributes.getNamedItem
(attributeName).text
>end function
>
>sub die_if(expression, errorText, exitcode)
> if expression then
> WScript.StdErr.WriteLine errorText
> WScript.Quit exitCode
> end if
>end sub
>
>
>===================================================
>
>
><?xml version="1.0"?>
><Environment prefix="OS"
wmiclass="Win32_OperatingSystem">
> <property name="BootDevice"/>
> <property name="Buildnumber"/>
> <property name="BuildType"/>
> <property name="CodeSet"/>
> <property name="CSDVersion"/>
> <property name="CurrentTimeZone"/>
> <property name="Debug"/>
> <property name="Distributed"/>
> <property name="EncryptionLevel"/>
> <property name="ForegroundApplicationBoost"/>
> <property name="InstallDate"/>
> <property name="LastBootUpTime"/>
> <property name="Locale"/>
> <property name="OSLanguage"/>
> <property name="OSType"/>
> <property name="ProductType"/>
> <property name="SerialNumber"/>
> <property name="ServicePackMajorVersion"/>
> <property name="ServicePackMinorVersion"/>
> <property name="SuiteMask"/>
> <property name="Version"/>
></Environment>
>
>
>
>.
>

 >> Stay informed about: Script and request for comments: Creating shell-level vari.. 
Back to top
Login to vote
Alex K. Angelopoulos [MVP

External


Since: Jun 27, 2004
Posts: 50



(Msg. 3) Posted: Wed Aug 27, 2003 3:23 pm
Post subject: Re: Script and request for comments: Creating shell-level variables from WMI data [Login to view extended thread Info.]
Archived from groups: microsoft>public>scripting>wsh, others (more info?)

jesseh wrote:
> Just throwing this out here...

Actually, you just threw out my tool - and I'm happy about it. Wink

It still is useful for some things, but ---

> I think WMIC has some kind of conversion process that is
> used to create xml files... am I thinking of the same
> thing you are or is this something different?

What I was working with was a generic underlying process for batch
processing some sources of information and instantiating them as WMI
variables. That said, this provides a very simple way to do all of this.
For example, suppose we want to create PROCESS namespace variables from
values in the class Win32_OperatingSystem, with prefix OS. We could use a
batch file wmi2env, with the classname and prefix as arguments:

wmi2env Win32_OperatingSystem OS

:: wmi2env.cmd
FOR /F %%i IN ('WMIC PATH %1 GET /VALUE') DO set %2.%%i

Null variables would simply disappear immediately...
 >> Stay informed about: Script and request for comments: Creating shell-level vari.. 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
AD script...... - I need a Asp script to log on to AD and list users in an OU and to reset there own password, change phone and adress.... If any have or know who to make one i would appreciate it

logon script - Hope someone can help me.... I'm looking to have a logon script in VB. I have no experience with this, so any help is greatly appreciated. I just need to map drives, printers that are hangin off WIN2000 workstations, and map the users home share. A....

WMI Remote Script - Dear all, I would like to get help from you to demomstrate how to make a WMI script running in same domain for inventory collection. Regards and Thanks.

WMI Script Modification - Dear all, I would like to modify the below script to be more details with software version. Ex. SoftwareTitle: Office XP, 1.0 Please help. Thanks and regards. Sub FindSoftware(sLine) Dim oFSO, WshShell, WshNetwork Set oFSO =..

script to unlock all user accounts in ad - hi all - does anyone have a script to share which would unlock all the user accounts in an active directory domain? i have found several for unlocking a specific user, but cannot see how to modify them to work en masse. tia! shad
   Windows Server (Home) -> Windows Server Scripting All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]