我试图调用以下方法:https://msdn.microsoft.com/en-us/library/dn469242(v=vs.85).aspx
相应的powershell是:
wmic /namespace:\\ROOT\Microsoft\Windows\DesiredStateConfiguration class MSFT_DSCLocalConfigurationManager call GetConfigurationStatus
我无法弄清楚如何使用go-ole正确地做到这一点。这是我到目前为止所拥有的。它失败了:invoke_test.go:47: wmi: error calling method GetConfiguration: Exception occurred. Generic failure )
当我尝试使用GetConfigurationStatus
代替GetConfiguration
时,我收到Unknown name
错误,因此我怀疑这种方法存在,我只是错误地调用它。
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
}
答案 0 :(得分:1)
以管理员身份运行 wbemtest.exe ,您可以打开MSFT_DSCLocalConfigurationManager类并使用显示MOF 查看其类定义:
[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是一种用于返回当前配置文档的方法,而不是最后一次DSC运行的状态。您可能正在使用较旧版本的DSC,请确保powershell中的 $ psversiontable 至少显示5.0.10018.0。
wbemtest.exe也允许你使用IWbemServices来执行一个方法(类似于你上面的代码)
并显示输出参数的类型为:
[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";