# 检查是否以管理员权限运行
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Start-Process powershell.exe -Verb runAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
exit
}
# 设置 ActiveMQ 下载 URL 和安装路径
$activeMQDownloadUrl = "https://archive.apache.org/dist/activemq/5.16.3/apache-activemq-5.16.3-bin.zip"
$installPath = "C:\activemq"
# 检查安装路径是否存在,如果不存在则创建
if (-not (Test-Path $installPath)) {
New-Item -ItemType Directory -Path $installPath | Out-Null
}
# 检查下载文件是否存在
$downloadPath = Join-Path $installPath "activemq.zip"
if (-not (Test-Path $downloadPath)) {
# 下载 ActiveMQ
Write-Host "正在下载 ActiveMQ..."
Invoke-WebRequest -Uri $activeMQDownloadUrl -OutFile $downloadPath
} else {
Write-Host "ActiveMQ 安装文件已存在,无需下载。"
}
# 检查解压后的目录是否存在
$activeMQDir = Join-Path $installPath (Get-ChildItem $installPath -Directory | Where-Object { $_.Name -like "apache-activemq-*" })
if (-not $activeMQDir) {
# 解压 ActiveMQ
Write-Host "正在解压 ActiveMQ..."
Expand-Archive -Path $downloadPath -DestinationPath $installPath
} else {
Write-Host "ActiveMQ 已解压,无需重复解压。"
}
# 设置 ActiveMQ 环境变量
$activeMQBinPath = $activeMQDir + "\bin\win64"
Write-Host "ActiveMQ bin 路径为:$activeMQDir"
Write-Host "ActiveMQ bin 路径为:$activeMQBinPath"
if (-not (Test-Path $activeMQBinPath)) {
Write-Host "ActiveMQ bin 目录不存在。请检查安装过程。"
exit
}
# 确认 activemq.bat 文件存在
$activeMQBatPath = Join-Path $activeMQBinPath "activemq.bat"
if (-not (Test-Path $activeMQBatPath)) {
Write-Host "activemq.bat 文件不存在。请检查安装过程。"
exit
}
# 自动下载并安装 NSSM
$nssmDownloadUrl = "https://nssm.cc/release/nssm-2.24.zip"
$nssmZipPath = Join-Path $installPath "nssm.zip"
if (-not (Test-Path $nssmZipPath)) {
Write-Host "正在下载 NSSM..."
Invoke-WebRequest -Uri $nssmDownloadUrl -OutFile $nssmZipPath
} else {
Write-Host "NSSM 安装文件已存在,无需下载。"
}
# 检查 NSSM 解压文件夹是否存在
$nssmExtractPath = Join-Path $installPath (Get-ChildItem $installPath -Directory | Where-Object { $_.Name -like "nssm-*" })
if (-not (Test-Path $nssmExtractPath)) {
# 解压 NSSM
Write-Host "正在解压 NSSM..."
Expand-Archive -Path $nssmZipPath -DestinationPath $installPath
# 确认解压后的文件夹存在
if (-not (Test-Path $nssmExtractPath)) {
Write-Host "NSSM 解压失败。请检查安装过程。"
exit
} else {
Write-Host "NSSM 解压到路径:$nssmExtractPath"
}
} else {
Write-Host "NSSM 已解压,无需重复解压。"
}
# 更新 nssm 路径查找
$nssmPath = Join-Path $nssmExtractPath "win64\nssm.exe"
if (-not (Test-Path $nssmPath)) {
Write-Host "NSSM 文件未找到。请检查安装过程。"
exit
}
# 尝试安装为系统服务
Write-Host "尝试将 ActiveMQ 安装为系统服务..."
Write-Host "111111"
$serviceName = "ActiveMQService"
# 检查服务是否存在,如果存在则先卸载(不弹出确认对话框)
if (Get-Service | Where-Object { $_.Name -eq $serviceName }) {
try {
& $nssmPath stop $serviceName
} catch {
Write-Host "停止服务时出现错误:$_"
}
Write-Host "正在卸载服务..."
try {
sc.exe delete $serviceName
} catch {
Write-Host "卸载服务时出现错误:$_"
}
}
# Read-Host "Press any key to continue..."
# 安装服务
if (-not (Get-Service | Where-Object { $_.Name -eq $serviceName })) {
Write-Host "222222"
Write-Host "ActiveMQ bin 路径为:$nssmPath"
Write-Host "ActiveMQ bin 路径为:$serviceName"
Write-Host "ActiveMQ bin 路径为:$activeMQBatPath"
& $nssmPath install $serviceName $activeMQBatPath
& $nssmPath set $serviceName Start SERVICE_AUTO_START
} else {
Write-Host "ActiveMQ 服务已存在。"
}
# 启动服务
Write-Host "正在启动 ActiveMQ 服务..."
$service = Get-Service -Name $serviceName
if ($service) {
if ($service.Status -ne "Running") {
try {
$service.Start()
Write-Host "ActiveMQ 服务正在启动..."
} catch {
Write-Host "启动 ActiveMQ 服务时出现错误:$_。请检查服务配置和权限。"
}
} else {
Write-Host "ActiveMQ 服务已在运行,无需再次启动。"
}
}
Write-Host "ActiveMQ 安装为系统服务并启动处理完成。"

