mardi 8 avril 2008

This is really fun to play with, and it gives inspiration on how to bind Silverlight to our day to day work with Infrastructure developpement ! :)

To illustrate this, I've inserted below a Silverlight Control with my personnal bookmarks about PowerShell. Blogger main frame isn't really wide, so please use the "Full screen" button to have a better look at this control. Notes are in French, I'll translate them soon. Links are all operational.

This sample is about "All Systems" Collection, however this collection isn't really usefull because, as we saw in the previous part of this tutorial, when you add a computer in SCCM, it is added by default in this collection. But this example shows us usefull elements: we know that we need to create an "SMS_CollectionRuleDirect", fills some parameters, then invoke the Method "AddMemberShipRule" with this object as a parameter.

Work done : our computer is now in the "PowerShell Collection" collection.

With some R&D, and with a good understanding of these different examples, you are now able to create many kinds of PowerShell scripts with SCCM. Note that nearly all this samples can be used with SMS too !

Hope this helps you SCCM people, have a Ctrl+C below for a complete PowerShell sample script :

The "ROOT\SMS" namespace is available, under it you'll see "ROOT\SMS\inv_schema" that we bypass for now, and another namespace called "ROOT\SMS\Site_NameOfSite"

DoubleClick on a Site namespace to list the classes

570 (!) classes will appear in the bottom left listbox. Yes, that's quite a lot, but take it cool because we will filter our search pretty easely as our needs arise.

For our first example, we will build a script that add a computer in a collection. Our first step is to look at the SDK chm file to find the proper class to do this task.

Here we see on the right side that, in order to add a computer, we should use the ImportMachineEntry classe of "SMS_Site" namespace. The SDK usually shows VB/C#/VBScript samples. The PowerShell syntax differs from this langage, but this doesn't really matters for our PowerShelled mind : we use the SDK only to know wich Methods/properties we need yo use.

In the VBscript sample of this SDK tip, we see that we need to use a "Connection.get" function to instance the class, then a SpawnInstance of the method, to finally add the required parameters (Mac Address, NetBiosName...). At last, we use ExecMethod to launch the process.

Double Click on the SMS_Site Class to list all properties and methods of the class. You'll see on the right panel that there's a Method called "ImportMachineEntry"

Now Double Click on this Method. Mow's WmiExplorer is a smart tool : it buil dynamically PowerShell sample to use the method as shown below :

Last but not least, it enumerate the different parameters of the script and the variable type (String, Boolean...)... Our remaining job is to populate these variables with appropriate values :

MacAddress (MAC Address of the target computer)

NetbiosName

...

Let's get more in details in this script to understand the invocation process in PowerShell :

When we create a connection to the SMS_Site class and we display as shown above the $mc object, we only got "SMS_Site" in the console

If we use get-membere, we see that there isn't any "ImportMachineEntry" method here. This is due to the specific object output formatting of PowerShell. In order to access properties and methods of this class, we need to use the PSBASE method to have a "RAW" access on this object (more information on PSBASE here ) :

As we can see, $mc.psbase brings Methods and Properties. Now list methods :

We got here our "ImportMachineEntry" method. In the VBSCript sample in the SDK, they told us to use SpawnInstance to define parameters for this method :

As you can see, the code is rather simple : this example just do the same thing.

Have a more indepth look at the $inParams members :

We see here our 4 properties : MacAddress, NetbiosName, OverwriteExistingRecord and SMBIOSGUID, all empty. Notice that each properties Types are exposed in the definition column, and that they are all Read/Write (get;Set)

Great ! know let's write values for 3 of our properties (we don't need MSBIOSGUID if MacAddress is filled... XP is the key to discover this kind of funny details :) )

Sorting the Output of $Inparams, we see that our object's properties aren't empty anymore.

Now we need to execute our Method with the parameters. The VBScript sample said :