windows 无法获取主板序列号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5581551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get motherboard serial number
提问by bharath
I used the following code for getting the motherboard serial number. But I got the o/p Result is empty. What mistake did I make in this code?
我使用以下代码获取主板序列号。但我得到 o/p Result is empty。我在这段代码中犯了什么错误?
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();
}
采纳答案by Lunivore
I can confirm that the VBS side of this works fine on my machine; however, the output I got was:
我可以确认这个 VBS 端在我的机器上工作正常;然而,我得到的输出是:
MB-1234567890
which doesn't seem particularly unique or helpful. Still, if this is what you're after, try the following. Paste the VBS into a .vbs file and run it using cscript <myfile>.vbs
:
这似乎不是特别独特或有用。不过,如果这就是您所追求的,请尝试以下操作。将 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
If that works, it's the Java that's at fault (and I suspect it's not capturing the process output for some reason). Otherwise, it's the VBS script failing you.
如果这有效,则是 Java 有问题(我怀疑由于某种原因它没有捕获进程输出)。否则,它是 VBS 脚本失败了。
There are some more hints and tips on this threadwhich might give you some different strategies.
在这个线程上还有一些提示和技巧,它们可能会给你一些不同的策略。
回答by Dead Programmer
Process p = Runtime.getRuntime().exec("wmic baseboard get serialnumber");
or
或者
Process p = Runtime.getRuntime().exec("wmic /node:"HOST" bios get serialnumber");
instead of HOST, give ur hostname, which can be arrived at typing hostnamein cmd prompt.
而不是HOST,给你的主机名,它可以在 cmd 提示符下输入主机名。