调用WMI类的方法 [英] Calling a method of a WMI Class

查看:67
本文介绍了调用WMI类的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用以下方法: https://msdn.microsoft.com/zh-cn/library/dn469242(v = vs.85).aspx

I'm attempting to call the following method: https://msdn.microsoft.com/en-us/library/dn469242(v=vs.85).aspx

相应的Powershell为:

The corresponding powershell is:

wmic / namespace:\\ROOT\Microsoft\Windows\DesiredStateConfiguration类MSFT_DSCLocalConfigurationManager调用GetConfigurationStatus

我不太清楚如何使用go-ole正确地做到这一点。这是我到目前为止的内容。它失败并显示: invoke_test.go:47:wmi:调用方法GetConfiguration时出错:发生异常。通用失败)

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 )

当我尝试使用 GetConfigurationStatus 而不是 GetConfiguration ,出现 Unknown name 错误,因此我怀疑此方法存在,我只是在调用错误。

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
}


推荐答案

以管理员身份运行 wbemtest.exe ,您可以打开MSFT_DSCLocalConfigurationMa nager类并使用显示MOF 来查看其类定义:

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是一种用于返回当前配置文档的方法,而不是最后一次DSC运行的状态。您可能使用的是旧版DSC,请确保Powershell中的 $ psversiontable 至少显示5.0.10018.0。

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还将让您使用IWbemServices执行方法(类似于上面的代码)

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";

这篇关于调用WMI类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆