StackExchange / wmi

WMI for Go
http://godoc.org/github.com/StackExchange/wmi
MIT License
434 stars 173 forks source link

Support for calling methods on WMI Classes #6

Closed gbrayut closed 3 years ago

gbrayut commented 9 years ago

In addition to WMI queries it would be very helpful if I could call specific methods on WMI classes. This would allow access to a bunch of things that cannot be directly accessed via WQL. For instance, the following Powershell can be used to call a WMI method on a class to get details about DSC (Run on ny-rdp01 for example).

$Result = Invoke-WmiMethod -Class MSFT_DSCLocalConfigurationManager -Namespace ROOT\Microsoft\Windows\DesiredStateConfiguration -Name GetConfigurationStatus 
$Result.ConfigurationStatus #Instance of MSFT_DSCConfigurationStatus

I would like to to be able to create a MSFT_DSCConfigurationStatus struct in go that has the properties I want, then populate that struct using something like the current wmi.Query function.

Looking at the current code this should be possible using SWbemObject and SWbemMethod

I'll see if I am able to create something that works and then create a pull request for discussion.

JamesCullum commented 5 years ago

To call a method, you need to retrieve the class and call an object like this

// Connect to namespace
// root/PanasonicPC = winmgmts:\\.\root\PanasonicPC
serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", nil, "root/PanasonicPC")
if err != nil {
    log.Panic(err)
}
service := serviceRaw.ToIDispatch()
defer serviceRaw.Clear()

// Get class
setBiosRaw, err := oleutil.CallMethod(service, "Get", "SetBIOS4Conf")
if err != nil {
    log.Panic(err)
}
setBios := setBiosRaw.ToIDispatch()
defer setBiosRaw.Clear()

// Run method
resultRaw, err := oleutil.CallMethod(setBios, "AccessAuthorization", "letmein")
resultVal := resultRaw.Value().(int32)
mholt commented 5 years ago

I'm extremely interested in this. How can I help make this happen? Would you accept a PR?

@JamesCullum Were you able to get that working external to this library, or did you put that inside this package?

EDIT: Nevermind, I see now that it relies on wmi as created in the CreateQuery method. I've got your code working, I think. Would a PR be reviewed and accepted?

EDIT 2: Okay, yes, I've triply confirmed I have this working. Let me know if you want the PR...

JamesCullum commented 5 years ago

Hey @mholt,

It's a separate code, that is dependant on oleutil & wmi - not specificially this wrapper.

My code is more of a sample to show how to do it. I think a RP would be useful, but then it would need a more clean interface. Do you want to create one or how do we want to do it?

I'm not sure how the maintenance of this repository works, it looks not properly maintained.

mholt commented 5 years ago

@JamesCullum Yeah, I think I have an idea of what I want it to look like, but I would appreciate your review so it can handle your use cases, too. (My use case so far is only a single method.)

It doesn't appear to be maintained anymore :( So I'm thinking of forking it and making the change to my own repo...

captncraig commented 5 years ago

@mholt "not maintained" may be slight overstatement, but yeah, we haven't needed a lot of changes lately. Can definitely review a pr if you want.

mholt commented 5 years ago

Yay! I'll try to put one together today or tomorrow!

(I meant no offense of course. This library has already saved my bacon at least twice.)

mholt commented 5 years ago

Feel free to take a look at https://github.com/StackExchange/wmi/pull/45

JamesCullum commented 5 years ago

Thank you for the quick work. Reviewed the changes and put that into the PR.