~~~
//这段网上复制来的 看起来有点像 估计改下可以实现我要的功能 不太看得懂 求助 希望帮改改下
//这个是一段 利用WMI获取当前Windows下进程的演示代码
//【如果可以 可以给个红包意思意思辛苦费嘻嘻!】
package main
import (
"github.com/mattn/go-ole"
"github.com/mattn/go-ole/oleutil"
)
func main() {
// init COM, oh yeah
ole.CoInitialize(0)
defer ole.CoUninitialize()
unknown, _ := oleutil.CreateObject("WbemScripting.SWbemLocator")
defer unknown.Release()
wmi, _ := unknown.QueryInterface(ole.IID_IDispatch)
defer wmi.Release()
// service is a SWbemServices
serviceRaw, _ := oleutil.CallMethod(wmi, "ConnectServer")
service := serviceRaw.ToIDispatch()
defer service.Release()
// result is a SWBemObjectSet
resultRaw, _ := oleutil.CallMethod(service, "ExecQuery", "SELECT * FROM Win32_Process")
result := resultRaw.ToIDispatch()
defer result.Release()
countVar, _ := oleutil.GetProperty(result, "Count")
count := int(countVar.Val)
for i :=0; i < count; i++ {
// item is a SWbemObject, but really a Win32_Process
itemRaw, _ := oleutil.CallMethod(result, "ItemIndex", i)
item := itemRaw.ToIDispatch()
defer item.Release()
asString, _ := oleutil.GetProperty(item, "Name")
println(asString.ToString())
}
}
~~~
~~~
改这样的例子
这是一段 powershell的代码
$getWMi = gwmi -n root\wmi -cl testData
$serverID = $getWMi.AddSession("test_Init")
$Getsession = gwmi -n root\wmi -q "select * from test where SessionId=$($serverID.SessionId)"
$output = $Getsession.GetValue("data/yu").value
echo "$output"
~~~
wmi很有局限性,某些电脑 wmi是不能启动的,不要问我为什么知道,运维的 呵呵。 您可以参考下
- go ps库 https://github.com/mitchellh/go-ps
- gopsutil库 https://github.com/shirou/gopsutil
- https://github.com/AllenDang/w32
#14