tuj*_*tuj 7 java windows powershell
我有一个 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'"
Run Code Online (Sandbox Code Playgroud)
有问题的 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
Run Code Online (Sandbox Code Playgroud)
如您所见,这非常简单。
我的 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;
}
Run Code Online (Sandbox Code Playgroud)
发生的情况如下:程序在 95% 的时间内继续执行 PowerShell 脚本,并且每次调用都成功下载文件。但是,大约 5% 的时间,我收到此错误消息:
& : 文件 P:\PSScripts\DownloadSslHtml.ps1 无法加载,因为在此系统上禁用了运行脚本。有关详细信息,请参阅 https://go.microsoft.com/fwlink/?LinkID=135170 上的 about_Execution_Policies。
这对我来说没有多大意义。我有权在我的机器上运行 PS 脚本,如果我从 PS GUI 手动运行脚本,它运行良好。这是一个间歇性问题,它似乎只是偶尔发生,而且从来没有持续发生过。
我想知道这可能是 Active Directory 问题还是系统无法检索策略的网络问题?只是一个想法,我真的不知道为什么我会收到这个错误。想法?我在 PS 命令参数中添加了“-ExecutionPolicy Bypass”,所以我想我们会看看这是否有帮助。
小智 1
如果您使用命令:
Get-ExecutionPolicy -List
Run Code Online (Sandbox Code Playgroud)
您将看到有多个级别的范围,每个级别都有自己的执行策略,虽然我不确定为什么您的结果如此不一致,但我怀疑这可能与您的 CurrentUser 策略未定义有关。尝试运行命令
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
157 次 |
| 最近记录: |