WMI Objects

Hi,

Can someone help me to understand what is instance in WMI objects. I can understand what is class; Win32_Bios, Win32_Operatingsystem are examples for class but what is instance

Hi,

I’ll take a stab at it.

Instance (computer science) - Wikipedia basically says an instance is a concrete occurrence of an object. It exists in memory, therefore, it is. Instantiation is the action of constructing the object, and its place in memory.

Possibly, the usage of “instance” is just a way to point to that WMI thing, that already has a place in memory.

If I’m wrong, I’m fine for the correction.

Cheers,

Seth

That is true. The class is a blueprint. An object is an instance of that blueprint.

Do you know c#? It’s kind of the same thing. An instance is created with a constructor and has instance methods. There are also static objects that have static methods, and don’t require a constructor to make them.

Ok. Let me tell you where i’m exactly. I read Don Jones’ Learn windows powershell in a month of Lunches. There is an statement below:

“When you have one or more manageable components, you will have an equal number of instances for that class. An Instance is real-world occurence of something represented by a class. If your computer has a single BIOS (and they all do), you will have 1 instance of win32_Bios in root\cimv2; If your computer has 100 background services installed, you will have 100 instances of win32_service.”

I don’t understand the above four lines. Could someone please explain. Thanks in advance.

The car analogy… The class is the blueprint for a specific model. When the car has been made, it is an object - an instance of the class. If you make 100 cars, you have 100 objects (instances). No matter how many cars you make, you still only have that one blueprint.

If you go through some tutorial of classes in probably any programming language it will be the same idea.

You can use classes in powershell as well since version 5 if I remember correctly.
E.g.

class Car {
    [string]$Brand
    [string]$Model
    [string]$Color
}

As you can see it’s just a number of properties, no value or Car objects have been created.

# this will instantiate an instance of a new Car object using the Car class as blueprint.
$myCar = [Car]::new()

# Set the values of the $myCar object.
$myCar.Brand = "Toyota"
$myCar.Model = "Supra"
$myCar.Color = "Blue"

So for each new car object you want to create you need to call the “new()” method.
E.g. if you want to create 10 different cars and you want all of them to remain as individual objects.
Then you would need to call the new() method 10 times.

If you’re confused about the Bios-thing it’s because a computer only have one BIOS, meaning you won’t see multiple BIOS’s on a computer
If a computer would have more than one BIOS then it would have multiple instances.
But the number running processes using a specific service could be one or many.

If you go through some tutorial of classes in probably any programming language it will be the same idea.

You can use classes in powershell as well since version 5 if I remember correctly.
E.g.

class Car {
    [string]$Brand
    [string]$Model
    [string]$Color
}

As you can see it’s just a number of properties, no value or Car objects have been created.

# this will instantiate an instance of a new Car object using the Car class as blueprint.
$myCar = [Car]::new()

# Set the values of the $myCar object.
$myCar.Brand = "Toyota"
$myCar.Model = "Supra"
$myCar.Color = "Blue"

So for each new car object you want to create you need to call the “new()” method.
E.g. if you want to create 10 different cars and you want all of them to remain as individual objects.
Then you would need to call the new() method 10 times.

If you’re confused about the Bios-thing it’s because a computer only have one BIOS, meaning you won’t see multiple BIOS’s on a computer
If a computer would have more than one BIOS then it would have multiple instances.
Whereas the number processes using a specific service could be many.

I’ll try to give wmi examples.

# get a wmi static class
$printclass = [wmiclass]'win32_printer'

# calling static method to create a printer
$printclass.AddPrinterConnection("\\$server\$printer")


# find the already created wmi instance of a printer 
$printerObject = Get-WmiObject win32_printer | where name -eq \\$server\$defaultprinter

# call the instance method
$printerObject.SetDefaultPrinter()

Notice the naming of the cim cmdlets:

Get-CimInstance  # like Get-WmmiObject
Get-CimClass

EDIT: Is this overkill? I could give similar examples in .net.

[quote quote=127361]Ok. Let me tell you where i’m exactly. I read Don Jones’ Learn windows powershell in a month of Lunches. There is an statement below:

“When you have one or more manageable components, you will have an equal number of instances for that class. An Instance is real-world occurence of something represented by a class. If your computer has a single BIOS (and they all do), you will have 1 instance of win32_Bios in root\cimv2; If your computer has 100 background services installed, you will have 100 instances of win32_service.”

I don’t understand the above four lines. Could someone please explain. Thanks in advance.

[/quote]
I think the key point that Don Jones is making in that statement is the last sentence. An instance is something that sits inside of something else. He is saying that Win32_Bios is an instance of root\cimv2. That would mean that the Bios is a single instance of the root class cimv2. Background services themselves are instances, of win32_service, which is an instance of whichever root class it belongs to. Certain instances can be instances of parent instances, which when dealing with WMI, most instances are part of a ‘class’.

After re-reading my initial comment, I should also add an additional tidbit of information. WMI is a namespace, namespaces contain classes, classes contain instances. Its stuff inside of other stuff.

I understand the instance but now I got a Child-question too. The difference between Object and Class? I gone through online and got idea like below. Just let me know whether i got the point correctly or not??

Class - Blueprint of a car
Object - A real Car
Instance - My Car,Your car (The most specific object is an instance)
Can someone please give a simple one-line command example using Get-wmiobject and differentiate the Object and Class as i'm a beginner to Wmiobject/Ciminstance.

I general and from what I’ve learned/read I would say it this way instead.

An Object is an Instance of a Class.

MyCar is an instance of the blueprint Car.
MyFriendsCar is an instance of the blueprint Car but it’s not the same car as MyCar.

In regard to WmiObject and Ciminstance, as far as I know they are really interchangeable.
WMI is Microsoft’s version of the open standard CIM.
The WmiObject commands have been there more or less from the start.
So if they would have introduced them today maybe the cmdlets would have been called WmiInstance instead.
Maybe I’m wrong but in this case you can probably just ignore the difference in words used for the cmdlets.

E.g.

$a = Get-CimInstance -ClassName Win32_Process
$b = Get-WmiObject -Class Win32_Process

Will both give you and array of objects.
The Wmi objects will even have Cim refrences in the attributes.

<p style=“text-align: left;”>An instance of the class win32_powershell could be MyPowershellBook, or MyPencil. Anything that’s in the room thats being used to learn about the ‘class’ would be an ‘instance’ of the class. The ‘class’ isn’t real, it’s like the “room” that the instances are inside of. When you pipe a ‘class’ to -gm you get all the objects, which are like the students, or members, of the class. Classes are also objects too, so it can definitely get confusing.</p>

So the members of any class or called as Objects/Instances? Right??

Get-WmiObject -class win32_bios | Get-Member

Yes, members are most often called on the object/instance, where they do something on that specific object.

There are members, though, that belong to the class - the static members. They do generic tasks. Feed Get-Member with -static to see them.

$a = "string a"
$b = "string b"

# calling method on object a
$a.CompareTo($b)

# calling static method on class string
# it doesn't know any values, so both a and b must be passed to it
[string]::Compare($a, $b)

In WMI, an instance is an actual manageable component on your system and is represented by a class… e.g. your computer may have a class win32_cdromdrive, and the actual physical CD/DVD ROM drive on that machine is an instance of that class.