This is the 6th post of a series of 6 articles entitled WMI Week“. It tends to covers the automation tasks around WMI mainly using the powershell WMI module.

The first gives a short introduction concerning WMI, and show what tools are usefull when working with WMI. The second post covers how to manage WMI namespaces using windows powershell. The third post highlights how we can, create, retrieve delete, and manage WMI classes.  The fourth post  covers everything you need to know about WMI properties. The fifth post  will highlight everything related to WMI qualifiers, and the last and six’th post (this one) will explain everything you need the know about WMI Instances.

[important]By the way,the powershell WMI module is available here ( and Don’t forget to rate it  by clicking on the stars ;)).[/important]

Ok, let’s Roll!


Howdy everybody!

Today it is sunday, and the sun is not really shining in lovely Strasbourg/France. So what to do when you are finished with all of your  sunday morning duties? Blogging? that seems to be a good idea.

Let’s continue our WMI week and talk about WMI qualifiers.In today’s post, we will tackle the following topics:

  • What is an Instance ?
  • How do we retrieve instances ?
  • How to create / add a new instance?
  • How to delete / remove a instance?

 

What is an instance?

Let’s start from the beggining, and let’s answer this very first question that perhaps not everybody understand fully: What is an Instance ?

The wikipedia definition is the following one:

In class-based programming, objects are created from classes by subroutines called constructors. An object is an instance of a class, and may be called a class instance or class object; instantiation is then also known as construction. Not all classes can be instantiated –abstract classes cannot be instantiated, while classes that can be instantiated are called concrete classes. In prototype-based programming, instantiation is instead done by copying a prototype.

In other words, an instance is actually a copy of the specefic class in memory that has been given specefic data information.

What is that suppose to mean  ?

Exactly, I was going to ask the same question! Let’s make an example and make thinks more concrete.

Remember our Win32_Share class ? Well this is a class, that as is, has properties, and methods. As I discussed during the day 5:  the qualifiers, each time we create a share we have to provide obligatory data information (and this is also true while using a GUI). We will need to give the share a name, make it point to a specefic local path, maybe set a description and validate. That defined structure that will force us to give always the same data would be called the constructor. This info is then used to create an instance of the Win32_share class, which will be our new share, with the different properties such as: path, name, description etc…

 

How do we retrieve WMI instances ?

This is a question that will be easy to answer : All the cmdlets (or most of them) and even the GUI’s actually show you instances. When you browse to your shares section in the computer management mmc, you will see all the Instances that are currently present. In other words all the instances of Win32_share that currently exist on the local machine.

The MOW wmi explorer I presented you earlier this week during “day 1: Introduction” allow’s you to retrieve the instances very easily.

get instances

As you can see, there is a button in the center of the screen in order to retrieve the current instances of the win32_share class. We have then list of the 11 instances currently present on my computer. Those 11 instances are actually my 11 shares that are present on my machine (you can see ADMIN$, C$, E$ etc…)

With PowerShell it is extremly simple, and we have several ways to get them:

With the old and well knowed Get-wmiobject like this:

This will return all the current instances (shares) as showed here under:

Get-wmiobject

The second method is using the CIM cmdlets like with the Get-CimInstance :

Which will return us the exact same information:

get-ciminstance

And the third and last method is using the Get-WMIClassInstance:

returns exactly the same content.

 

get-wmiclassinstance

[notice]I have actually written the Get-WMIClassInstance only to keep a certain logic in the cmdlets available in the WMI module. It was really necessary for me to write it, but it made sense for me to keep a consistency in the cmdlets available in this module.[/notice]

How do we create WMI instances ?

there are several ways that exist in order to create instances. Until the WMI module there was no direct cmdlet that could actually create an instance. We had to go through a manual process.

If you want to create a new instance of the share class, this is how you would process using powershell only.

When we take a look in the $myNewInstance variable, we can see that the all the properties are listed, but their value is for each of them empty.

 

We will not need to provide all of the information, only the key qualifiers. How do we get them again ? exactly! using Get-WMIKeyQualifier as explained in yesterday’s post.

KeyQualifier

The URL property here is the KEY qualifier.  This means that we will not be able to create an instance of this class before that particular property has a value.

Let’s what there is in our previously created instance:

emptyproperties

All of the properties are empty except for the Description one where we had set a default value while working on the properties section during day 2

We can add values to the different properties by using $nameofthevariable dot the name of property (see example below).

SetValues

We can now create the instance using the Set-WMIClassInstance cmdlet as followed:

 

 

Instance created

If we go check in the MOW WMI explorer for instance in the District class

MOW Instance

 

References:

cim link –> http://technet.microsoft.com/en-us/library/jj553783.aspx

cheat sheet http://blogs.msdn.com/b/powershell/archive/2012/10/28/wmi-cheat-sheet-for-ps-users.aspx

 

Ok, that’s all for today folks. I see you guy’s tomorow where we will talk about mof files. 🙂

Cheers!

Stéphane[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

By | 2016-10-20T01:14:42+00:00 September 27th, 2014|PowerShell, WMI, wmi module, WMI Week|4 Comments

About the Author:

Stéphane is a dynamic and passionate Cloud and datacenter Microsoft MVP since. He is the founder of the Basel PowerShell user Group (BPUG), the co-founder of the French Speaking PowerShell UserGroup (FRPSUG), author, blogger, and received the community award "PowerShell Hero" from PowerShell.org. Stéphane has implemented microsoft infrastructure solutions in various countries of Europe and is currently working in Basel / Switzerland. Stéphane help his clients to reduce their global infrastructure costs by implementing Microsft infrastructure solutions by combining great products such as System Center, Windows Server, with heavy automation using Windows PowerShell. Stéphane loves languages, Belgium beer, French cheese and French Wine. If any of these topics are of your interest, don't hesitate to come and say hi.

4 Comments

  1. Ekram August 23, 2016 at 3:46 pm - Reply

    Hi Stephan,

    Nice Article.

    Help Needed….on PowerShell script.

    i am trying to delete all instances which show in WMI query. i tried searching all over but couldn’t find much on this. your help will be really appreciated.


    1. Start an elevated command prompt, and run wbemtest.exe
    2. Click Connect, and specify the path: root\ccm\SoftMgmtAgent and connect
    3. Select Query, and type select * from DownloadContentRequestEx2
    4. Delete each instance that returned by the query.
    5. Select query, and select * from downloadinfoex2
    6. Delete each instance that returned by the query.
    7. Restart SMS Agent Host service

    is there any PS script available which can perform this task.

    Regards,
    Ekram

  2. NEVERAGAIN September 3, 2015 at 1:49 pm - Reply

    that is a great article .. very well explained , thanks!

  3. […] WMI Week – Day 6: Instances […]

Leave a Reply

%d bloggers like this: