I'm attempting to call the following method: https://msdn.microsoft.com/en-us/library/dn469242(v=vs.85).aspx
The corresponding powershell is:
wmic /namespace:\\ROOT\Microsoft\Windows\DesiredStateConfiguration class MSFT_DSCLocalConfigurationManager call GetConfigurationStatus
I can't quite figure out how to do it correctly using go-ole. Here's what I have so far. It fails with: invoke_test.go:47: wmi: error calling method GetConfiguration: Exception occurred. Generic failure )
When I try using GetConfigurationStatus
instead of GetConfiguration
, I get a Unknown name
error, so I suspect this method exists I'm just invoking it wrong.
package wmi
import (
"testing"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
func TestMethod(t *testing.T) {
err := ole.CoInitializeEx(0, ole.COINIT_MULTITHREADED)
if err != nil {
t.Fatal(err)
}
unknown, err := oleutil.CreateObject("WbemScripting.SWbemLocator")
if err != nil {
t.Fatal(err)
}
defer unknown.Release()
wmi, err := unknown.QueryInterface(ole.IID_IDispatch)
if err != nil {
t.Fatal(err)
}
defer wmi.Release()
serviceRaw, err := oleutil.CallMethod(wmi, "ConnectServer", nil, `ROOT\Microsoft\Windows\DesiredStateConfiguration`)
if err != nil {
t.Fatal(err)
}
service := serviceRaw.ToIDispatch()
defer serviceRaw.Clear()
typeName := "MSFT_DSCLocalConfigurationManager"
typeRaw, err := oleutil.CallMethod(service, "Get", typeName)
if err != nil {
t.Fatalf("wmi: error fetching type %v: %v", typeName, err)
}
typeResult := typeRaw.ToIDispatch()
defer typeRaw.Clear()
methodName := "GetConfiguration"
methodRaw, err := oleutil.CallMethod(typeResult, methodName)
if err != nil {
t.Fatalf("wmi: error calling method %v: %v", methodName, err)
}
item := methodRaw.ToIDispatch()
defer methodRaw.Clear()
_ = item
}
Running wbemtest.exe as an administrator you can open the MSFT_DSCLocalConfigurationManager class and use Show MOF to see it's class definition:
GetConfiguration is a method used to return the current configuration document, not the status of the last DSC run. You may be using an older version of DSC, make sure $psversiontable in powershell shows at least 5.0.10018.0.
wbemtest.exe will also let you use IWbemServices to execute a method (similar to the code you have above)
and it shows the output parameters are of type: