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!
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.
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:
get-wmiobject -class win32_share
This will return all the current instances (shares) as showed here under:
The second method is using the CIM cmdlets like with the Get-CimInstance :
get-ciminstance -classname win32_share
Which will return us the exact same information:
And the third and last method is using the Get-WMIClassInstance:
get-wmiclassinstance -classname win32_share
returns exactly the same content.
[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.
$mynewInstance = New-WMIClassInstance -NameSpace RootDistrict -ClassName PowerShellDistrict -instancename DistrictDemo
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.
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:
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).
We can now create the instance using the Set-WMIClassInstance cmdlet as followed:
New-WMIClassInstance -NameSpace RootDistrict -ClassName PowerShellDistrict -InstanceName DistrictDemo -putInstance $MyNewInstance
If we go check in the MOW WMI explorer for instance in the District class
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. 🙂