Question

I am trying to write a script in PowerShell that will use the invoke command with credentials on a remote machine to execute code that will return a string containing the Java version on that machine. Java is not installed, sometimes it is just dropped in the server as a JDK folder and the application is pointed to that location, so unfortunately I cannot use the registry to find the Java version. The only idea I came up with is to call from within the invoke command something along the lines of:

& "path_to_java.exe" "-version"

Or even something more complicated and then use a regular expression to find the version:

start-process $java -ArgumentList "-version" -NoNewWindow -Wait -RedirectStandardError "$path\tempoutput.txt"
$javaversionoutput = gc "$path\tempoutput.txt"
del "$path\tempoutput.txt"

However, on some servers I keep running into an issue where it is complaining about not having enough heap space to run that Java version command (through the invoke command on that remote server).

Error occurred during initialization of VM
Could not reserve enough space for object heap
D:\Java\jdk1.6.0_39\bin\java.exe
Could not create the Java virtual machine.
+ CategoryInfo          : NotSpecified: (Could not creat...irtual machine.:String) [],     RemoteException
+ FullyQualifiedErrorId : NativeCommandError
+ PSComputerName        : <server>

From my research it has to do with the MaxMemoryPerShellMB that is set for a remote PowerShell session. On some servers it is set to 150, on some it's 1024. So sometimes I can successfully get the java -version to run from the PowerShell remote session, and sometimes I cannot, because there is too little memory. If I set it on the remote machine by logging in and doing it manually:

winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="1000"}

It starts to work. But I cannot go to each server each time and set it manually. If I try to set MaxMemoryPerShellMB through an invoke command, it says I don't have access, even though I can use the same account to change MaxMemoryPerShellMB on the server if I login and do it directly.

I feel like I have run out of solutions and was hoping to find some help here. Sorry if there has been a topic for this, I only found three and none of them answer my question. Below are the references if you guys are interested. There really is nothing I could find :P

Was it helpful?

Solution

Honestly, I would just check the version of the file java.exe, something like

$Machines = Get-Content C:\Lists\Servers.txt

$Machines | for-each{

$java = gci '\\$_\c$\Program Files (x86)\Java\jre7\bin\java.exe'

$java.VersionInfo

And you get a VersionInfo object back , something like

 ProductVersion   FileVersion      FileName
 --------------   -----------      --------
 7.0.510.13       7.0.510.13       \\$_\c$\Program Files (x86)\Java\jre7\bin\java.exe

Or probably $null if path not found (of course you can check for that first with test-path but whatever). Good thing is that you can do real operations with the VersionInfo object if you so choosed, one of the awesome benefits of powershell. And Java sucks.

OTHER TIPS

You can use this one liner for getting the latest java version installed. If u have older versions installed too it only returns the latest:

[system.diagnostics.fileversioninfo]::GetVersionInfo(('C:\Program Files (x86)\Java\'+([regex]::matches((&"java.exe" -version 2>&1)[0].ToString(), '(?<=\").+?(?=\")').value.Insert(0,"jre")) + "\bin\java.exe"))

This should work with no problem

gc "D:\serverlists\serverlist.txt" | foreach {  
[system.diagnostics.fileversioninfo]::GetVersionInfo("\\$_\c$\Program Files\Java\jre6\bin\java.exe"); }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top