Creating Data in Service Manager   Leave a comment

In my last post, we saw how we were able to retrieve data from Service Manager, where we also said farewell to scripting.  In this post, I’ll quickly go through how to create instance data in Service Manager.  It was a short farewell to scripting, because unlike the last post were we needed some C# to do what we wanted, we can create most objects in the Service Manager CMDB directly from script.  In order to create instances in Service Manager, we need to use CreatableEnterpriseManagementObject.  The constructor for this object takes a reference to the EnterpriseManagementGroup and a ManagementPackClass.  After this, it’s simply a matter of assigning values to various properties of the object.  Here’s a script that creates 5 instances of Microsoft.Windows.Computer and sets a number of the property values.

 

$NS   = "Microsoft.EnterpriseManagement"
$EMGT = "${NS}.EnterpriseManagementGroup"
$EMG = new-object $EMGT localhost
$CEMOT = "${NS}.Common.CreatableEnterpriseManagementObject"
$ComputerClass = $EMG.EntityTypes.GetClasses()|?{$_.name -eq "Microsoft.Windows.Computer"}
1..5 | %{
    $NewComputer = new-object $CEMOT $EMG,$ComputerClass
    $PrincipalNameProperty = $NewComputer.GetProperties()|?{$_.name -eq "PrincipalName"}
    $Name = "Computer${_}.woodgrove.com"
    $NewComputer.Item($PrincipalNameProperty).Value = $Name
    $NewComputer.Commit()
}

this should add 5 computers to our system, (computer1 to computer5).

We can check this with our previous cmdlets:

PS> get-smclass microsoft.windows.computer$|get-scsmobject | ft DisplayName

DisplayName
-----------
Computer2.woodgrove.com
Computer5.woodgrove.com
WIN-752HJBSX24M.woodgrove.com
Computer1.woodgrove.com
Computer4.woodgrove.com
Computer3.woodgrove.com

Woo hoo!  There are my new instances! However, it’s not quite as simple as this.  Some properties are required and we have to be sure that we provide values for those properties.  So, how can we find out what they are?  These properties are designated as key properties.  We can determine what these key properties are, by inspecting the properties of the newly created object. 

PS> $NS   = "Microsoft.EnterpriseManagement"
PS> $EMGT = "${NS}.EnterpriseManagementGroup"
PS> $EMG = new-object $EMGT localhost
PS> $CEMOT = "${NS}.Common.CreatableEnterpriseManagementObject"
PS> $ComputerClass = $EMG.EntityTypes.GetClasses()|?{$_.name -eq "Microsoft.Windows.Computer"}
PS> $NewComputer = new-object $CEMOT $EMG,$ComputerClass

PS> $newcomputer.getproperties()|ft key,name,type -au

  Key Name                                Type
  --- ----                                ----
 True PrincipalName                     string
False DNSName                           string
False NetbiosComputerName               string
False NetbiosDomainName                 string
False IPAddress                         string
False NetworkName                       string
False ActiveDirectoryObjectSid          string
False IsVirtualMachine                    bool
False DomainDnsName                     string
False OrganizationalUnit                string
False ForestDnsName                     string
False ActiveDirectorySite               string
False LogicalProcessors                    int
False OffsetInMinuteFromGreenwichTime      int
False LastInventoryDate               datetime
False Owner                             string
False Customer                          string
False Engineer                          string
False Description                       string
False URL                               string
False ServerType                        string
False ObjectStatus                        enum
False AssetStatus                         enum
False Notes                           richtext
False DisplayName                       string

So, I can see that the only the PrincipalName is required to create an instance, and the type of value that I must supply is a string.

In my next post, I’ll look more closely at how to provide values for things other than strings.

Advertisements

Posted August 14, 2009 by jtruher3 in ServiceManager

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: