#161756 - 2006-05-08 05:33 PM
Specific WMI Targets
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Does anyone know a way to get a specific item of a specific topic of the WMI Classes ?
For instance getting the UserName information from Win32_ComputerSystem. The only way I know how to get that info is Code:
$strComputer = "." $objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2") $colItems = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) For each $objItem in $colItems ? "UserName: " + $objItem.UserName Next
Isn't it possible to do something like Code:
$strComputer = "." $objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2") $Uname = $objWMIService.Get("Win32_ComputerSystem.UserName",0,"") ? $Uname
Any help would be apreciated.
|
Top
|
|
|
|
#161757 - 2006-05-08 06:26 PM
Re: Specific WMI Targets
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
Funny you asked that. Was searching the net on this and here is another guy asking the same question ...
http://www.developersdex.com/asp/message.asp?p=1825&r=4313892
You would think that the collection returned from the wmi query would conform to the standards (reading MSDN it should) - the collection should have an Item property and a Count property - and indeed they do. But for some reason, they dont work (at least not in Kixtart) ...
Ideally you should be able to do this:
?"Username=" + $colItems.Item(0).UserName
But that no work - you should be able to do this:
?"Count = " $colItems.Count
that no work. The properties do indeed exist (because if you access some made-up proerty name, you get an unknown name error). The errors being returned from Item and Count are just generic "Unspecified errors".
I tried accessing the Count property from within for FOR/EACH loop (thinking that maybe the enumeration has to start, before these properties are valid) ... that no work either.
So idk - reading that article, even if .Item worked - the Item property takes an argument that identifies which element your looking to pull (by name) - kind of a catch 22 thing.
Bottom line (afaik) ...
1) The FOR/EACH is the best (only) way
2) The WMI designers are idiots (sorry for saying that, but they are).
-Shawn
|
Top
|
|
|
|
#161759 - 2006-05-08 07:11 PM
Re: Specific WMI Targets
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
Thanks Shawn
Now you can whip up a mini-udf and make it NoVarInStrings compliant.
|
Top
|
|
|
|
#161762 - 2006-05-08 07:58 PM
Re: Specific WMI Targets
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Thats funny how that works, basically you'd have to specify your computername in order to get the username. What about if you want to retrieve the computername ?
Thanks for looking into it btw Shawn Also, a bit Off topic but how to list all properties in a COM object for instance WScript.Network ?
|
Top
|
|
|
|
#161764 - 2006-05-08 08:01 PM
Re: Specific WMI Targets
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
hehe
Also, I found a couple of items on g00gle which use objWMIService.Get() but I can't seem to find any material that explains the .get part.
List em as in Script, like kixomatic does with WMI Classes
|
Top
|
|
|
|
#161768 - 2006-05-10 08:38 AM
Re: Specific WMI Targets
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Thx alot for that krabourn The trouble is you still have to specify the computername. So the problem remains when you are looking for the computername itself Thx for your input though
|
Top
|
|
|
|
#161770 - 2006-05-11 03:38 PM
Re: Specific WMI Targets
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Ah ok, Still weird to get to it in one line though but this is basically what I'm looking for thx alot
|
Top
|
|
|
|
#212119 - 2016-11-16 11:10 AM
Re: Specific WMI Targets
[Re: Arend_]
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Takes 10 years to finally find the proper answer Basically the array WMI returns has the following property "ItemIndex" (Also "Count" as pointed out earlier in this thread). So in effect it would be $colItems.ItemIndex(0).Property Example:
$strComputer = "."
? "ComputerName= "+GetObject("winmgmts:\\"+$strComputer+"\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem").ItemIndex(0).Name
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 370 anonymous users online.
|
|
|