Get Java Version from PowerShell Remote Server

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 version of Java on that computer. Java is not installed, sometimes it is simply deleted on the server as a JDK folder , and the application points to this location, so, unfortunately, I can not use the registry to find the version of Java. The only idea I came up with is to invoke the invoke command from the inside with something like strings:

& "path_to_java.exe" "-version"

Or even something more complex, and then use a regular expression to search for a 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 run into a problem when it complains that it does not have enough heaps to run this Java version command (via the invoke command on this 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, this is related to MaxMemoryPerShellMB, which is configured for a remote PowerShell session. On some servers, it is 150, on some - 1024. Therefore, sometimes I can successfully launch java -versionPowerShell to start from a remote session, and sometimes I can not, because there is too little memory. If I installed it on a remote computer by logging in and doing it manually:

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

. . MaxMemoryPerShellMB invoke, , , MaxMemoryPerShellMB , .

, , - . , , , . , . : P

+3
4

, java.exe, -

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

$Machines | for-each{

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

$java.VersionInfo

VersionInfo , -

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

, , $null, (, , ). , VersionInfo, , powershell. Java .

+3

gc "D:\serverlists\serverlist.txt" | foreach {  
[system.diagnostics.fileversioninfo]::GetVersionInfo("\\$_\c$\Program Files\Java\jre6\bin\java.exe"); }
0

java. , :

[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"))
0

[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"))

Brilliant BUT works, how can I use it against a list of 300 servers?

0
source

All Articles