1

我正在移植一个利用 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

4

1 回答 1

0

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

受保护的 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 附带的模块保留的。不要将模块安装到此位置。

于 2015-06-12T03:04:34.150 回答