首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行PowerShell文件时表示该文件没有“.ps1”扩展名

运行PowerShell文件时表示该文件没有“.ps1”扩展名
EN

Stack Overflow用户
提问于 2017-05-20 15:07:29
回答 1查看 1.1K关注 0票数 2

我正在努力弄清楚如何在Java内部运行PowerShell脚本。请记住,我对Java非常陌生,因此可能有一种更好的方法。

目前,我在Fedora 25工作站上安装了PowerShell。

正如解释过的这里,首先安装Fedora .NET核心包:

代码语言:javascript
复制
sudo dnf config-manager --add-repo https://copr.fedorainfracloud.org/coprs/nmilosev/dotnet-sig/repo/fedora-25/nmilosev-dotnet-sig-fedora-25.repo
sudo dnf update
sudo dnf install dotnetcore

然后下载并安装CentOS 7的RPM

代码语言:javascript
复制
sudo dnf install Downloads/powershell-6.0.0_beta.1-1.el7.centos.x86_64.rpm
powershell

然后,我使用这个帖子的代码运行一个"Hello“ps1文件:

代码语言:javascript
复制
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PowerShellCommand {
    public static void main(String[] args) throws IOException {
        String command = "powershell $PSVersionTable.PSVersion";
        command = "powershell -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -File \"/home/b/Downloads/MyScript.ps1\"";

        Process powerShellProcess = Runtime.getRuntime().exec(command);
        powerShellProcess.getOutputStream().close();
        String line;
        System.out.println("Standard Output:");
        BufferedReader stdout = new BufferedReader(new InputStreamReader(
                powerShellProcess.getInputStream()));
        while ((line = stdout.readLine()) != null) {
            System.out.println(line);
        }
        stdout.close();
        System.out.println("Standard Error:");
        BufferedReader stderr = new BufferedReader(new InputStreamReader(
                powerShellProcess.getErrorStream()));
        while ((line = stderr.readLine()) != null) {
            System.out.println(line);
        }
        stderr.close();
        System.out.println("Done");
    }
}

在尝试运行.ps1文件时遇到的错误是:

由于文件没有‘-File’扩展名,所以处理.ps1‘“/home/b/Download/MyScript.ps1”失败。指定有效的Windows PowerShell脚本文件名,然后再试一次。

在试图从Java代码中运行此操作时,我确实获得了变量的正确输出:

代码语言:javascript
复制
String command = "powershell $PSVersionTable.PSVersion";

在Gnome终端中,从bash shell运行以下代码时,运行起来也很好,脚本通过说"Hello world“来正确执行:

代码语言:javascript
复制
powershell -ExecutionPolicy RemoteSigned -NoProfile -NonInteractive -File "/home/b/Downloads/MyScript.ps1"

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-07 22:55:55

Note

  • 自问题发布以来,pwsh,核心可执行文件的名称已从powershell更改为powershell,反映在下面的解决方案中。
  • 问题中的特定脚本-- /home/b/Downloads/MyScript.ps1中的文件路径--并不严格要求引用(包含在"..."中),但是是一个普遍健壮的解决方案--应该使用引用--如下所示。

通过将命令构造为一个" 单字符串,将嵌入的实例保留为文本E 234,这就是错误消息反映的内容:

-File '"/home/b/Downloads/MyScript.ps1"' .

请注意,'...'中引用的值是如何包含"..."的--而且一个字面上包含"字符的路径显然不存在,而且由于以"结尾--没有.ps1扩展。

Runtime.exec()所做的是通过StringTokenizer来执行只使用空格的字符串标记。因此,结果数组保留嵌入引号,并且也不会将带有嵌入式空格的引用标记识别为单个标记。

为了解决这个问题,您有两个选择:,:

  • (a) -Command使用和而不是-File来调用脚本,在这种情况下,PowerShell使用另一轮解析,因此嵌入式"实例被认为具有语法功能。 String命令= "pwsh -NoProfile -Command &\“/home/b/下载/myScript.ps1\”;
  • (b) Preferably,将命令行的令牌作为字符串数组传递: String[]命令=新String[] { "pwsh“、"-NoProfile”、"-File“、”/home/b/下载/MyScript.ps1“};

注意,对于(a),如果在命令字符串中包含要传递给脚本的参数,这些参数也会被PowerShell解析,这意味着您可能也需要对它们进行嵌入引用。

请注意,由于脚本路径参数是作为自己的数组元素指定的,因此在(b)中不需要对脚本路径参数进行嵌入引用。通过使用PowerShell的-File参数,传递的所有参数都被视为文本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44087451

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档