简体   繁体   中英

Invoke powershell script from java

I am trying to invoke some powercli script from Java and it fails.

I am working with VMWare esx

This is my Java code:

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):

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.

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

ipmo hyperv

for hypervisor vmware esxi you should see down link vmware esxi via powershell and for add

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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