gpt4 book ai didi

Java JACOB WMI 查询本地组成员身份

转载 作者:行者123 更新时间:2023-12-01 06:17:01 28 4
gpt4 key购买 nike

我正在研究如何使用 JACOB 在计算机上拉取本地组的成员。

我能做什么:使用单独的 WMI 查询从计算机中提取本地组列表。我不能做的:拉取这些组的成员。

我使用下面的文章给出了程序的框架:Using Java, How can I get a list of all local users on a windows machine

问题:我相信我的问题要么出在我正在使用的 WMI 查询中,要么出在程序如何使用查询的输出中。

下面是我正在使用的代码。我包含了在代码示例中使用的两个查询。这是“实例”变体。

import java.util.Enumeration;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class WMITest {
public static void main(String[] args) {
ComThread.InitMTA();
try {
ActiveXComponent wmi = new ActiveXComponent("winmgmts:\\\\.");

//Variant instances = wmi.invoke("InstancesOf", "Win32_Group Where LocalAccount=True");

Variant instances = wmi.invoke("InstancesOf", "Win32_GroupUser WHERE GroupComponent=\"Win32_Group.Domain='.',Name='Administrators'\"");

Enumeration<Variant> en = new EnumVariant(instances.getDispatch());
while (en.hasMoreElements())
{
ActiveXComponent bb = new ActiveXComponent(en.nextElement().getDispatch());
System.out.println(bb.getPropertyAsString("Name"));

}
} finally {
ComThread.Release();
System.exit(1);
}
}
}

最佳答案

请小心查询,因为动词(where)之前必须有一个空格,请注意我的代码

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;
import com.jacob.com.Dispatch;

// you have to add Jacob library into your project
// you have to copy jacob.xx.xx.dll from Jacob Library (both x64 and x86) and paste in c:/...java/jdk/bin (both x64 and x86)
// you can call any class in WMI by checking class and prop by using WMI Explorer.


public class GET_COM_PROPERTIES_CLASS {

// Step 1: Set exported value , you call them from another class.

public static String COMNAME; //Get Computer Name from Win32_Computersystem-
public static String SERIAL_NUMBER; //Get Serial Number from Win32_BIOS-
public static String BIOS_VERSION; //Get Bios Version from Win32_BIOS-
public static String NOTEBOOK_BRAND; //Get Notebook Brand from Win32_Computersystem-
public static String DOMAIN; // Get domain from Win32_Computersystem-
public static String SYSTEM_FAMILY; //Get Laptop model from Win32_Computersystem-
public static String SYSTEM_TYPE; //Get x64 or x86 from Win32_Computersystem-
public static String WORKGROUP; //Get Workgroup name from Win32_Computersystem-
public static String MOTHER_BOARD_PN; //Get ther board partnumber from Win32_MotherboardDevice-
public static String GRAPHIC_CARD_NAME; //Get graphic car name(Prop: name)Win32_VideoController-
public static String GRAPHIC_CARD_VER; //Get graphic card version from Win32_VideoController-
public static String GRAPHIC_SOLUTION; //Get solution of card from Win32_VideoController-
public static String DISK_SIZE; //Get disk size from Win32_DiskDrive-
public static String DISK_INTERFACE_TYPE; //Get IDE or other type of interface from Win32_DiskDrive-
public static String WIN_VERSION; // Get Window version from Win32_OperatingSystem-
public static String WIN_SERIAL_NUMBER; // Get windows serial number from Win32_OperatingSystem




public static void main(String[] args) {


//Step 2: Set Var of ActiveXComponent
ActiveXComponent wmi_Computersystem, wmi_Win32_BIOS, wmi_Win32_MotherboardDevice, wmi_Win32_VideoController,
wmi_Win32_DiskDriver, wmi_Win32_OperatingSystem = null;

//Set SWbemLocator for specify server or your computer and it is used for connect WMI
wmi_Computersystem = new ActiveXComponent("WbemScripting.SWbemLocator");
wmi_Win32_BIOS = new ActiveXComponent("WbemScripting.SWbemLocator");
wmi_Win32_MotherboardDevice = new ActiveXComponent("WbemScripting.SWbemLocator");
wmi_Win32_VideoController = new ActiveXComponent("WbemScripting.SWbemLocator");
wmi_Win32_DiskDriver = new ActiveXComponent("WbemScripting.SWbemLocator");
wmi_Win32_OperatingSystem = new ActiveXComponent("WbemScripting.SWbemLocator");



//Step 3: Call method of SWbemLocator to connect server or your computer

Variant conRet_Computersystem = wmi_Computersystem.invoke("ConnectServer");
Variant conRet_Win32_BIOS = wmi_Win32_BIOS.invoke("ConnectServer");
Variant conRet_Win32_MotherboardDevice = wmi_Win32_MotherboardDevice.invoke("ConnectServer");
Variant conRet_Win32_VideoController = wmi_Win32_VideoController.invoke("ConnectServer");
Variant conRet_Win32_DiskDriver = wmi_Win32_DiskDriver.invoke("ConnectServer");
Variant conRet_Win32_OperatingSystem = wmi_Win32_OperatingSystem.invoke("ConnectServer");


//Step 4: Prepare object for query
ActiveXComponent wmiconnect_Computersystem = new ActiveXComponent(conRet_Computersystem.toDispatch());
ActiveXComponent wmiconnect_Win32_BIOS = new ActiveXComponent(conRet_Win32_BIOS.toDispatch());
ActiveXComponent wmiconnect_Win32_MotherboardDevice = new ActiveXComponent(conRet_Win32_MotherboardDevice.toDispatch());
ActiveXComponent wmiconnect_Win32_VideoController = new ActiveXComponent(conRet_Win32_VideoController.toDispatch());
ActiveXComponent wmiconnect_Win32_DiskDriver = new ActiveXComponent(conRet_Win32_DiskDriver.toDispatch());
ActiveXComponent wmiconnect_SYSTEM_TYPE = new ActiveXComponent(conRet_Win32_OperatingSystem.toDispatch());



//Step 5: Set query
String query_Computersystem = "select Name, Manufacturer, Domain, SystemFamily, SystemType, Workgroup " +
"from Win32_Computersystem";

String query_Win32_BIOS = "select SerialNumber, BIOSVersion " +
"from Win32_BIOS";

String query_Win32_MotherboardDevice = "select PNPDeviceID " +
"from Win32_MotherboardDevice";

String query_Win32_VideoController = "select Name, DriverVersion, VideoModeDescription " +
"from Win32_VideoController";

String query_Win32_DiskDrive = "select Size, InterfaceType " +
"from Win32_DiskDrive";

String query_Win32_OperatingSystem = "select Caption, SerialNumber " +
"from Win32_OperatingSystem";

//Step 6: Set object for enum
Variant vCollection_Computersystem = wmiconnect_Computersystem.invoke("ExecQuery", new Variant(query_Computersystem));
Variant vCollection_Win32_BIOS = wmiconnect_Computersystem.invoke("ExecQuery", new Variant(query_Win32_BIOS));
Variant vCollection_Win32_MotherboardDevice = wmiconnect_Computersystem.invoke("ExecQuery", new Variant(query_Win32_MotherboardDevice));
Variant vCollection_Win32_VideoController = wmiconnect_Computersystem.invoke("ExecQuery", new Variant(query_Win32_VideoController));
Variant vCollection_Win32_DiskDrive = wmiconnect_Computersystem.invoke("ExecQuery", new Variant(query_Win32_DiskDrive));
Variant vCollection_Win32_OperatingSystem = wmiconnect_Computersystem.invoke("ExecQuery", new Variant(query_Win32_OperatingSystem));


//Step 7: Set Enum and Dispatch object
EnumVariant enumVariant_Computersystem = new EnumVariant(vCollection_Computersystem.toDispatch());
EnumVariant enumVariant_Win32_BIOS = new EnumVariant(vCollection_Win32_BIOS.toDispatch());
EnumVariant enumVariant_Win32_MotherboardDevice = new EnumVariant(vCollection_Win32_MotherboardDevice.toDispatch());
EnumVariant enumVariant_Win32_VideoController = new EnumVariant(vCollection_Win32_VideoController.toDispatch());
EnumVariant enumVariant_Win32_DiskDrive = new EnumVariant(vCollection_Win32_DiskDrive.toDispatch());
EnumVariant enumVariant_Win32_OperatingSystem = new EnumVariant(vCollection_Win32_OperatingSystem.toDispatch());
Dispatch item_Computersystem, item_Win32_BIOS, item_Win32_MotherboardDevice, item_Win32_VideoController, item_Win32_DiskDrive,
item_Win32_OperatingSystem = null;

//Step 8: Set Conmtread for Try
ComThread.InitMTA();

//Step 9: Step Try anf Finally


try {

while (enumVariant_Computersystem.hasMoreElements()) {
//Step 10: Set Item
item_Computersystem = enumVariant_Computersystem.nextElement().toDispatch();
item_Win32_BIOS = enumVariant_Win32_BIOS.nextElement().toDispatch();
item_Win32_MotherboardDevice = enumVariant_Win32_MotherboardDevice.nextElement().toDispatch();
item_Win32_VideoController = enumVariant_Win32_VideoController.nextElement().toDispatch();
item_Win32_DiskDrive = enumVariant_Win32_DiskDrive.nextElement().toDispatch();
item_Win32_OperatingSystem = enumVariant_Win32_OperatingSystem.nextElement().toDispatch();


// call prop of Win32_ComputerSystem
COMNAME = Dispatch.call(item_Computersystem, "Name").toString();
NOTEBOOK_BRAND = Dispatch.call(item_Computersystem, "Manufacturer").toString();
DOMAIN = Dispatch.call(item_Computersystem, "Domain").toString();
SYSTEM_FAMILY = Dispatch.call(item_Computersystem, "SystemFamily").toString();
SYSTEM_TYPE = Dispatch.call(item_Computersystem, "SystemType").toString();
WORKGROUP = Dispatch.call(item_Computersystem, "Workgroup").toString();

// call prop of Win32_BIOS
SERIAL_NUMBER = Dispatch.call(item_Win32_BIOS, "SerialNumber").toString();
BIOS_VERSION = Dispatch.call(item_Win32_BIOS, "BIOSVersion").toString();

//call prop of Win32_MotherboardDevice
MOTHER_BOARD_PN = Dispatch.call(item_Win32_MotherboardDevice, "PNPDeviceID").toString();

//call prop of Win32_VideoController
GRAPHIC_CARD_NAME = Dispatch.call(item_Win32_VideoController, "Name").toString();
GRAPHIC_CARD_VER = Dispatch.call(item_Win32_VideoController, "DriverVersion").toString();
GRAPHIC_SOLUTION = Dispatch.call(item_Win32_VideoController, "VideoModeDescription").toString();

//call prop of Win32_DiskDrive
DISK_SIZE = Dispatch.call(item_Win32_DiskDrive, "Size").toString();
DISK_INTERFACE_TYPE = Dispatch.call(item_Win32_DiskDrive, "InterfaceType").toString();

//call prop of Win32_OperatingSystem
WIN_VERSION = Dispatch.call(item_Win32_OperatingSystem, "Caption").toString();
WIN_SERIAL_NUMBER = Dispatch.call(item_Win32_OperatingSystem, "SerialNumber").toString();




//output for Win32_Computersystem
System.out.println("Computer Name : " + COMNAME + "\n");
System.out.println("Notebook Brand : " + NOTEBOOK_BRAND + "\n");
System.out.println("Domain Name : " + DOMAIN + "\n");
System.out.println("System Family : " + SYSTEM_FAMILY + "\n");
System.out.println("System Type : " + SYSTEM_TYPE + "\n");
System.out.println("Workgroup Name : " + WORKGROUP + "\n");

//output for Win32_BIOS
System.out.println("Serial Number : " + SERIAL_NUMBER + "\n");
System.out.println("Bios Version : " + BIOS_VERSION + "\n");

//output for Win32_MotherboardDevice
System.out.println("MotherBoard Part Number : " + MOTHER_BOARD_PN + "\n");

//output for Win32_VideoController
System.out.println("Graphic Card Name : " + GRAPHIC_CARD_NAME + "\n");
System.out.println("Graphic Card Version : " + GRAPHIC_CARD_VER + "\n");
System.out.println("Graphic Version : " + GRAPHIC_SOLUTION + "\n");

//output for Win32_DiskDrive
System.out.println("Disk Size : " + DISK_SIZE + "\n");
System.out.println("Disk Interface Type : " + DISK_INTERFACE_TYPE + "\n");

//output for Win32_OperatingSystem
System.out.println("Windows Version : " + WIN_VERSION + "\n");
System.out.println("Windows Serial Number : " + WIN_SERIAL_NUMBER + "\n");

break;

}
} finally {
ComThread.Release();

}
}

}

关于Java JACOB WMI 查询本地组成员身份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22337564/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com