|
|
<!--
****************************************************************** ' ' rename.wsf ' ' Purpose: test object renaming ' ' Parameters: none ' ' Returns: 0 - success ' 1 - failure ' '***************************************************************** -->
<job id="WMI Rename Test"> <reference object="WbemScripting.SWbemLocator" version="1.2"/> <script language="VBScript">
on error resume next
set scriptHelper = CreateObject("WMIScriptHelper.WSC") scriptHelper.logFile = "c:\temp\rename.txt" scriptHelper.loggingLevel = 3 scriptHelper.testName = "WMI Rename" scriptHelper.testStart
dim l dim ns
TestPreamble TestRename 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 = "RENAME000TEST"
' 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 ("RENAME000TEST").SpawnInstance_
if err <> 0 then scriptHelper.writeErrorToLog err, "Failed to get new instance" else scriptHelper.writeToLog "Successful get of new instance", 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
End Sub
'****************************************************************** ' ' TestPostamble ' ' Purpose: Create some objects for this test ' '*****************************************************************
Sub TestPostamble
on error resume next
' Clean up ns.Delete ("RENAME000TEST")
if err <> 0 then scriptHelper.writeErrorToLog err, "Failed to delete classes" else scriptHelper.writeToLog "Successful delete of classes", 2 end if
End Sub
'****************************************************************** ' ' TestRename ' ' Purpose: test rename functionality ' '*****************************************************************
Sub TestRename
on error resume next
scriptHelper.writeToLog ">>>", 2 scriptHelper.writeToLog ">>> TestRename", 2 scriptHelper.writeToLog ">>>", 2
' Open the instance to be renamed set oInstance = ns.Get ("RENAME000TEST=1")
if err <> 0 then scriptHelper.writeErrorToLog err, "Failed to open instance" else scriptHelper.writeToLog "Successful opening of instance", 2 end if
'Now rename it oInstance.RenameTo_ ("RENAME000TEST=2")
if err <> 0 then scriptHelper.writeErrorToLog err, "Failed to rename instance" else scriptHelper.writeToLog "Successful rename of instance", 2 end if
' Should have been renamed in place if oInstance.p0 = 2 then scriptHelper.writeToLog "Instance has correct in-place key", 2 else scriptHelper.writeErrorToLog err, "Wrong in-place key [2]; " & oInstance.p0 end if
'Check we can get the new one set inst = ns.Get ("RENAME000TEST=2")
if err <> 0 then scriptHelper.writeErrorToLog err, "Failed to get renamed instance" else scriptHelper.writeToLog "Successful get of renamed instance", 2 end if
'Check we can't get the old one set inst = ns.Get ("RENAME000TEST=1")
if wbemErrNotFound = err.Number then scriptHelper.writeToLog "Successful removal of original instance", 2 err.Clear else scriptHelper.writeErrorToLog err, "Failed to remove original instance" end if
scriptHelper.writeToLog "<<<", 2 scriptHelper.writeToLog "<<< TestRename", 2 scriptHelper.writeToLog "<<<", 2
End Sub
</script> </job>
|