调用 WMI 类的方法

huangapple go评论75阅读模式
英文:

Calling a method of a WMI Class

问题

我正在尝试调用以下方法: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
}

希望这可以帮到你!

英文:

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

得分: 1

以管理员身份运行wbemtest.exe,您可以打开MSFT_DSCLocalConfigurationManager类,并使用Show 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来执行一个方法(类似于您上面的代码)。

调用 WMI 类的方法

它显示输出参数的类型为:

[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";
	}
}
英文:

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)

调用 WMI 类的方法

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

huangapple
  • 本文由 发表于 2015年6月10日 06:53:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/30744205.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定