从远程服务器的 PowerShell 获取 Java 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21863002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:07:45  来源:igfitidea点击:

Get Java version from PowerShell of remote server

javapowershellversionpowershell-remotinginvoke-command

提问by alexfvolk

I am trying to write a script in PowerShellthat 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 JDKfolder 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:

我正在尝试在PowerShell中编写一个脚本,该脚本将使用带有凭据的 invoke 命令在远程机器上执行代码,该代码将返回包含该机器上 Java 版本的字符串。Java 未安装,有时它只是作为JDK文件夹放入服务器中,并且应用程序指向该位置,因此不幸的是我无法使用注册表来查找 Java 版本。我想出的唯一想法是从 invoke 命令中调用以下内容:

& "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).

但是,在某些服务器上,我一直遇到一个问题,它抱怨没有足够的堆空间来运行该 Java 版本命令(通过该远程服务器上的 invoke 命令)。

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

根据我的研究,它与为远程 PowerShell 会话设置的 MaxMemoryPerShellMB 有关。在某些服务器上它设置为 150,在某些服务器上它设置为 1024。所以有时我可以成功地java -version从 PowerShell 远程会话中运行,有时我不能,因为内存太少。如果我通过登录并手动在远程机器上设置它:

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.

它开始工作。但是我不能每次都去每台服务器手动设置。如果我尝试通过调用命令设置 MaxMemoryPerShellMB,它会说我没有访问权限,即使我可以使用相同的帐户在服务器上更改 MaxMemoryPerShellMB,如果我直接登录并执行此操作。

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

我觉得我已经用完了解决方案,并希望在这里找到一些帮助。抱歉,如果有相关主题,我只找到了三个,但没有人回答我的问题。下面是大家感兴趣的参考。我真的找不到任何东西:P

采纳答案by MDMoore313

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

老实说,我只会检查文件的版本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

And you get a VersionInfoobjectback , something like

然后你得到一个VersionInfo对象,比如

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

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

或者可能$null如果找不到路径(当然,您可以先使用 test-path 进行检查,但无论如何)。好消息是,如果您愿意,您可以使用 VersionInfo 对象进行实际操作,这是 powershell 的一大优势。Java 很烂。

回答by NinjAmit

This should work with no problem

这应该没有问题

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

回答by Mathis Michel

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:

您可以使用这一衬垫来安装最新的 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"))