How to Create a Configuration Manager Object by Using WMI

You create a Configuration Manager object, in Configuration Manager, by calling the SWbemObject object SpawnInstance_ method.

The SWbemObject is the class definition for the object type that you want to create. For example, SMS_Package. You get the SWbemObject by calling the SWBemServices object Get method.

To create a Configuration Manager object

  1. Set up a connection to the SMS Provider. For more information, see How to Connect to an SMS Provider in Configuration Manager by Using WMI.

  2. Using the SWBemServices object you obtain from step one, call Get to get the SWbemObject for the Configuration Manager object class definition.

  3. Call SpawnInstance_ on the SWbemObject to create the new object. An SWbemObject is returned for the new object.

  4. Using the SWbemObject returned from the call to SpawnInstance, populate the object properties.

  5. Call Put_ to commit the new object to the SMS Provider.

Example

The following VBScript code example creates an SMS_Package object.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

Sub CreatePackage (connection)  

    On Error Resume Next  

    ' Create a package object.  
    Set package = connection.Get("SMS_Package").SpawnInstance_()  

    If Err.Number<>0 Then  
        Wscript.Echo "Couldn't create packages object"  
        Exit Sub  
    End If  

    ' Populate the object.  
    package.Name = "Test Package"  
    package.Description = "A test package"  
    package.PkgSourceFlag = 2  
    package.PkgSourcePath = "C:\temp"  

    package.Put_  

    If Err.Number<>0 Then  
        Wscript.Echo "Couldn't commit the package"  
        Exit Sub  
    End If  

    WScript.Echo "Package created"  
End Sub  

This example method has the following parameters:

Parameter Type Description
Connection SWBemServices A valid connection to the SMS Provider.

Compiling the Code

See Also

Windows Management Instrumentation
Objects overview How to Call a Configuration Manager Object Class Method by Using WMI
How to Connect to an SMS Provider in Configuration Manager by Using WMI
How to Delete a Configuration Manager Object by Using WMI
How to Modify a Configuration Manager Object by Using WMI
How to Perform an Asynchronous Configuration Manager Query by Using WMI
How to Perform a Synchronous Configuration Manager Query by Using WMI
How to Read a Configuration Manager Object by Using WMI
How to Read Lazy Properties by Using WMI