【问题标题】:Running PowerShell from Java loading 3rd party Modules - ResourceUnavailable: FileNotFoundException从 Java 加载第 3 方模块运行 PowerShell - ResourceUnavailable: FileNotFoundException
【发布时间】:2015-06-11 18:00:20
【问题描述】:

我正在移植一个利用 3rd 方模块 (NetCmdlets) 的 PS4 脚本,并且在 Powershell 窗口中运行良好,但在从 Java 程序运行时似乎忘记了如何加载 3rd 方模块。

我很想知道任何人对此的潜在原因和解决方案的想法......

这里是调用 Java PGM:

package ImagineOne.PSJava2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ExecuteCommand {

 /**
  * @param args
  * @throws IOException 
  */
 public static void main(String[] args) throws IOException {

  // This is the command
 //  String command = "powershell.exe  $PSVersionTable.PSVersion | Get-Member";
 // String command = "powershell.exe  C:\\Users\\versaggi\\Desktop\\PowerCLI\\DevScripts\\CANES_VMWare_Extraction_Functions.ps1";
 String command = "powershell.exe  C:\\Users\\versaggi\\Desktop\\PowerCLI\\DevScripts\\CANES_IBM_RackSwitch_Extraction_Functions.ps1";

  Process powerShellProcess = Runtime.getRuntime().exec(command);
  powerShellProcess.getOutputStream().close();
  String line;

  System.out.println("Output:");
  BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));

  while ((line = stdout.readLine()) != null) {
   System.out.println(line);
  }

  stdout.close();
  System.out.println("Error:");
  BufferedReader stderr = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));

  while ((line = stderr.readLine()) != null) {
   System.out.println(line);
  }

  stderr.close();
  System.out.println("Done");

 }

} //End Class

这是输出:

Output:
** NetCmdlets Modules Loaded ** 
** Disconnected from RackSwitch ** 
Error:
Import-Module : **The specified module 'C:\Windows\System32\WindowsPowerShell\v1.
0\Modules\NetCmdlets\NetCmdlets.psd1' was not loaded because no valid module 
file was found in any module directory**.At C:\Users\versaggi\Desktop\PowerCLI\De
vScripts\CANES_IBM_RackSwitch_Extraction_Functions.ps1:732 char:1
+ Import-Module 
C:\Windows\System32\WindowsPowerShell\v1.0\Modules\NetCmdlets\NetC ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~
    + CategoryInfo          : ResourceUnavailable: (C:\Windows\Syst...NetCmdle 
   ts.psd1:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm 
   ands.ImportModuleCommand

Done

这是 PowerShell 脚本:(仅应加载模块作为测试 [概念证明])

Import-Module C:\Windows\System32\WindowsPowerShell\v1.0\Modules\NetCmdlets\NetCmdlets.psd1

write-host "** NetCmdlets Modules Loaded ** "

我仔细阅读过的一些研究与开发:

http://technirman.blogspot.com/2014/06/invoke-powershell-commands-through-java.html

https://blogs.oracle.com/vaibhav/entry/not_as_easy_as_we

https://social.technet.microsoft.com/Forums/windowsserver/en-US/d32537bd-0aef-440e-8760-6b3085390c37/executing-powershell-script-via-java?forum=winserverpowershell

【问题讨论】:

  • 我刚刚修复了 Java/Powershell 互操作性问题。凭直觉(在深入研究了 VMware 对他们从未遇到问题的模块所做的事情之后),我推测 NetCmdlet 已默认将其模块安装在 Windows 操作系统中的受保护空间(参见下面的目录)中。移动到未受保护的用户空间后(参见下面的目录),它工作得很好。我什至做了一些压力测试,结果很好。受保护的 Win 操作系统空间:$PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules) 不受保护的空间:C:\Users\versaggi\Desktop\PowerCLI\windowspowershell\modules

标签: java powershell powershell-module


【解决方案1】:

我刚刚修复了 Java/Powershell 互操作性问题。凭直觉(在深入了解 VMware 对他们从未遇到问题的模块所做的事情之后)我推测 NetCmdlets 已将他们的模块安装在 受保护空间(参见下面的目录)。在移动到 未受保护的用户空间(参见下面的目录)后,它工作得很好。我什至做了一些压力测试,结果很好。

受保护的 Win OS 空间:

$PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules) 

未受保护的空间:

C:\Users\versaggi\Desktop\PowerCLI\windowspowershell\modules

根据有关该主题的 Windows 文档:

在 PSModulePath 中安装模块

尽可能将所有模块安装在 PSModulePath 环境变量中列出的路径中,或将模块路径添加到 PSModulePath 环境变量值中。 PSModulePath 环境变量 ($env:PSModulePath) 包含 Windows PowerShell 模块的位置。 Cmdlet 依赖此环境变量的值来查找模块。

默认情况下,PSModulePath 环境变量值包含以下系统和用户模块目录,但您可以添加和编辑该值。

$PSHome\Modules (%Windir%\System32\WindowsPowerShell\v1.0\Modules)

警告注意:

此位置是为 Windows 附带的模块保留的。 不要将模块安装到此位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多