我使用以下代码获取主板序列号。但是我得到了o / p 结果是空的。我在这段代码中犯了什么错误?
File file = File.createTempFile("realhowto",".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs =
"Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n"
+ "Next \n";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
if(result.equalEgnoreCase(" ") {
System.out.println("Result is empty");
} else {
System.out.println("Result :>"+result);
}
input.close();
}
答案 0 :(得分:1)
Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
或
Process p = Runtime.getRuntime().exec("wmic /node:"HOST" bios get serialnumber");
而不是 HOST ,请提供主机名,这可以在cmd提示符下输入 hostname 。
答案 1 :(得分:1)
我可以确认VBS方面在我的机器上工作正常;但是,我得到的输出是:
MB-1234567890
这似乎并不特别独特或有帮助。尽管如此,如果这是您所追求的,请尝试以下方法。将VBS粘贴到.vbs文件中并使用cscript <myfile>.vbs
:
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_BaseBoard")
For Each objItem in colItems
Wscript.Echo objItem.SerialNumber
exit for ' do the first cpu only!
Next
如果可行的话,那就是Java出错了(我怀疑它没有因某种原因捕获过程输出)。否则,VBS脚本会让你失望。
this thread还有一些提示和提示可能会给你一些不同的策略。