2

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?

maya
  • 53
  • 6

2 Answers2

0

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
Soheil
  • 837
  • 8
  • 17
  • I get this error: ipmo : The specified module 'hyperv' was not loaded because no valid module file was found in any module directory. At line:1 char:1 + ipmo hyperv + ~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (hyperv:String) [Import-Mod ule], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Comm ands.ImportModuleCommand – maya Mar 22 '15 at 12:58
  • see this [link](https://technet.microsoft.com/en-us/library/hh846767.aspx) and this like for module [link](https://pshyperv.codeplex.com/) – Soheil Mar 22 '15 at 13:03
  • sorry, ive been a little confused. I am working with VMWare esx and that is why it did not worked well. i want to run this on powercli. i run this command in cmd: C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1 to open the prompt and then the commands Get-VM are working. but how it is possible for me to do it in Java? – maya Mar 22 '15 at 15:51
  • @maya oops u should tell that is vmware esxi now see [link](https://www.vmware.com/support/developer/windowstoolkit/wintk40u1/html/) and you should get module and import with `ipmo CimCmdlets` – Soheil Mar 22 '15 at 15:59
  • It didn't solve the problem, but thanks. The problem is to invoke powercli commands from java when i'm using VMWare esx. Any other suggestions? – maya Mar 29 '15 at 12:02
  • your first question about powershell not powercli sorry check [java](http://vijava.sourceforge.net/) – Soheil Mar 29 '15 at 12:10
0

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.

noam
  • 1,914
  • 2
  • 20
  • 26
  • noam, I have replaced the command according to your suggestion and now the first command (Connect-VIServer) is done, but the Get-VM commands do not execute, the program runs and nothing happens. – maya Mar 29 '15 at 12:27
  • It works. I also add to the script the command $ConfirmPreference="none" that cancel the "are you sure?" question. Thanks a lot! – maya Mar 29 '15 at 13:12