Export (0) Print
Expand All

Creating Resources

Microsoft® Windows® 2000 Scripting Guide

Some WMI classes (including Win32_Share and Win32_Process) include a Create method. If a class includes the Create method, that method can be used to create such things as a new shared folder or a new process.

Creating a new resource requires you to use the Get method to bind to the actual WMI class (rather than retrieve instances of the class). After you have an object representing the class, use the SpawnInstance_ method to create a new, "blank," instance of the class. Configure properties for the instance, and then call the Create method.

The script template in Listing 6.27 uses the Create method to create a new shared folder.

Listing 6.27 Template for Creating Resources

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16


strComputer = "."
strNamespace = "\root\cimv2"
strClassName = "Win32_Share"

Set objSWbemServices = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & strNamespace)

Set objNewShare = objSWbemServices.Get(strClassName)
Set objInParams = _ objNewShare.Methods_("Create").InParameters.SpawnInstance_()

objInParams.Properties_.Item("Description") = "New Share Description"
objInParams.Properties_.Item("Name") = "New Share Name"
objInParams.Properties_.Item("Path") = "C:\scripts\shared"
objInParams.Properties_.Item("Type") = 0

objNewShare.ExecMethod_ "Create", objInParams

To use this template with other WMI classes:

  1. Set the value of strClassName to the appropriate WMI class.

  2. If necessary, set the value of strNamespace to the appropriate WMI namespace.

  3. Replace the statements within the For Each loop that configure the values for the new share. Remove the following lines, and replace them with the appropriate lines of code for the object being created:

    
    
    objInParams.Properties_.Item("Description") = "New Share Description"
    objInParams.Properties_.Item("Description") = "New Share Description"
    objInParams.Properties_.Item("Name") = "New Share Name"
    objInParams.Properties_.Item("Path") = "C:\scripts\shared"
    objInParams.Properties_.Item("Type") = 0
    
    
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft