Solved

vbscript WMI Using Public Member Functions (nVidia NV2)

Posted on 2013-10-26
33
795 Views
Last Modified: 2013-11-18
Hi All,

I am trying to use some nVidia functions in there WMI API (attached, it is a txt file but should be renamed to chm for help file)

I am new to vbscript so can be doing something wrong.

My code is as follows:

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

arrComputers = Array(".")
For Each strComputer In arrComputers
   WScript.Echo
   WScript.Echo "=========================================="
   WScript.Echo "Computer: " & strComputer
   WScript.Echo "=========================================="

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\NV")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM SyncTopology", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems
		NodeID = objItem.id
		WScript.Echo "id: " & NodeID
		WScript.Echo
   Next

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\NV")
	Set SyncClass = objWMIService.Get("Sync")
	if (SyncClass.toggleSource()) Then
		wscript.echo "done!"
	End if
	
Next

Open in new window


I am actually trying to use a different function but this one is the easiest and takes no arguments.

The class is 'Sync' the function is toggleSource, should be too easy!

I get an error on line:
	if (SyncClass.toggleSource()) Then

Open in new window


stating:
C:\Users\User\Desktop\test3.vbs(28, 2) SWbemObjectEx: Invalid method Parameter(s)

I can query attributes in the class just can run methods :(

I can use these methods in Powershell so they should work, just can't get it working in vbscript!! AHHH...
nvwmi.txt
0
Comment
Question by:acrouch817
  • 18
  • 15
33 Comments
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I would suggest downloading the WMI Object Browser: http://www.microsoft.com/en-au/download/details.aspx?id=24045.  It will at least be able to tell you if your WMI path is correct.
I was going to suggest trying to call the method indirectly using the ExecMethod function of the SWbemObjectEx but it would be guess work until you know for sure that the path is correct (for vbs use anyway)
0
 

Author Comment

by:acrouch817
Comment Utility
Hi tagit,

The path is correct, how would one use ExecMethod to run a method? I only see examples on how to use it with an executable (I apologize in advanced for what appears to be a 'stupid' question).
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I'd like you to just try testing the path with :NV instead of /NV

such that your GetObject becomes:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2:NV")
0
 

Author Comment

by:acrouch817
Comment Utility
When I try:

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2:NV")

Open in new window

I get an error: (null): 0x80041002
I tried:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\NV:Sync")

Open in new window

But get the same error when trying to access the method...
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
As for the execMethod, you can use it with your namespace (but I can't test this in my environment):

Set WbemServices = GetObject("winmgmts:")
Set oService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2:NV.Sync")
Set oPath = oService.Path_
WbemServices.ExecMethod oPath, "toggleSource"

Open in new window

0
 

Author Comment

by:acrouch817
Comment Utility
Thanks for the help!

I tried the method example.

Had to change to "CIMV2\NV:Sync" (otherwise that line gave an error)

I get an error on the last line SWbemServicesEx: Not Found

....
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Ok so the path is right - sorry I just had to make absolutely sure.  The manual also confirms it.

Did you try the root/CIMV2/NV.Sync ??

It does seem you're doing everything right so I wonder if it's a permissions issue?  What is your environment? Wiindows 8 etc etc

Did you also download the WMI Browser?  It will show you the NV namespace along with the Sync objects and others beneath it.  It will also show you the methods etc.  If you have this up and you try and click it will also tell you if you don't have the right permissions to access it.
0
 

Author Comment

by:acrouch817
Comment Utility
I am running Windows 7 Embedded, but again I can connect to the class just not the method :(

I did try NV.Sync and I get an error.


I did try the WMI Browser and maybe misinterpreted it, it shows a red line through the methods, I thought before this is just what the methods should show but now think that this means they are not accessible?
0
 

Author Comment

by:acrouch817
Comment Utility
I can run the method with power shell though, as the same user check if powershell does something fancy that vbscript does not
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
I did try the WMI Browser and maybe misinterpreted it, it shows a red line through the methods, I thought before this is just what the methods should show but now think that this means they are not accessible?

Certainly would seem that way... I had to run the WMI browser in IE8 to get it to work.  There certainly could be security restrictions in place for scripting vs using powershell.

Should've asked this earlier but why don't you just use powershell?
0
 

Author Comment

by:acrouch817
Comment Utility
i am integrating this snipit in a larger vbs file already created, putting a powershell would be an ugly thing to add in the vbs script that I would like to avoid :)
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Sure and it would be.
How did you run the wmi browser? I could get it only to work in ie8
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
It's certainly looking like the the methods are inaccessible for some reason.  Have you tried to run your script with elevated privledges.  ie right click your vbs and "run as administrator" ??
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Can you post a screenshot of the red lines through the methods?
Also does it ask you to log in when running the wmi browser?
0
 

Author Comment

by:acrouch817
Comment Utility
Sorry for the late reply,,,, so before I was running it remotely from the domain controller on a network now I am running it on a client, directly with admin rights.

I get an error on:
WbemServices.ExecMethod oPath, "toggleSource"

It is:
SWbemServicesEx: not found....

Sorry to jump around :(
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
All good to jump around :-)
What path did you use when you got that error?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:acrouch817
Comment Utility
ok, i am running on windows embedded and am remote right now and can't put IE 10 on it so objectbrowser is not working... :(

Using path \root\CIMV2\NV:Sync

If I use \root\CIMV2:NV.Sync, I get an error on this line so using the \NV:Sync
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
If you press f12 and bring up the dev tools you can change the standards to ie7 or ie8. That should allow the browser to work
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
And the \NV:Sync working?
0
 

Author Comment

by:acrouch817
Comment Utility
ok back..

So yes \NV:Sync is working!

Also had no IE installed, got IE 9 installed Object Browser works!

Attached is the screen shot of the methods of Sync.

Thanks!
Untitled.png
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Ha, yes only works in IE (thanks Microsoft).

When you run the browser, right click it first and run as administrator.  Does that remove the red lines?

Ok so we know the path is correct and that the functions are there but permissions are not correct to allow you to use them, hence why I'm trying to get you to run the browser with elevated permissions to see if it makes a difference.

The other thing to note is that you should be prompted to login when the browser first opens  so try a few combinations here as well to see if it makes any difference.
0
 

Author Comment

by:acrouch817
Comment Utility
sorry back so late, got all weekend on this one...

I tried a bunch of stuff, no luck.  I am logged on as Administrator though.  You can't start ObjectBrowser as admin, so I start IE as Admin and then try to go to ObjectBrowser, no change... :(
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
:) fun when you've got all the time in the world eh?

When you run IE as admin and go to the ObjectBrowser, does it prompt you to log in?
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
i get the following prompts:

namespace
click enable all privileges
enable all priv
0
 

Author Comment

by:acrouch817
Comment Utility
ok o did some more work here....

I did not know about the enable so thanks, but tried this and no change...

FYI, never asked for a password....

but I looked at the method 'rename' of Win32_ComputerSystem class and this too has the red check but I can run it!

So maybe the permissions are OK?

I reran the original script and I am getting "SWbemObjectEX: Type mismatch", maybe I am inputting the data incorrect?

I am calling setSyncStateByID(2147881091, 1)


Function is described by:

boolean setSyncStateById  ( [in] uint32  syncDisplayIds[],  
  [in] uint32  syncState[]  
 )    


Parameters:
 syncDisplayIds  Array of display IDs to synchronize(SyncTopology.id), separated by comma. Order is important. For Mosaic topologies, user can specify either a master display or all displays in a Mosaic topology. If user does not specify any displays, the method will choose the best topology to synchronize. To un-synchronize the displays, user will have to pass all the displays with displaySyncState as UnSynced.  

 syncState  Possible values are: 0 - UnSynced, 1 - Slave, 2 - Master


Maybe the numbers really need to be an array???
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Yes that's what it looks like so what if you try:
 setSyncStateByID(Array(2147881091), Array(1))
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 500 total points
Comment Utility
Sounds right about the permissions as  well. But we needed to go through that process :-)
0
 

Author Comment

by:acrouch817
Comment Utility
FIGURED IT OUT!!

working on code will be back!  Thanks for the help!!!!
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
Yes! Sounds great! Please post back :-)
0
 

Author Comment

by:acrouch817
Comment Utility
So finally back!

This was how I needed to input the code:

SyncType = Array(0)
NodeID = Array(12345678)

Set objWMIService = GetObject("winmgmts:" _
			& "{impersonationLevel=impersonate}!\\" & '." & "\root\cimv2\NV")

Set colComputers = objWMIService.ExecQuery ("Select * from Sync")

For Each objComputer in colComputers
		ErrCode = objComputer.setSyncStateById(NodeID, SyncType)
Next

Open in new window


Obviously simplified down...

What is the Array(X) do?  It is hard to google because if you google 'array' you get array informaiton :)
0
 

Author Closing Comment

by:acrouch817
Comment Utility
Great help through the problem!
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
Comment Utility
the sync state function is expecting two arguments: nodeID and syncType. But it also is designed so that you can give it more than one nodeID so even though your only doing one node it doesn't know that and is expecting an array, so you have to pass it an array with only one element.

You could also do this if you had more than one nodeID, or wanted to change the same nodeID's syncType multiple times:

SyncType = Array(0,1,0) ' corresponding states for following IDs 
NodeID = Array(12345678, 2341235, 3388382) ' assuming these are valid IDs

Set objWMIService = GetObject("winmgmts:" _
			& "{impersonationLevel=impersonate}!\\" & '." & "\root\cimv2\NV")

Set colComputers = objWMIService.ExecQuery ("Select * from Sync")

For Each objComputer in colComputers
		ErrCode = objComputer.setSyncStateById(NodeID, SyncType)
Next

Open in new window


Make sense?
0
 

Author Comment

by:acrouch817
Comment Utility
Yes, I think now it does :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Utilizing an array to gracefully append to a list of EmailAddresses
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now