电脑时间越来越慢如何cmd命令同步时间(建临时VBS脚本提权,管理员执行java代码)
电脑时间越来越慢如何cmd命令同步时间
·
windows的设置里面同步报错,就使用cmd手动同步
net time /set
java代码表示
public static void main(String[] args) throws InterruptedException, IOException {
try {
// 创建临时VBS脚本提权
Path vbsFile = Files.createTempFile("elevate", ".vbs");
// Java 8兼容的多行字符串写法
String vbsContent = "Set UAC = CreateObject(\"Shell.Application\")\n" +
"UAC.ShellExecute \"cmd.exe\", \"/c net time /set /y \\\\dddd.china.youku.com\", \"\", \"runas\", 1";
// Java 8兼容的文件写入方式
try (BufferedWriter writer = Files.newBufferedWriter(vbsFile, StandardCharsets.UTF_8)) {
writer.write(vbsContent);
}
// 执行VBS脚本
ProcessBuilder pb = new ProcessBuilder("wscript", vbsFile.toString());
pb.redirectErrorStream(true);
Process process = pb.start();
// 读取输出(保持GBK编码)
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), "GBK"))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
int exitCode = process.waitFor();
System.out.println("Exit code: " + exitCode);
// 删除临时文件
Files.deleteIfExists(vbsFile);
} catch (Exception e) {
e.printStackTrace();
}
}
其他,
杀掉进程,管理员执行命令
public static void main(String[] args) {
try {
// 执行带有管理员权限的taskkill命令
String[] cmd = {
"powershell.exe",
"-Command",
"Start-Process",
"taskkill",
"-ArgumentList",
"'/F /IM msedge.exe'",
"-Verb",
"runAs"
};
Process process = Runtime.getRuntime().exec(cmd);
// 等待命令执行完成
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("PowerShell命令执行成功");
} else {
System.err.println("PowerShell命令执行失败,退出码:" + exitCode);
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
更多推荐
所有评论(0)