我们可以使用 Powershell 在 azure VM 上远程安装 java 吗?

Can we install java on azure VM remotely using Powershell?

我正尝试在 VSTS 管道中使用 PowerShell 运行 azure VM 上的 Apache jMeter。问题是 azure VM 是新创建的,默认情况下没有安装 JDK。由于 jMeter 需要 'java' 所以它会抛出以下错误。

C:\apache-jmeter-5.2.1\bin>jmeter -n -t C:\User-search.jmx -l C:\
Not able to find Java executable or version. Please check your Java installation.
errorlevel=2

我需要在 运行 安装 jMeter 之前安装 java。有什么方法可以使用 Powershell 远程安装 java?

或者如果有任何 windows 预装 java 可用的 azure VM 映像?

您可以使用 Powershell Remoting, the easiest way of installing Java Runtime is using Chocolatey package manager 在任何机器上执行任意命令,这应该很简单:

choco install javaruntime -y

参考文献:

首先与 VM 建立会话,然后 运行 下面的脚本。

#Download and silent install JDK

#Working directory path
Write-Host "Creating temp working directory..."
$workd = "c:\temp"

#Check if work directory exists if not create it
If (!(Test-Path -Path $workd -PathType Container))
{ 
    New-Item -Path $workd  -ItemType directory 
}

#Create config file for silent install
Write-Host "Creating config file for silent install..."
$text = '
INSTALL_SILENT=Enable
AUTO_UPDATE=Enable
SPONSORS=Disable
REMOVEOUTOFDATEJRES=1
'
$text | Set-Content "$workd\jdkinstall.cfg"

#Download executable file
Write-Host "Download JDK file to temp directory..."
$source = "https://download.oracle.com/otn-pub/java/jdk/13.0.2+8/d4173c853231432d94f001e99d882ca7/jdk-13.0.2_windows-x64_bin.exe"
$destination = "$workd\jdk-13.0.2_windows-x64_bin.exe"
$client = New-Object System.Net.WebClient
$cookie = "oraclelicense=accept-securebackup-cookie"
$client.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $cookie)
$client.DownloadFile($source, $destination)
Write-Host "Download JDK file completed !"

#Install silently
Write-Host "Trying to install JDK silently..."
Start-Process -FilePath "$workd\jdk-13.0.2_windows-x64_bin.exe" -ArgumentList INSTALLCFG="$workd\jdkinstall.cfg" -Wait
Write-Host "JDK installation completed successfully !"

#Remove the installer
Write-Host "Trying to remove JDK file from temp directory..."
rm -Force $workd\jdk*
Write-Host "JDK file deleted successfully !"