-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.ps1
More file actions
81 lines (69 loc) · 2.93 KB
/
install.ps1
File metadata and controls
81 lines (69 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Get server and key
param($server, $key, $tls)
# Check PowerShell version
if ($PSVersionTable.PSVersion.Major -lt 5) {
Write-Host "Require PS >= 5, your PSVersion:" $PSVersionTable.PSVersion.Major -BackgroundColor DarkGreen -ForegroundColor White
Write-Host "Refer to the community article and install manually! https://nyko.me/2020/12/13/nezha-windows-client.html" -BackgroundColor DarkRed -ForegroundColor Green
exit
}
# Set agent repo and fixed version
$agentrepo = "nezhahq/agent"
$agenttag = "v0.20.5"
# Determine file type based on architecture
if ([System.Environment]::Is64BitOperatingSystem) {
if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {
$file = "nezha-agent_windows_arm64.zip"
} else {
$file = "nezha-agent_windows_amd64.zip"
}
} else {
$file = "nezha-agent_windows_386.zip"
}
# Remove existing installation if present
if (Test-Path "C:\nezha\nezha-agent.exe") {
Write-Host "Nezha monitoring already exists, delete and reinstall" -BackgroundColor DarkGreen -ForegroundColor White
# Stop the nezha-agent process
if (Get-Process -Name "nezha-agent" -ErrorAction SilentlyContinue) {
Stop-Process -Name "nezha-agent" -Force
}
C:\nezha\nezha-agent.exe service uninstall
Remove-Item "C:\nezha" -Recurse -Force
}
# Determine download URL
$region = "Unknown"
foreach ($url in ("https://dash.cloudflare.com/cdn-cgi/trace", "https://developers.cloudflare.com/cdn-cgi/trace", "https://1.0.0.1/cdn-cgi/trace")) {
try {
$ipapi = Invoke-RestMethod -Uri $url -TimeoutSec 5 -UseBasicParsing
if ($ipapi -match "loc=(\w+)") {
$region = $Matches[1]
break
}
} catch {
Write-Host "Error occurred while querying $url : $_"
}
}
if ($region -ne "CN") {
$download = "https://github.com/$agentrepo/releases/download/$agenttag/$file"
Write-Host "Location: $region, connect directly!" -BackgroundColor DarkRed -ForegroundColor Green
} else {
$download = "https://gitee.com/naibahq/agent/releases/download/$agenttag/$file"
Write-Host "Location: CN, use mirror address" -BackgroundColor DarkRed -ForegroundColor Green
}
# Download and extract files
Write-Host "Downloading Nezha Agent from $download" -BackgroundColor DarkGreen -ForegroundColor White
Invoke-WebRequest $download -OutFile "C:\nezha.zip"
Expand-Archive "C:\nezha.zip" -DestinationPath "C:\temp" -Force
# Create installation directory
if (!(Test-Path "C:\nezha")) { New-Item -Path "C:\nezha" -Type Directory }
# Move files to installation directory
if (Test-Path "C:\nezha\nezha-agent.exe") {
Remove-Item "C:\nezha\nezha-agent.exe" -Force
}
Move-Item -Path "C:\temp\nezha-agent.exe" -Destination "C:\nezha\nezha-agent.exe"
# Clean up temporary files
Remove-Item "C:\nezha.zip"
Remove-Item "C:\temp" -Recurse
# Install service
C:\nezha\nezha-agent.exe service install -s "$server" -p "$key" $tls
# Done
Write-Host "Enjoy It!" -BackgroundColor DarkGreen -ForegroundColor Red