Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[WMIClass] versus get-wmiobject

24 views
Skip to first unread message

Hal Rottenberg

unread,
Jul 2, 2007, 2:05:36 PM7/2/07
to
Can someone explain why these two statements produce different results
and what it means?

1# $a = get-wmiobject win32_process

TypeName: System.Management.ManagementObject#root\cimv2\Win32_Process

2# $b = [wmiclass]"\\localhost\root\cimv2:win32_process"

TypeName: System.Management.ManagementClass#ROOT\cimv2\Win32_Process

They are quite different in that the latter ($b) has a Create() method
which is pretty darn cool.

dreeschkind

unread,
Jul 2, 2007, 3:12:00 PM7/2/07
to
Well, I can't explain all the details, but the first statement will get you a
single wmiobject instance for every process that is running
(ManagementObject) and the second statement will get you only one instance of
the corresponding wmiclass (ManagementClass).

>$a.gettype()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array

>$a[0].gettype()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ManagementObject
System.Management.ManagementBaseObject

>$b.gettype()

IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True ManagementClass
System.Management.ManagementObject

--
greetings
dreeschkind

/\/\o\/\/ [MVP]

unread,
Jul 2, 2007, 3:21:20 PM7/2/07
to
in one case you get a WMI class in the other cave the WMI instances

[wmi]win32_process
[wmiclass]win32_process

for more WMI info and examples :
http://thepowershellguy.com/blogs/posh/archive/tags/WMI/default.aspx

Greetings /\/\o\/\/
http://thePowerShellGuy.com

"Hal Rottenberg" <halr...@gmail.com> wrote in message
news:1183399536.3...@q75g2000hsh.googlegroups.com...

Hal Rottenberg

unread,
Jul 2, 2007, 3:26:14 PM7/2/07
to
On Jul 2, 3:12 pm, dreeschkind <dreeschk...@discussions.microsoft.com>
wrote:

> Well, I can't explain all the details, but the first statement will get you a
> single wmiobject instance for every process that is running
> (ManagementObject) and the second statement will get you only one instance of
> the corresponding wmiclass (ManagementClass).

I was hoping $a[0].Create() would be there but it isn't. I'd still
rather use get-wmiobject if I can just for simplicity's sake but if I
have to do the other way I'd like to know why that is so that I can
explain it to others.

thx

Hal Rottenberg

unread,
Jul 2, 2007, 3:40:38 PM7/2/07
to
On Jul 2, 3:21 pm, "/\\/\\o\\/\\/ [MVP]" <mow...@hotmail.NoSpam>
wrote:

> in one case you get a WMI class in the other cave the WMI instances
>
> [wmi]win32_process
> [wmiclass]win32_process
>
> for more WMI info and examples :http://thepowershellguy.com/blogs/posh/archive/tags/WMI/default.aspx

Thanks /\\/\\o\\/\\/!

dreeschkind

unread,
Jul 2, 2007, 4:46:00 PM7/2/07
to
Is there something wrong with my system?

PS> [wmi]win32_process
Unexpected token 'win32_process' in expression or statement.
At line:1 char:18
+ [wmi]win32_process <<<<

PS> [wmi]'win32_process'
Cannot convert value "win32_process" to type
"System.Management.ManagementObject". Error: "Das angegebene Argument liegt
außerhalb des gültigen Werte
bereichs.
Parametername: path"
At line:1 char:6
+ [wmi]' <<<< win32_process'

--
greetings
dreeschkind

Flowering Weeds

unread,
Jul 2, 2007, 5:33:31 PM7/2/07
to

"dreeschkind"

> Is there something wrong with my system?
>
> PS> [wmi]win32_process
> Unexpected token 'win32_process' in expression or statement.
> At line:1 char:18
> + [wmi]win32_process <<<<
>
> PS> [wmi]'win32_process'
> Cannot convert value "win32_process" to type
> "System.Management.ManagementObject". Error: "Das angegebene
> Argument liegt
> außerhalb des gültigen Werte
> bereichs.
> Parametername: path"
> At line:1 char:6
> + [wmi]' <<<< win32_process'
>

PS> get-process notepad

Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName
------- ------ ----- ----- ----- ------ -- -----------
42 3 1228 3696 32 0.06 2016 notepad

PS> [wmi]"win32_process.handle='2016'" | select name, handle, __Path |
format-list

name : notepad.exe
handle : 2016
__PATH : \\ServerName\root\cimv2:Win32_Process.Handle="2016"

PS>

See how it works now?

dreeschkind

unread,
Jul 3, 2007, 8:52:05 AM7/3/07
to
"Flowering Weeds" wrote:

> See how it works now?
>

Yep, thanks for your example.


--
greetings
dreeschkind

Hal Rottenberg

unread,
Jul 3, 2007, 9:37:54 AM7/3/07
to
On Jul 2, 3:21 pm, "/\\/\\o\\/\\/ [MVP]" <mow...@hotmail.NoSpam>
wrote:

> in one case you get a WMI class in the other cave the WMI instances
>
> [wmi]win32_process
> [wmiclass]win32_process

# help get-wmiobject

NAME
Get-WmiObject

SYNOPSIS
Gets instances of WMI classes or information about available
classes.
---

Isn't that backwards of what you said? I'm afraid I still don't get
it. I was looking on your site for specific example of using
[wmiclass] and didn't find anything that jumped out at me but I
might've missed the good ones.

Is there a way to use the win32_process.Create() method with get-
wmiobject, or is that just not gonna happen?

Jason

unread,
Jul 3, 2007, 10:29:01 AM7/3/07
to
> Is there a way to use the win32_process.Create() method with get-
> wmiobject, or is that just not gonna happen?


Hi Hal:

Here's a function to do it which uses get-wmiobject and the -query
parameter:


######################################

function Create-ProcessWithWMI ($computer = ".", $commandline = $(throw
"Enter the command to execute.") )
{
$ProcessClass = get-wmiobject -query "SELECT * FROM Meta_Class WHERE
__Class = 'Win32_Process'" -namespace "root\cimv2" -computername $computer
$results = $ProcessClass.Create( $commandline )

if ($results.ReturnValue -eq 0) { $results.ProcessID } # Or just return
$true if you don't want the PID.
else { $false ; throw "Failed to create process!" }
}

######################################

Cheers,
Jason


------------------------------------------------------
PowerShell Training at SANS Conferences
http://www.WindowsPowerShellTraining.com
------------------------------------------------------


0 new messages