0

I want to write a java code which will use powershell command to connect the remote system and execute java -version command in remote system. Can someone please help me on this.

Thanks in advance :)

1
  • runtimeProcess = Runtime.getRuntime().exec(new String[] {"powershell.exe", "/c","$pw = convertto-securestring -AsPlainText -Force -String "+password+ "; $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist \""+domain+"\\"+user+"\""+",$pw"+ "; $session = new-pssession -computername \""+host+"\" -credential $cred"+ "; Enter-PSSession -ComputerName \""+host+"\" -Credential $cred ; java -version"}) I am using this but it is showing java version of local system. Commented Jul 9, 2014 at 7:28

1 Answer 1

1

Have you attempted to use Invoke-Command instead of Enter-PSSession?

After some testing of the PowerShell and tweaking it to get the return values I had to end up with the following code.

$pw = convertto-securestring -AsPlainText -Force -String "Password"
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "Domain\User",$pw
$session = New-PSSession -ComputerName Computer -Credential $cred
Invoke-Command -Session $session -ScriptBlock { return "$(&java -Xms256m -Xmx256m -version 2>&1)";}

I had to pass memory configuration via Invoke-Command because remote java via WinRM seems to try and fill more memory than is available. I also had to redirect the error output (2>&1) as the return from the -version call is picked up as an Error by PowerShell.

I guess your Java would read something like this:

runtimeProcess = Runtime.getRuntime().exec(new String[] {"powershell.exe", "/c","$pw = convertto-securestring -AsPlainText -Force -String \""+password+"\"; $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist \""+domain+"\\"+user+"\""+",$pw"+ "; $session = new-pssession -computername \""+host+"\" -credential $cred"+ "; Invoke-Command -Session $session -ScriptBlock { return \"$(&java -Xms256m -Xmx256m -version 2>&1)\";}

To Set the Shell Memory:

Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 2048

To Set the Shell Memory Remotely once configuring your credentials as above:

Invoke-Command -Session $session -ScriptBlock { Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 2048 }

You could also attempt to run it with a lower -Xmx or -Xms value, though java may fail to load in lower memory environments.

runtimeProcess = Runtime.getRuntime().exec(new String[] {"powershell.exe", "/c","$pw = convertto-securestring -AsPlainText -Force -String \""+password+"\"; $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist \""+domain+"\\"+user+"\""+",$pw"+ "; $session = new-pssession -computername \""+host+"\" -credential $cred"+ "; Invoke-Command -Session $session -ScriptBlock { return \"$(&java -Xms16m -Xmx64m -version 2>&1)\";}

I hope this helps.

Cheers, Chris.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Chris, Thanks for your response. I am getting below error while executing this code: Error occurred during initialization of VM Could not reserve enough space for object heap
I mentioned this in some ways with the -Xms and -Xmx part, but you may also need to increase the memory size of the remote Powershell Shell. eg: Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024 or: Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 2048
You should be able to run this through an Authenticated Shell (like Invoke-Command) providing that you have Administrative credentials. You could try lowering the XmX and Xms values in order to get java to stop allocating too much heap for the Shell to handle. You could try -Xmx64m and -Xms64m also, and hope that it runs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.