2019独角兽企业重金招聘Python工程师标准>>>
简介
WMI简介:
WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的 Windows 管理技术;用户可以使用 WMI 管理本地和远程计算机。
WQL简介:
WQL就是WMI中的查询语言,WQL的全称是WMI Query Language,简称为WQL,翻译成中文好像可以成为Windows管理规范查询语言。熟悉SQL语言的朋友会感觉它和SQL非常相似。
WQL其实非常简单,它有如下特点:
1、每个WQL语句必须以SELECT开始;
2、SELECT后跟你需要查询的属性名(我刚才对应SQL将其称之为字段名了),也可以像SQL一样,以*表示返回所有属性值;
3、FROM关键字;
4、你要查询的类的名字;
5、另外,如果你想精确查询结果还可以加上WHERE条件从句。比如某个类有Enable属性,你可以在查询的时候加上WHERE ENABLE=true,条件可以添加and or等关系操作。
如以下的都是正确的WQL语句:
Select * From Win32_LogicalDisk where drivetype=3 ’‘’‘查询所有硬盘分区
SELECT * FROM Win32_share ’‘查询所有共享目录
SELECT * FROM Win32_NetworkAdapterConfiguration WHERE DHCPEnabled=TRUE
SELECT Description FROM Win32_Account WHERE Name='Administrator'
SELECT Freespace,DeviceID FROM Win32_LogicalDisk
在搜索框输入wbemtest即可查看WQL测试工具
转载:http://my.oschina.net/noahxiao/blog/73163
1、环境准备
a、windows要开启Remote Registry与Windows Management Instrumentation服务
b、禁用放火墙
c、window2008系统注册表越狱(高版本的win)
需要修改regedit中HKEY_CLASSES_ROOT\CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6}的权限,windows2008不再给Administrators完全控制权。
http://files.helgeklein.com/downloads/SetACL/current/SetACL%20(executable%20version).zip
set_wmi_reg_acl.cmd
SetACL.exe -on "HKEY_CLASSES_ROOT\CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6}" -ot reg -actn setowner -ownr "n:Administrators"
SetACL.exe -on "HKEY_CLASSES_ROOT\CLSID\{76A64158-CB41-11D1-8B02-00600806D9B6}" -ot reg -actn ace -ace "n:Administrators;p:full"
以超级管理员身份执行这个cmd,就会把owner与完全控制权限授予Administrators组
注意这里需要把这个cmd命令和SetACL.exe copy到C:\Windows\System32文件夹
2、程序代码
java类
import org.jinterop.dcom.common.JIException;
import org.jinterop.dcom.common.JISystem;
import org.jinterop.dcom.core.*;
import org.jinterop.dcom.impls.JIObjectFactory;
import org.jinterop.dcom.impls.automation.IJIDispatch;
import org.jinterop.dcom.impls.automation.IJIEnumVariant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.UnknownHostException;
import java.util.logging.Level;
/**
* Created with IntelliJ IDEA.
* User: noah
* Date: 8/16/12
* Time: 8:00 AM
* To change this template use File | Settings | File Templates.
*/
public class WmiService {
private JIComServer m_ComStub = null;
private IJIComObject m_ComObject = null;
private IJIDispatch m_Dispatch = null;
private String m_Address = null;
private JISession m_Session = null;
private IJIDispatch m_WbemServices = null;
private static final String WMI_CLSID = "76A6415B-CB41-11d1-8B02-00600806D9B6";
private static final String WMI_PROGID = "WbemScripting.SWbemLocator";
private Logger logger = LoggerFactory.getLogger(this.getClass());
public WmiService(String address) {
JISystem.setAutoRegisteration(true);
JISystem.getLogger().setLevel(Level.WARNING);
m_Address = address;
}
public void query(String strQuery) {
System.out.println("query:" + strQuery);
JIVariant results[] = new JIVariant[0];
try {
results = m_WbemServices.callMethodA("ExecQuery", new Object[]{new JIString(strQuery), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM(), JIVariant.OPTIONAL_PARAM()});
IJIDispatch wOSd = (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
int count = wOSd.get("Count").getObjectAsInt();
IJIComObject enumComObject = wOSd.get("_NewEnum").getObjectAsComObject();
IJIEnumVariant enumVariant = (IJIEnumVariant) JIObjectFactory.narrowObject(enumComObject.queryInterface(IJIEnumVariant.IID));
IJIDispatch wbemObject_dispatch = null;
for (int c = 0; c < count; c++) {
Object[] values = enumVariant.next(1);
JIArray array = (JIArray) values[0];
Object[] arrayObj = (Object[]) array.getArrayInstance();
for (int j = 0; j < arrayObj.length; j++) {
wbemObject_dispatch = (IJIDispatch) JIObjectFactory.narrowObject(((JIVariant) arrayObj[j]).getObjectAsComObject());
}
String str = (wbemObject_dispatch.callMethodA("GetObjectText_", new Object[]{1}))[0].getObjectAsString2();
System.out.println("(" + c + "):");
System.out.println(str);
System.out.println();
}
} catch (JIException e) {
e.printStackTrace();
}
}
public void connect(final String domain, final String username, final String password) {
try {
m_Session = JISession.createSession(domain, username, password);
m_Session.useSessionSecurity(true);
m_Session.setGlobalSocketTimeout(5000);
m_ComStub = new JIComServer(JIProgId.valueOf(WMI_PROGID), m_Address, m_Session);
IJIComObject unknown = m_ComStub.createInstance();
m_ComObject = unknown.queryInterface(WMI_CLSID);
m_Dispatch = (IJIDispatch) JIObjectFactory.narrowObject(m_ComObject.queryInterface(IJIDispatch.IID));
JIVariant results[] = m_Dispatch.callMethodA(
"ConnectServer",
new Object[]{
new JIString(m_Address),
JIVariant.OPTIONAL_PARAM(),
JIVariant.OPTIONAL_PARAM(),
JIVariant.OPTIONAL_PARAM(),
JIVariant.OPTIONAL_PARAM(),
JIVariant.OPTIONAL_PARAM(),
0,
JIVariant.OPTIONAL_PARAM()
}
);
m_WbemServices = (IJIDispatch) JIObjectFactory.narrowObject((results[0]).getObjectAsComObject());
} catch (JIException e) {
e.printStackTrace();
if (m_Session != null) {
try {
JISession.destroySession(m_Session);
} catch (JIException e1) {
logger.error(e.getMessage(), e);
}
}
} catch (UnknownHostException e) {
if (m_Session != null) {
try {
JISession.destroySession(m_Session);
} catch (JIException e1) {
logger.error(e.getMessage(), e);
}
}
}
}
public void disconnect() {
try {
JISession.destroySession(m_Session);
} catch (JIException e) {
logger.error(e.getMessage(), e);
}
}
public static void main(String[] args) {
WmiService wmiService = new WmiService("172.16.158.129");
//域(一般为空字符),用户名,密码
注意:链接的用户一定是超级管理员Administrator
wmiService.connect("", "username", "password");
//系统信息
wmiService.query("SELECT * FROM Win32_ComputerSystem");
//CPU信息
wmiService.query("SELECT * FROM Win32_PerfFormattedData_PerfOS_Processor WHERE Name != '_Total'");
//内存信息
wmiService.query("SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory");
//磁盘信息
wmiService.query("SELECT * FROM Win32_PerfRawData_PerfDisk_PhysicalDisk Where Name != '_Total'");
wmiService.disconnect();
}
}
mavne配置(pom.xml)
3、有可能出现的问题
报错a
org.jinterop.dcom.common.JIException: Message not found for errorCode: 0xC0000034
at org.jinterop.winreg.smb.JIWinRegStub.winreg_OpenHKLM(JIWinRegStub.java:115)
at org.jinterop.dcom.core.JIProgId.getIdFromWinReg(JIProgId.java:130)
at org.jinterop.dcom.core.JIProgId.getCorrespondingCLSID(JIProgId.java:162)
at org.jinterop.dcom.core.JIComServer.
at org.noah.wmitest.WmiService.connect(WmiService.java:122)
at org.noah.wmitest.WmiService.main(WmiService.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: jcifs.smb.SmbException: The system cannot find the file specified.
at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:522)
at jcifs.smb.SmbTransport.send(SmbTransport.java:622)
at jcifs.smb.SmbSession.send(SmbSession.java:239)
at jcifs.smb.SmbTree.send(SmbTree.java:109)
at jcifs.smb.SmbFile.send(SmbFile.java:718)
at jcifs.smb.SmbFile.open0(SmbFile.java:923)
at jcifs.smb.SmbFile.open(SmbFile.java:940)
at jcifs.smb.SmbFileOutputStream.
at jcifs.smb.TransactNamedPipeOutputStream.
at jcifs.smb.SmbNamedPipe.getNamedPipeOutputStream(SmbNamedPipe.java:187)
at rpc.ncacn_np.RpcTransport.attach(RpcTransport.java:92)
at rpc.Stub.attach(Stub.java:105)
at rpc.Stub.call(Stub.java:110)
at org.jinterop.winreg.smb.JIWinRegStub.winreg_OpenHKLM(JIWinRegStub.java:113)
... 10 more
需要开启Remote Registry服务
报错b
org.jinterop.dcom.common.JIException: Message not found for errorCode: 0xC0000001
at org.jinterop.winreg.smb.JIWinRegStub.winreg_OpenHKLM(JIWinRegStub.java:115)
at org.jinterop.dcom.core.JIProgId.getIdFromWinReg(JIProgId.java:130)
at org.jinterop.dcom.core.JIProgId.getCorrespondingCLSID(JIProgId.java:162)
at org.jinterop.dcom.core.JIComServer.
at org.noah.wmitest.WmiService.connect(WmiService.java:87)
at org.noah.wmitest.WmiService.main(WmiService.java:142)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: jcifs.smb.SmbException: Connection timeout
jcifs.util.transport.TransportException: Connection timeout
或
org.jinterop.dcom.common.JIException: Message not found for errorCode: 0x8001FFFF
at org.jinterop.dcom.core.JIComServer.init(JIComServer.java:576)
at org.jinterop.dcom.core.JIComServer.initialise(JIComServer.java:481)
at org.jinterop.dcom.core.JIComServer.
at org.noah.wmitest.WmiService.connect(WmiService.java:87)
at org.noah.wmitest.WmiService.main(WmiService.java:142)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.net.ConnectException: Connection timed out