【发布时间】:2021-10-04 08:23:45
【问题描述】:
我有一个 PS 脚本,我需要在用户无需打开 powershell 的情况下运行它——我认为最简单的事情是编写一个应用程序或 .bat 文件来运行 PS 脚本。但是我无法让脚本使用任何其他语言运行。
我尝试过使用 java,但它不运行脚本,C# 或批处理命令也不运行。我会附上下面的批次,因为这似乎是更简单的方法,我只是不确定为什么它不起作用 - 没有错误消息它只是不起作用。
编辑** Powershell 脚本如下,可以运行。我需要一个运行脚本的第三方应用程序。
$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("Select a Folder that you want to Analyze
Please be aware the larger the folder size the Longer it will take to Generate")
@echo off
powershell.exe -ExecutionPolicy Unrestricted -Command "'I:\TestScript\FolderInformation.ps1'"
TIMEOUT /T 10
我只需要能够通过为不知道使用 ISE 或任何其他 PS 应用程序的用户启动单个应用程序来运行脚本。
到目前为止,我使用的任何应用程序或脚本都不能正常工作,只能使用 ISE 来运行它。
*****Java 代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class PowerShellJavaDemo {
public static void main(String[] args) throws IOException {
String line;
String command = "powershell.exe & \"I:\\testingscript\"";
Process powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();
BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
System.out.println("Output: " + line);
}
stdout.close();
BufferedReader stderr = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));
while ((line = stderr.readLine()) != null) {
System.out.println("Output: " + line);
}
stderr.close();
}
}
【问题讨论】:
-
而here列出的选项并不能解决问题?
-
我建议您阅读
-file和-command选项的作用。 -
不是我自己不能运行 PS 脚本。脚本从 ISE 运行 - 我需要能够通过不同的程序运行它
-
哦,所以你想写一个程序来启动这个PS1。明白了,你使用了什么 Java 代码?也许这对我们来说是一个起点
-
为什么要使用其他脚本解释器?这真的没有必要。可以创建属性 Target 包含
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -NoLogo -file "I:\TestScript\FolderInformation.ps1"的快捷方式文件(.lnk文件)并自定义其他属性,例如 Start in 和 Comment(将鼠标指针悬停在快捷方式上时显示为工具提示)。然后发布快捷方式文件。
标签: java powershell batch-file