从Java运行PowerShell加载第三方模块ResourceUnavailable:FileNotFoundException
我正在移植一个PS4脚本,该脚本利用了第三方模块(NetCmdlet),在Powershell窗口中运行得非常好,但似乎不知道从Java程序运行时如何加载第三方模块
我对任何人关于这个问题的潜在原因和解决方案的想法都感兴趣
以下是正在调用的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 ** "
一些R&;D我仔细阅读并考虑:
http://technirman.blogspot.com/2014/06/invoke-powershell-commands-through-java.html
# 1 楼答案
我刚刚解决了Java/Powershell互操作性问题。凭直觉(在深入研究了VMware与他们的模块之间从未出现过问题的内容后),我猜测NetCmdlet默认情况下已将其模块安装在Windows操作系统中受保护的空间(请参阅下面的目录)。在移动到一个未受保护的用户空间后(见下面的目录),它工作得很好。我甚至做了一些压力测试,结果很好
受保护的Win OS空间:
未受保护的空间:
根据有关该主题的Windows文档:
在PSModulePath中安装模块
尽可能将所有模块安装在PSModulePath环境变量中列出的路径中,或将模块路径添加到PSModulePath环境变量值中。 PSModulePath环境变量($env:PSModulePath)包含Windows PowerShell模块的位置cmdlet依赖于此环境变量的值来查找模块
默认情况下,PSModulePath环境变量值包含以下系统和用户模块目录,但您可以添加和编辑该值
$PSHome\Modules(%Windir%\System32\WindowsPowerShell\v1.0\Modules)
警告注意:
此位置是为Windows附带的模块保留的不要将模块安装到此位置