我正在使用Java来调用PowerShell命令,然后捕获输出。
我遇到了一个问题,一个简单的PowerShell命令将返回输出,但另一个不会,我试图理解为什么。
这是逻辑(更新为包括检查错误输出流)
public class Example {
private void myMethod(String command) throws IOException {
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = "";
String line;
while((line = bufferedReader.readLine()) != null){
output += (line + "\n");
}
System.out.println((output.isEmpty() ? "No output was received!!!" : output));
// Also iterate through the error stream to see if it caught anything.
BufferedReader errorBufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String errorOutput = "";
while((line = errorBufferedReader.readLine()) != null){
errorOutput += (line + "\n");
}
System.out.println((errorOutput.isEmpty() ? "Nothing in the error output stream." : errorOutput));
}
public static void main(String[] args) throws IOException {
new Example().myMethod("powershell -Command \"$PSVersionTable\"");
new Example().myMethod("powershell -Command \"Get-Module -Name VMware* -ListAvailable\"");
}
}
这是输出
Name Value
---- -----
PSVersion 5.0.10586.117
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.10586.117
CLRVersion 4.0.30319.18444
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Nothing in the error output stream.
No output was received!!!
Nothing in the error output stream.
所以在Main()
中执行的第一个命令返回输出,但第二个命令没有返回输出,我不明白为什么。
我可以通过命令提示符手动运行第二个命令,它肯定有效。
关于为什么在我以编程方式执行它时无法检索第二个命令的输出的任何想法?
更新
当手动运行第二个命令时,这是生成的输出。(以及我希望我的Java代码捕获的内容)
U:\>powershell -Command "Get-Module -Name VMware* -ListAvailable"
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Binary 6.5.1.6... VMware.DeployAutomation {Add-DeployRule, Add-ProxyServer, Add-ScriptBundle, Copy-DeployRule...}
Binary 6.5.1.6... VMware.ImageBuilder {Add-EsxSoftwareDepot, Add-EsxSoftwarePackage, Compare-EsxImageProfile, Export-EsxImageProfile...}
Manifest 6.5.4.7... VMware.PowerCLI
Binary 6.5.4.6... VMware.VimAutomation.Cis.Core {Connect-CisServer, Disconnect-CisServer, Get-CisService}
Binary 6.5.1.5... VMware.VimAutomation.Cloud {Add-CIDatastore, Connect-CIServer, Disconnect-CIServer, Get-Catalog...}
Manifest 6.5.4.6... VMware.VimAutomation.Common
Binary 6.5.2.6... VMware.VimAutomation.Core {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost, Add-VMHostNtpServer...}
Binary 6.5.4.7... VMware.VimAutomation.HA Get-DrmInfo
Binary 7.1.0.5... VMware.VimAutomation.HorizonView {Connect-HVServer, Disconnect-HVServer}
Binary 6.5.1.5... VMware.VimAutomation.License Get-LicenseDataManager
Binary 2.0.0.6... VMware.VimAutomation.Nsxt {Connect-NsxtServer, Disconnect-NsxtServer, Get-NsxtService}
Binary 6.5.1.5... VMware.VimAutomation.PCloud {Connect-PIServer, Disconnect-PIServer, Get-PIComputeInstance, Get-PIDatacenter}
Manifest 1.0.0.5... VMware.VimAutomation.Sdk {Get-PSVersion, Get-InstallPath}
Binary 6.5.1.5... VMware.VimAutomation.Srm {Connect-SrmServer, Disconnect-SrmServer}
Binary 6.5.4.7... VMware.VimAutomation.Storage {Add-KeyManagementServer, Copy-VDisk, Export-SpbmStoragePolicy, Get-KeyManagementServer...}
Script 1.1 VMware.VimAutomation.StorageUtility Update-VmfsDatastore
Binary 6.5.1.5... VMware.VimAutomation.Vds {Add-VDSwitchPhysicalNetworkAdapter, Add-VDSwitchVMHost, Export-VDPortGroup, Export-VDSwitch...}
Binary 6.5.4.7... VMware.VimAutomation.Vmc {Connect-Vmc, Disconnect-Vmc, Get-VmcService, Connect-VmcServer...}
Binary 6.5.1.5... VMware.VimAutomation.vROps {Connect-OMServer, Disconnect-OMServer, Get-OMAlert, Get-OMAlertDefinition...}
Binary 6.5.1.5... VMware.VumAutomation {Add-EntityBaseline, Copy-Patch, Get-Baseline, Get-Compliance...}
所以我想出了这里发生了什么。
快速回顾一下问题::
这种情况发生的原因::
通过32位Powershell安装VMware模块后,我的Java程序(在32位上下文中运行)开始按预期工作。