共用方式為


How to Create a Configuration Manager Object by Using WMI

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

You create a Configuration Manager object, in Microsoft System Center Configuration Manager 2007, 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. A 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

Concepts

Configuration Manager 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
How to Use Configuration Manager Objects with WMI

Other Resources

https://go.microsoft.com/fwlink/?LinkId=43950