问题
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
}
回答1:
Running wbemtest.exe as an administrator you can open the MSFT_DSCLocalConfigurationManager class and use Show MOF to see it's class definition:
[ClassVersion("1.0.0"), locale(1033), dynamic: ToInstance, provider("dsccore"): ToInstance]
class MSFT_DSCLocalConfigurationManager
{
[implemented, static: DisableOverride ToSubClass] uint32 SendConfiguration([In, Octetstring: DisableOverride ToSubClass] uint8 ConfigurationData[], [In] boolean force);
[implemented, static: DisableOverride ToSubClass] uint32 SendConfigurationApply([In, Octetstring: DisableOverride ToSubClass] uint8 ConfigurationData[], [In] boolean force);
[implemented, static: DisableOverride ToSubClass] uint32 GetConfiguration([In, Octetstring: DisableOverride ToSubClass] uint8 configurationData[], [Stream: DisableOverride ToSubClass, EmbeddedInstance("OMI_BaseResource"): ToSubClass, Out] OMI_BaseResource configurations[]);
[implemented, static: DisableOverride ToSubClass] uint32 TestConfiguration([In, Octetstring: DisableOverride ToSubClass] uint8 configurationData[], [Out] boolean InDesiredState, [Out, EmbeddedInstance("MSFT_ResourceInDesiredState"): ToSubClass] MSFT_ResourceInDesiredState ResourcesInDesiredState[], [Out, EmbeddedInstance("MSFT_ResourceNotInDesiredState"): ToSubClass] MSFT_ResourceNotInDesiredState ResourcesNotInDesiredState[]);
[implemented, static: DisableOverride ToSubClass] uint32 ApplyConfiguration([In] boolean force);
[implemented, static: DisableOverride ToSubClass] uint32 SendMetaConfigurationApply([In, Octetstring: DisableOverride ToSubClass] uint8 ConfigurationData[]);
[implemented, static: DisableOverride ToSubClass] uint32 GetMetaConfiguration([Out, EmbeddedInstance("MSFT_DSCMetaConfiguration"): ToSubClass] MSFT_DSCMetaConfiguration MetaConfiguration);
[implemented, static: DisableOverride ToSubClass] uint32 RollBack([In] uint8 configurationNumber);
[implemented, static: DisableOverride ToSubClass] uint32 PerformRequiredConfigurationChecks([In] uint32 Flags);
[implemented, static: DisableOverride ToSubClass] uint32 StopConfiguration([In] boolean force);
[implemented, static: DisableOverride ToSubClass] uint32 GetConfigurationStatus([In] uint32 Flags, [Stream: DisableOverride ToSubClass, EmbeddedInstance("MSFT_DSCConfigurationStatus"): ToSubClass, Out] MSFT_DSCConfigurationStatus configurationStatus[]);
[implemented, static: DisableOverride ToSubClass] uint32 SendConfigurationApplyAsync([In, Octetstring: DisableOverride ToSubClass] uint8 ConfigurationData[], [In] boolean force, [In] string jobId);
[implemented, static: DisableOverride ToSubClass] uint32 GetConfigurationResultOutput([In] string jobId, [In, Octetstring: DisableOverride ToSubClass] uint8 resumeOutputBookmark[], [Out, Stream: DisableOverride ToSubClass, EmbeddedInstance("MSFT_DSCConfigurationOutput"): ToSubClass] MSFT_DSCConfigurationOutput output[]);
[implemented, static: DisableOverride ToSubClass] uint32 RemoveConfiguration([In] uint32 Stage, [In] boolean Force);
[implemented, static: DisableOverride ToSubClass] uint32 ResourceGet([In] string ResourceType, [In] string ModuleName, [In, Octetstring: DisableOverride ToSubClass] uint8 resourceProperty[], [Stream: DisableOverride ToSubClass, EmbeddedInstance("OMI_BaseResource"): ToSubClass, Out] OMI_BaseResource configurations[]);
[implemented, static: DisableOverride ToSubClass] uint32 ResourceSet([In] string ResourceType, [In] string ModuleName, [In, Octetstring: DisableOverride ToSubClass] uint8 resourceProperty[], [Out] boolean RebootRequired);
[implemented, static: DisableOverride ToSubClass] uint32 ResourceTest([In] string ResourceType, [In] string ModuleName, [In, Octetstring: DisableOverride ToSubClass] uint8 resourceProperty[], [Out] boolean InDesiredState);
};
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:
[abstract]
class __PARAMETERS
{
[Stream: DisableOverride ToSubClass, EmbeddedInstance("MSFT_DSCConfigurationStatus"): ToSubClass, Out, ID(1): DisableOverride ToInstance] MSFT_DSCConfigurationStatus configurationStatus[] = {
instance of MSFT_DSCConfigurationStatus
{
DurationInSeconds = 33;
HostName = "GBRAY02";
来源:https://stackoverflow.com/questions/30744205/calling-a-method-of-a-wmi-class