调用 PowerShell 时添加 NoProfile 参数 (#3923)

This commit is contained in:
Glavo
2025-05-20 12:54:04 +08:00
committed by GitHub
parent 5479607597
commit 2f3bf61d94
2 changed files with 14 additions and 22 deletions

View File

@@ -17,10 +17,7 @@
*/ */
package org.jackhuang.hmcl.util.platform; package org.jackhuang.hmcl.util.platform;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -248,37 +245,31 @@ public final class CommandBuilder {
} }
public static boolean hasExecutionPolicy() { public static boolean hasExecutionPolicy() {
if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS) { if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS)
return true; return true;
} if (!OperatingSystem.isWindows7OrLater())
return false;
try { try {
final Process process = Runtime.getRuntime().exec(new String[]{"powershell", "-Command", "Get-ExecutionPolicy"}); String policy = SystemUtils.run("powershell.exe", "-NoProfile", "-Command", "Get-ExecutionPolicy").trim();
if (!process.waitFor(5, TimeUnit.SECONDS)) { return "Unrestricted".equalsIgnoreCase(policy) || "RemoteSigned".equalsIgnoreCase(policy);
process.destroy();
return false;
}
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), OperatingSystem.NATIVE_CHARSET))) {
String policy = reader.readLine();
return "Unrestricted".equalsIgnoreCase(policy) || "RemoteSigned".equalsIgnoreCase(policy);
}
} catch (Throwable ignored) { } catch (Throwable ignored) {
} }
return false; return false;
} }
public static boolean setExecutionPolicy() { public static boolean setExecutionPolicy() {
if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS) { if (OperatingSystem.CURRENT_OS != OperatingSystem.WINDOWS)
return true; return true;
} if (!OperatingSystem.isWindows7OrLater())
return false;
try { try {
final Process process = Runtime.getRuntime().exec(new String[]{"powershell", "-Command", "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"}); SystemUtils.run("powershell.exe", "-NoProfile", "-Command", "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser");
if (!process.waitFor(5, TimeUnit.SECONDS)) { return true;
process.destroy();
return false;
}
} catch (Throwable ignored) { } catch (Throwable ignored) {
return false;
} }
return true;
} }
private static boolean containsEscape(String str, String escapeChars) { private static boolean containsEscape(String str, String escapeChars) {

View File

@@ -58,6 +58,7 @@ public final class WindowsHardwareDetector extends HardwareDetector {
List<Map<String, String>> videoControllers = SystemUtils.run(Arrays.asList( List<Map<String, String>> videoControllers = SystemUtils.run(Arrays.asList(
"powershell.exe", "powershell.exe",
"-NoProfile",
"-Command", "-Command",
String.join(" | ", String.join(" | ",
getCimInstance + " -Class Win32_VideoController", getCimInstance + " -Class Win32_VideoController",