Page 1 of 1 1
Topic Options
#161756 - 2006-05-08 05:33 PM Specific WMI Targets
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
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 Offline
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
#161758 - 2006-05-08 07:08 PM Re: Specific WMI Targets
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Ok, been playing and got this to work:

Have to change your wmi query to this (take off the parms):

$colItems = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

Then do this:

$hostname = "SHAWN"

?"UserName=" $colItems.Item("Win32_ComputerSystem='$HOSTNAME'").UserName

Top
#161759 - 2006-05-08 07:11 PM Re: Specific WMI Targets
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
Thanks Shawn

Now you can whip up a mini-udf and make it NoVarInStrings compliant.

Top
#161760 - 2006-05-08 07:21 PM Re: Specific WMI Targets
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
roflmao - bastard !

seriously, glad we did this exersise because I always wondered if there was a one-shot way of querying these single-item wmi collections.

Just wondering why this doesn't work now:

?"UserName=" + $objWMIService.ExecQuery("Select * from Win32_ComputerSystem").Item("Win32_ComputerSystem='$HOSTNAME'").UserName

-Shawn

Top
#161761 - 2006-05-08 07:26 PM Re: Specific WMI Targets
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Oy yeah, and I take back those disparaging remarks I made about the WMI developers.
Top
#161762 - 2006-05-08 07:58 PM Re: Specific WMI Targets
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
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
#161763 - 2006-05-08 08:00 PM Re: Specific WMI Targets
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
No probs, like I said - good question, always wondered that too.

List them as in "look at them with your eyeballs" or list them as in "a script that will list them out" ?

Top
#161764 - 2006-05-08 08:01 PM Re: Specific WMI Targets
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
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
#161765 - 2006-05-08 08:27 PM Re: Specific WMI Targets
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
um, I am not aware of a COM object for dumping other COM object's properties/methods. I know that programatically it can be done (there are COM interfaces for dumping COM interfaces) - and I have seen classes and code for them. An internet search for a third-party library might yield something.

-Shawn

Top
#161766 - 2006-05-09 05:34 PM Re: Specific WMI Targets
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
Loc: Hilversum, The Netherlands
Apparantly thats a hard task to complete, I've tried different viewers to scan for COM objects but till now no succcess. It should be scriptable just like WMI is. like a ComOMatic or something. I guess it's the same old story... "everything that 'should' be per microsoft definition 'isn't'"
Top
#161767 - 2006-05-09 11:32 PM Re: Specific WMI Targets
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
I know this is a little late, but here are a few ways of getting the username.
Code:
BREAK ON
$Nul = SetOption("WrapAtEOL", "On")

$strComputer = @Wksta

$objWMIService = GetObject("winmgmts:\\" + $strComputer + "\root\cimv2")

$colItems = $objWMIService.Get(Win32_ComputerSystem)

? "UserName= " $objWMIService.InstancesOf(Win32_ComputerSystem).Item("Win32_ComputerSystem='" + $strComputer + "'").UserName

$colItems = $objWMIService.ExecQuery("Select * from Win32_ComputerSystem where name='" + @Wksta + "'")

? "Count=" $colItems.Count

? "UserName= " $colItems.Item("Win32_ComputerSystem='" + @Wksta + "'").UserName

? "UserName= " GetObject("winmgmts:\\" + $strComputer + "\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem where name='"
+ $strComputer + "'").Item("Win32_ComputerSystem='" + $strComputer + "'").UserName

? "UserName= " GetObject("winmgmts:\\" + $strComputer + "\root\cimv2").InstancesOf(Win32_ComputerSystem).Item("Win32_ComputerSystem='"
+ $strComputer + "'").UserName

_________________________
Kelly

Top
#161768 - 2006-05-10 08:38 AM Re: Specific WMI Targets
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
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
#161769 - 2006-05-10 03:03 PM Re: Specific WMI Targets
krabourn Offline
Hey THIS is FUN
*****

Registered: 2000-12-11
Posts: 244
Loc: San Antonio, Texas, USA
The problem is that Name is what the class, Win32_ComputerSystem, is indexed by. Index may not be the best way to describe it, but you have to know one of the values of a keyed attribute to get a attribute of a particular instance. You can use the WMI CIM Studio to see which attributes have the key next to it.

Here is an example getting the Computer's name from another class. Code:
? "BusSystemName= " GetObject("winmgmts:\\" + $strComputer + "\root\cimv2").InstancesOf(Win32_Bus).Item("Win32_Bus='PCI_BUS_0'").SystemName

_________________________
Kelly

Top
#161770 - 2006-05-11 03:38 PM Re: Specific WMI Targets
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
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_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
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:
 Code:
$strComputer = "."
? "ComputerName= "+GetObject("winmgmts:\\"+$strComputer+"\root\cimv2").ExecQuery("Select * from Win32_ComputerSystem").ItemIndex(0).Name

Top
#212127 - 2016-11-21 06:25 PM Re: Specific WMI Targets [Re: Arend_]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11623
Loc: CA
LOL, Well thanks for sharing Arend :-)
Top
Page 1 of 1 1


Moderator:  Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.07 seconds in which 0.023 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org