简体   繁体   English

从Java调用Powershell脚本

[英]Invoke powershell script from java

I am trying to invoke some powercli script from Java and it fails. 我试图从Java调用一些powercli脚本,但它失败了。

I am working with VMWare esx 我正在使用VMWare esx

This is my Java code: 这是我的Java代码:

String command = "cmd.exe C:\\Users\\mayan\\Desktop\\scriptPS.ps1";
Process powerShellProcess = Runtime.getRuntime().exec(command);
powerShellProcess.getOutputStream().close();

This is my script (.ps1 file): 这是我的脚本(.ps1文件):

C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1
Get-VM -Name dev-maya | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$false           
Get-VM -Name dev-maya | Get-NetworkAdapter | Set-NetworkAdapter -Connected:$true

Java invoke the first command and ignore the rest. Java调用第一个命令,而忽略其余命令。

Does anyone have idea how to fix this problem? 有谁知道如何解决这个问题?

please check your module first 请先检查您的模块

get-module 

if you do not have add Hyper-v module then you should have Hyper-v module for use get-vm 如果没有添加Hyper-v模块,则应该具有Hyper-v模块以使用get-vm

ipmo hyperv

for hypervisor vmware esxi you should see down link vmware esxi via powershell and for add 对于hypervisor vmware esxi,您应该通过powershell向下查看vmware esxi链接,并进行添加

ipmo CimCmdlets

Try replacing 尝试更换

C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1

with

Add-PSSnapin vmware.vimautomation.core

You'll also need to run connect-viserver as implied in the other answer. 您还需要按照其他答案中的暗示运行connect-viserver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM