Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

831 lines
23 KiB

<!--
******************************************************************
'
' container.wsf
'
' Purpose: test SWbemServicesEx and SWbemObjectEx containers
'
' Parameters: none
'
' Returns: 0 - success
' 1 - failure
'
'*****************************************************************
-->
<job id="WMI Container Test">
<reference object="WbemScripting.SWbemLocator" version="1.2"/>
<script language="VBScript">
on error resume next
set scriptHelper = CreateObject("WMIScriptHelper.WSC")
scriptHelper.logFile = "c:\temp\container.txt"
scriptHelper.loggingLevel = 3
scriptHelper.testName = "WMI Container"
scriptHelper.testStart
dim l
dim ns
TestPreamble
TestLocatorOpen
TestServicesOpen
TestScopeOnObject
TestCollection
TestCollectionOnObject
'TestPostamble
scriptHelper.testComplete
if scriptHelper.statusOK then
WScript.Echo "PASS"
WScript.Quit 0
else
WScript.Echo "FAIL"
WScript.Quit 1
end if
'******************************************************************
'
' TestPreamble
'
' Purpose: Create some objects for this test
'
'*****************************************************************
Sub TestPreamble
on error resume next
' Create a locator
set l = CreateObject("WbemScripting.SWbemLocatorEx")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to create locator"
else
scriptHelper.writeToLog "Successful creation of locator", 2
end if
' Connect to the namespace
set ns = l.ConnectServer (,"root\default")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to connect to root\default"
else
scriptHelper.writeToLog "Successful connection to root\default", 2
end if
'Make a new class
set newClass = ns.Get
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get empty class"
else
scriptHelper.writeToLog "Successful retrieval of empty class", 2
end if
newClass.Path_.Class = "CONTAINER000TEST"
' Add a key property (uint32)
set p0 = newClass.Properties_.Add ("p0", wbemCimtypeUint32)
p0.Qualifiers_.Add "key", true
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to define class"
else
scriptHelper.writeToLog "Successful definition of class", 2
end if
' Save the class
newClass.Put_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to save class"
else
scriptHelper.writeToLog "Successful save of class", 2
end if
' Get it back and make an instance
set newInstance = ns.Get ("CONTAINER000TEST").SpawnInstance_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get class"
else
scriptHelper.writeToLog "Successful get of class", 2
end if
' Create an instance
newInstance.p0 = 1
newInstance.Put_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to create instance"
else
scriptHelper.writeToLog "Successful creation of instance", 2
end if
'Make another new class
newClass.Path_.Class = "CONTAINER001TEST"
newClass.Put_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to define class"
else
scriptHelper.writeToLog "Successful definition of class", 2
end if
'Make an association class
set newClass = ns.Get
newClass.Path_.Class = "CONTAINER000TESTASSOC"
newClass.Qualifiers_.Add "association", true
set propList = newClass.Properties_
set p1 = propList.Add ("p000", wbemCimtypeReference)
p1.Qualifiers_.Add "key", true
p1.Qualifiers_.Add "cimtype", "ref:CONTAINER000TEST"
set p2 = propList.Add ("p001", wbemCimtypeReference)
p2.Qualifiers_.Add "key", true
p2.Qualifiers_.Add "cimtype", "ref:CONTAINER001TEST"
newClass.Put_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to define assoc class"
else
scriptHelper.writeToLog "Successful definition of assoc class", 2
end if
End Sub
'******************************************************************
'
' TestPostamble
'
' Purpose: Create some objects for this test
'
'*****************************************************************
Sub TestPostamble
on error resume next
' Clean up
ns.Delete ("CONTAINER000TEST")
ns.Delete ("CONTAINER001TEST")
ns.Delete ("CONTAINER000TESTASSOC")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to delete class"
else
scriptHelper.writeToLog "Successful delete of class", 2
end if
End Sub
'******************************************************************
'
' TestLocatorOpen
'
' Purpose: test SWbemLocatorEx scope functionality
'
'*****************************************************************
Sub TestLocatorOpen
on error resume next
scriptHelper.writeToLog ">>>", 2
scriptHelper.writeToLog ">>> TestLocatorOpen", 2
scriptHelper.writeToLog ">>>", 2
' Open the instance as a scope
set scope = l.Open ("root\default:CONTAINER000TEST=1", ,,,,, wbemTypeServices)
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to open instance as scope"
else
scriptHelper.writeToLog "Successful opening of instance as scope", 2
TestScope scope
end if
scriptHelper.writeToLog "<<<", 2
scriptHelper.writeToLog "<<< TestLocatorOpen", 2
scriptHelper.writeToLog "<<<", 2
End Sub
'******************************************************************
'
' TestScope
'
' Purpose: test SWbemServicesEx scope functionality
'
'*****************************************************************
Sub TestScope (scope)
on error resume next
scriptHelper.writeToLog ">>>", 2
scriptHelper.writeToLog ">>> TestScope", 2
scriptHelper.writeToLog ">>>", 2
' Add an object to the scope (note class retrieval is from scope!)
set newInstance = scope.Get ("CONTAINER000TEST").SpawnInstance_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get class from scope"
else
scriptHelper.writeToLog "Successful retrieval of class from scope", 2
end if
newInstance.p0 = 2
scope.Put (newInstance)
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to save instance in scope"
else
scriptHelper.writeToLog "Successful saving of instance in scope", 2
end if
' Check we can access the scoped element from the parent namespace
set newInstance = ns.Get("CONTAINER000TEST=1:CONTAINER000TEST=2")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get instance from parent namespace"
else
scriptHelper.writeToLog "Successful retrieval of instance from parent namespace", 2
end if
' Check we can access the scoped element from this scope
set newInstance = scope.Get("CONTAINER000TEST=2")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get instance from this scope"
else
scriptHelper.writeToLog "Successful retrieval of instance from this scope", 2
end if
' Enumerate the members of the scope
set objColl = scope.InstancesOf ("CONTAINER000TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate instances in scope"
else
scriptHelper.writeToLog "Successful enumeration of instances in scope", 2
end if
if 1 = objColl.Count then
' Check it is the right object
for each obj in objColl
if 2 = obj.p0 then
scriptHelper.writeToLog "Correct instance in scope enumeration", 2
else
scriptHelper.writeErrorToLog null, "Incorrect p0 [2]: " & obj.p0
end if
exit for
next
else
scriptHelper.writeErrorToLog null, "Incorrect count [1]: " & objColl.Count
end if
' Enumerate the members of the scope using the intrinsic collection
firstElement = true
ok = true
for each obj2 in scope
if err <> 0 then
scriptHelper.writeErrorToLog err, "Error enumerating intrinsic collection on scope"
ok = false
exit for
elseif 2 <> obj2.p0 then
scriptHelper.writeErrorToLog null, "Incorrect p0 [2]: " & obj2.p0
ok = false
elseif Not(firstElement) then
scriptHelper.writeErrorToLog null, "Incorrect count [0]: "
ok = false
else
scriptHelper.writeToLog "Default enumeration of scope: " & obj2.Path_.Path, 2
firstElement = false
end if
next
if ok then scriptHelper.writeToLog "Successful instance enumeration using scope default collection", 2
' Set up a filter and try it again
scope.Filter_ = Array ("CONTAINER000TEST", "CONTAINER001TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to set filter on scope"
else
scriptHelper.writeToLog "Successful set of filter on scope", 2
end if
firstElement = true
ok = true
for each obj in scope
if err <> 0 then
scriptHelper.writeErrorToLog err, "Error enumerating intrinsic filtered collection on scope"
ok = false
exit for
elseif 2 <> obj.p0 then
scriptHelper.writeErrorToLog null, "Incorrect p0 [2]: " & obj.p0
ok = false
elseif Not(firstElement) then
scriptHelper.writeErrorToLog null, "Incorrect count [0]: "
ok = false
else
scriptHelper.writeToLog "Default filtered enumeration: " & obj.Path_.Path, 2
firstElement = false
end if
next
if ok then scriptHelper.writeToLog "Successful instance enumeration using scope default collection", 2
' Now set the filter to match 0 elements in the scope
scope.Filter_ = Array ("CONTAINER001TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to set filter on scope"
else
scriptHelper.writeToLog "Successful set of filter on scope", 2
end if
ok = true
for each obj in scope
scriptHelper.writeErrorToLog null, "Incorrect count [0]: "
ok = false
exit for
next
if ok then scriptHelper.writeToLog "Successful instance enumeration using scope default collection", 2
' Delete the item from the scope
scope.Delete "CONTAINER000TEST=2"
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to delete instances in scope"
else
scriptHelper.writeToLog "Successful delete of instances in scope", 2
end if
' Check it's gone
set objColl = scope.InstancesOf ("CONTAINER000TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate instances in scope"
else
scriptHelper.writeToLog "Successful enumeration of instances in scope", 2
end if
if 0 = objColl.Count then
scriptHelper.writeToLog "Correct instance count in scope enumeration [0]", 2
else
scriptHelper.writeErrorToLog null, "Incorrect count [0]: " & objColl.Count
end if
scriptHelper.writeToLog "<<<", 2
scriptHelper.writeToLog "<<< TestScope", 2
scriptHelper.writeToLog "<<<", 2
End Sub
'******************************************************************
'
' TestServicesOpen
'
' Purpose: test SWbemServicesEx scope functionality
'
'*****************************************************************
Sub TestServicesOpen
on error resume next
scriptHelper.writeToLog ">>>", 2
scriptHelper.writeToLog ">>> TestServicesOpen", 2
scriptHelper.writeToLog ">>>", 2
' Open the instance as a scope
set scope = ns.Open ("CONTAINER000TEST=1", wbemConnectionFlagOpenScope _
OR wbemConnectionFlagAllowNamespaceTraversal OR wbemConnectionFlagAllowMachineTraversal)
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to open instance as scope"
else
scriptHelper.writeToLog "Successful opening of instance as scope", 2
TestScope scope
end if
scriptHelper.writeToLog "<<<", 2
scriptHelper.writeToLog "<<< TestServicesOpen", 2
scriptHelper.writeToLog "<<<", 2
End Sub
'******************************************************************
'
' TestScopeOnObject
'
' Purpose: test SWbemObjectEx scope functionality
'
'*****************************************************************
Sub TestScopeOnObject
scriptHelper.writeToLog ">>>", 2
scriptHelper.writeToLog ">>> TestScopeOnObject", 2
scriptHelper.writeToLog ">>>", 2
on error resume next
' Open the instance as an object
set instance = ns.Get ("CONTAINER000TEST=1")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to open instance as object"
else
scriptHelper.writeToLog "Successful opening of instance as object", 2
end if
' Add an object to the scope (note class retrieval is from scope!)
set newInstance = instance.CreateInstance_ ("CONTAINER001TEST=1")
newInstance.Put_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to save instance in object"
else
scriptHelper.writeToLog "Successful saving of instance in object", 2
end if
' Check we can access the scoped element from the parent namespace
set newInstance = ns.Get("CONTAINER000TEST=1:CONTAINER001TEST=1")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get instance from parent namespace"
else
scriptHelper.writeToLog "Successful retrieval of instance from parent namespace", 2
end if
' Check we can access the scoped element from this object
set newInstance = instance.GetInstance_ ("CONTAINER001TEST=1")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get instance from this object"
else
scriptHelper.writeToLog "Successful retrieval of instance from this object", 2
end if
' Enumerate the members of the scope
set objColl = instance.ExecQuery ("select * from CONTAINER001TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate instances in object"
else
scriptHelper.writeToLog "Successful enumeration of instances in object", 2
end if
if 1 = objColl.Count then
' Check it is the right object
for each obj in objColl
if 1 = obj.p0 then
scriptHelper.writeToLog "Correct instance in scope enumeration", 2
else
scriptHelper.writeErrorToLog null, "Incorrect p0 [1]: " & obj.p0
end if
exit for
next
else
scriptHelper.writeErrorToLog null, "Incorrect count [1]: " & objColl.Count
end if
' Enumerate the members of the scope using the intrinsic collection
firstElement = true
ok = true
for each obj in instance
if 1 <> obj.p0 then
scriptHelper.writeErrorToLog null, "Incorrect p0 [1]: " & obj.p0
ok = false
elseif Not(firstElement) then
scriptHelper.writeErrorToLog null, "Incorrect count [1]: "
ok = false
else
scriptHelper.writeToLog "Default enumeration: " & obj.Path_.Path, 2
firstElement = false
end if
next
if ok then scriptHelper.writeToLog "Successful instance enumeration using object default collection", 2
' Set up a filter and try it again
instance.Filter_ = Array ("CONTAINER000TEST", "CONTAINER001TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to set filter on object"
else
scriptHelper.writeToLog "Successful set of filter on object", 2
end if
firstElement = true
ok = true
for each obj in instance
if 1 <> obj.p0 then
scriptHelper.writeErrorToLog null, "Incorrect p0 [1]: " & obj.p0
ok = false
elseif Not(firstElement) then
scriptHelper.writeErrorToLog null, "Incorrect count [1]: "
ok = false
else
scriptHelper.writeToLog "Default enumeration: " & obj.Path_.Path, 2
firstElement = false
end if
next
if ok then scriptHelper.writeToLog "Successful instance enumeration using object default collection", 2
' Now set the filter to match 0 elements in the scope
instance.Filter_ = Array ("CONTAINER000TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to set filter on object"
else
scriptHelper.writeToLog "Successful set of filter on object", 2
end if
ok = true
for each obj in instance
scriptHelper.writeErrorToLog null, "Incorrect count [0]: "
ok = false
exit for
next
if ok then scriptHelper.writeToLog "Successful instance enumeration using object default collection", 2
' Delete the item from the scope
instance.DeleteInstance_ "CONTAINER001TEST=1"
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to delete instances in object"
else
scriptHelper.writeToLog "Successful delete of instances in object", 2
end if
' Check it's gone
set objColl = instance.ExecQuery_ ("select * from CONTAINER001TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate instances in object"
else
scriptHelper.writeToLog "Successful enumeration of instances in object", 2
end if
if 0 = objColl.Count then
scriptHelper.writeToLog "Correct instance count in object instance enumeration [0]", 2
else
scriptHelper.writeErrorToLog null, "Incorrect count [0]: " & objColl.Count
end if
scriptHelper.writeToLog "<<<", 2
scriptHelper.writeToLog "<<< TestScopeOnObject", 2
scriptHelper.writeToLog "<<<", 2
End Sub
'******************************************************************
'
' TestCollection
'
' Purpose: test SWbemServicesEx collection functionality
'
'*****************************************************************
Sub TestCollection
on error resume next
scriptHelper.writeToLog ">>>", 2
scriptHelper.writeToLog ">>> TestCollection", 2
scriptHelper.writeToLog ">>>", 2
' Open the instance as a collection based on our assoc class
set collection = ns.Open ("CONTAINER000TEST=1", wbemConnectionFlagOpenCollection, "CONTAINER000TESTASSOC")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to open instance as collection"
else
scriptHelper.writeToLog "Successful opening of instance as collection", 2
end if
' Make a new instance in the same namespace
set newInstance = ns.Get("CONTAINER001TEST").SpawnInstance_
newInstance.p0 = 23
newInstance.Put_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to create new instance in namespace"
else
scriptHelper.writeToLog "Successful creation of instance in namespace", 2
end if
' Add the new instance to this collection
collection.Add "CONTAINER001TEST=23"
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to add instance to collection"
else
scriptHelper.writeToLog "Successful addition of instance to collection", 2
end if
' Do we have a new association instance?
set assocInstance = ns.Get ("CONTAINERTEST000ASSOC.p000=1,p001=23")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get assoc instance from parent namespace"
else
scriptHelper.writeToLog "Successful retrieval of assoc instance from parent namespace", 2
end if
' Check we can access the new instance as a member of this collection
set newInstance = collection.Get("CONTAINER001TEST=23")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get instance from this collection"
else
scriptHelper.writeToLog "Successful retrieval of instance from this collection", 2
end if
' Enumerate the members of the collection
set objColl = collection.InstancesOf ("CONTAINER001TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate instances in collection"
else
scriptHelper.writeToLog "Successful enumeration of instances in collection", 2
end if
if 1 = objColl.Count then
' Check it is the right object
for each obj in objColl
if 23 = obj.p0 then
scriptHelper.writeToLog "Correct instance in collection enumeration", 2
else
scriptHelper.writeErrorToLog null, "Incorrect p0 [23]: " & obj.p0
end if
exit for
next
else
scriptHelper.writeErrorToLog null, "Incorrect count [1]: " & objColl.Count
end if
' Remove the item from the container
container.Remove "CONTAINER001TEST=23"
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to remove instance from container"
else
scriptHelper.writeToLog "Successful removal of instance from container", 2
end if
' Check it's gone
set objColl = container.InstancesOf ("CONTAINER001TEST")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate instances in collection"
else
scriptHelper.writeToLog "Successful enumeration of instances in collection", 2
end if
if 0 = objColl.Count then
scriptHelper.writeToLog "Correct instance count in collection enumeration [0]", 2
else
scriptHelper.writeErrorToLog null, "Incorrect count [0]: " & objColl.Count
end if
scriptHelper.writeToLog "<<<", 2
scriptHelper.writeToLog "<<< TestCollection", 2
scriptHelper.writeToLog "<<<", 2
End Sub
'******************************************************************
'
' TestCollectionOnObject
'
' Purpose: test SWbemObjectEx collection functionality
'
'*****************************************************************
Sub TestCollectionOnObject
on error resume next
scriptHelper.writeToLog ">>>", 2
scriptHelper.writeToLog ">>> TestCollectionOnObject", 2
scriptHelper.writeToLog ">>>", 2
' Open the instance as an object
set instance = ns.Get ("CONTAINER000TEST=1")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to open instance"
else
scriptHelper.writeToLog "Successful opening of instance", 2
end if
' Make a new instance in the same namespace
set newInstance = ns.Get("CONTAINER001TEST").SpawnInstance_
newInstance.p0 = 23
newInstance.Put_
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to create new instance in namespace"
else
scriptHelper.writeToLog "Successful creation of instance in namespace", 2
end if
' Add the new instance to this collection
instance.Add_ "CONTAINER001TEST=23", "CONTAINER000TESTASSOC"
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to add instance to object collection"
else
scriptHelper.writeToLog "Successful addition of instance to object collection", 2
end if
' Do we have a new association instance?
set assocInstance = ns.Get ("CONTAINERTEST000ASSOC.p000=1,p001=23")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get assoc instance from parent namespace"
else
scriptHelper.writeToLog "Successful retrieval of assoc instance from parent namespace", 2
end if
' Check we can access the new instance as a member of this collection
set newInstance = instance.GetMember_("CONTAINER001TEST=23", "CONTAINER000TESTASSOC")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to get member from this object collection"
else
scriptHelper.writeToLog "Successful retrieval of member from this object collection", 2
end if
' Enumerate the members of the collection
set objColl = instance.Members_ ("CONTAINER000TESTASSOC")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate members in object collection"
else
scriptHelper.writeToLog "Successful enumeration of members in object collection", 2
end if
if 1 = objColl.Count then
' Check it is the right object
for each obj in objColl
if 23 = obj.p0 then
scriptHelper.writeToLog "Correct instance in member enumeration", 2
else
scriptHelper.writeErrorToLog null, "Incorrect p0 [23]: " & obj.p0
end if
exit for
next
else
scriptHelper.writeErrorToLog null, "Incorrect count [1]: " & objColl.Count
end if
' Remove the item from the collection
instance.Remove "CONTAINER001TEST=23", "CONTAINER000TESTASSOC"
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to remove member from object collection"
else
scriptHelper.writeToLog "Successful removal of member from object collection", 2
end if
' Check it's gone
set objColl = instance.Members_ ("CONTAINER000TESTASSOC")
if err <> 0 then
scriptHelper.writeErrorToLog err, "Failed to enumerate members in object collection"
else
scriptHelper.writeToLog "Successful enumeration of members in object collection", 2
end if
if 0 = objColl.Count then
scriptHelper.writeToLog "Correct member count in object collection [0]", 2
else
scriptHelper.writeErrorToLog null, "Incorrect member count [0]: " & objColl.Count
end if
scriptHelper.writeToLog "<<<", 2
scriptHelper.writeToLog "<<< TestCollectionOnObject", 2
scriptHelper.writeToLog "<<<", 2
End Sub
</script>
</job>