gpt4 book ai didi

java - 从 Java 调用 PowerShell 时偶尔出现 Windows 权限错误 - "running scripts disabled"

转载 作者:行者123 更新时间:2023-12-01 10:36:23 26 4
gpt4 key购买 nike

我有一个 Java 程序,它执行 shell 调用以使用脚本调用 PowerShell。

我的 PS 脚本行如下所示:

execPS:powershell -command "& 'P:/PSScripts/DownloadSslHtml.ps1' -url 'https://somewebsite.com' -outputFile 'P:/DownloadedData/SomeFile.zip' -PFXPath 'P:/Properties/Config/certfile.pfx' -PFXPassword 'cert_password'"

有问题的 PS 脚本具有以下代码:
param([string]$url = "https://somewebsite.com",
[string]$outputFile = "P:/DownloadedData/someoutput.html",
[string]$PFXPath = "P:/Properties/Config/certfile.pfx",
[string]$PFXPassword = "cert_password")

$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($PFXPath,$PFXPassword,'UserKeySet')
Invoke-WebRequest -Uri $url -Certificate $cert -OutFile $outputFile -Verbose

如您所见,这非常简单。

我的 Java shell 调用如下所示:
public static String downloadSslWithPshell(String url, String outputFileName, String certName,
String password, boolean waitFor){
String res = "";
try {
ConfigStore configStore = ConfigStore.getInstance();
String driveLetter = configStore.getDriveFor514();
String execPS = "powershell -command \"& '" + driveLetter + "PSScripts/DownloadSslHtml.ps1' -url '"
+ url + "' -outputFile '" + outputFileName + "' -PFXPath '"
+ certName + "' -PFXPassword '" + password + "'\"";
System.out.println("execPS:" + execPS);
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(execPS);
if(waitFor) {
try {
int resultCode = proc.waitFor();
if (resultCode == 0) {
System.out.println("Script run without errors!");
}
} catch (InterruptedException ex) {
Logger.getLogger(WebManagement.class.getName()).log(Level.SEVERE, null, ex);
}
}
//from experience we need to put a pause to let it run
try {
TimeUnit.MILLISECONDS.sleep(1600);
} catch (InterruptedException ex) {
Logger.getLogger(WebManagement.class.getName()).log(Level.SEVERE, null, ex);
}
String line;
InputStream errStream = proc.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(errStream));
int countError = 0;
String strError = "";
while ((line = reader.readLine()) != null) {
countError++;
if(countError<=2){
strError = strError+line;
}
}
proc.getOutputStream().close();
if(countError== 0){
res = "200";
} else {
res = strError;
}
} catch (IOException ex) {
Logger.getLogger(WebManagement.class.getName()).log(Level.SEVERE, null, ex);
}
return res;
}

发生的情况如下:程序在 95% 的时间内继续执行 PowerShell 脚本,并且每次调用都成功下载文件。但是,大约 5% 的时间,我收到此错误消息:

& : File P:\PSScripts\DownloadSslHtml.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.



这对我来说没有多大意义。我有权在我的机器上运行 PS 脚本,如果我从 PS GUI 手动运行脚本,它运行良好。这是一个间歇性问题,它似乎只是偶尔发生,而且从来没有持续发生过。

我想知道这可能是 Active Directory 问题还是系统无法检索策略的网络问题?只是一个想法,我真的不知道为什么我会收到这个错误。想法?我在 PS 命令参数中添加了“-ExecutionPolicy Bypass”,所以我想我们会看看这是否有帮助。

最佳答案

如果使用命令:

Get-ExecutionPolicy -List

您会看到有多个级别的范围,每个级别都可以有自己的执行策略,虽然我不确定为什么您的结果如此不一致,但我怀疑这可能与您的 CurrentUser 策略未定义有关。尝试运行命令
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass

关于java - 从 Java 调用 PowerShell 时偶尔出现 Windows 权限错误 - "running scripts disabled",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60007791/

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