2022-04-29 03:23:33 +08:00
#Get server and key
2022-05-14 22:02:08 +08:00
param ( $server , $key , $tls )
2022-04-29 03:23:33 +08:00
# Download latest release from github
2022-06-23 14:03:03 +08:00
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
}
2023-05-11 09:39:38 +08:00
$agentrepo = " nezhahq/agent "
2024-05-21 23:11:43 +08:00
# x86 or x64 or arm64
2022-04-29 03:23:33 +08:00
if ( [ System.Environment ] :: Is64BitOperatingSystem ) {
2024-05-21 23:11:43 +08:00
if ( $env:PROCESSOR_ARCHITECTURE -eq " ARM64 " ) {
$file = " nezha-agent_windows_arm64.zip "
} else {
$file = " nezha-agent_windows_amd64.zip "
}
2022-04-29 03:23:33 +08:00
}
else {
$file = " nezha-agent_windows_386.zip "
}
2022-06-23 14:03:03 +08:00
$agentreleases = " https://api.github.com/repos/ $agentrepo /releases "
2022-05-10 12:37:30 +08:00
#重复运行自动更新
2024-05-21 23:11:43 +08:00
if ( Test-Path " C:\nezha\nezha-agent.exe " ) {
2022-05-10 12:37:30 +08:00
Write-Host " Nezha monitoring already exists, delete and reinstall " -BackgroundColor DarkGreen -ForegroundColor White
2024-05-21 23:11:43 +08:00
C: \ nezha \ nezha-agent . exe service uninstall
2022-05-10 12:37:30 +08:00
Remove-Item " C:\nezha " -Recurse
}
#TLS/SSL
2022-04-29 03:23:33 +08:00
Write-Host " Determining latest nezha release " -BackgroundColor DarkGreen -ForegroundColor White
[ Net.ServicePointManager ] :: SecurityProtocol = [ Net.SecurityProtocolType ] :: Tls12
2022-06-23 14:03:03 +08:00
$agenttag = ( Invoke-WebRequest -Uri $agentreleases -UseBasicParsing | ConvertFrom-Json ) [ 0 ] . tag_name
2024-05-21 23:11:43 +08:00
if ( [ string ] :: IsNullOrWhiteSpace ( $agenttag ) ) {
$optionUrl = " https://fastly.jsdelivr.net/gh/nezhahq/agent/ "
Try {
$response = Invoke-WebRequest -Uri $optionUrl -UseBasicParsing -TimeoutSec 10
if ( $response . StatusCode -eq 200 ) {
$versiontext = $response . Content | findstr / c: " option.value "
$version = [ regex ] :: Match ( $versiontext , " @(\d+\.\d+\.\d+) " ) . Groups [ 1 ] . Value
$agenttag = " v " + $version
}
} Catch {
$optionUrl = " https://gcore.jsdelivr.net/gh/nezhahq/agent/ "
$response = Invoke-WebRequest -Uri $optionUrl -UseBasicParsing -TimeoutSec 10
if ( $response . StatusCode -eq 200 ) {
$versiontext = $response . Content | findstr / c: " option.value "
$version = [ regex ] :: Match ( $versiontext , " @(\d+\.\d+\.\d+) " ) . Groups [ 1 ] . Value
$agenttag = " v " + $version
}
}
}
2022-04-29 19:00:45 +08:00
#Region判断
2024-07-23 18:26:45 +08:00
$ipapi = " "
$region = " Unknown "
foreach ( $url in ( " https://dash.cloudflare.com/cdn-cgi/trace " , " https://cf-ns.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 : $_ "
}
}
2022-04-29 19:00:45 +08:00
echo $ipapi
if ( $region -ne " CN " ) {
2022-06-23 14:03:03 +08:00
$download = " https://github.com/ $agentrepo /releases/download/ $agenttag / $file "
Write-Host " Location: $region ,connect directly! " -BackgroundColor DarkRed -ForegroundColor Green
2022-04-29 19:00:45 +08:00
} else {
2024-07-14 07:34:45 +08:00
$download = " https://gitee.com/naibahq/agent/releases/download/ $agenttag / $file "
2022-06-23 14:03:03 +08:00
Write-Host " Location:CN,use mirror address " -BackgroundColor DarkRed -ForegroundColor Green
2022-04-29 19:00:45 +08:00
}
2022-06-23 14:03:03 +08:00
echo $download
2022-04-29 03:23:33 +08:00
Invoke-WebRequest $download -OutFile " C:\nezha.zip "
#解压
Expand-Archive " C:\nezha.zip " -DestinationPath " C:\temp " -Force
if ( ! ( Test-Path " C:\nezha " ) ) { New-Item -Path " C:\nezha " -type directory }
#整理文件
2022-05-10 12:56:37 +08:00
Move-Item -Path " C:\temp\nezha-agent.exe " -Destination " C:\nezha\nezha-agent.exe "
2022-04-29 03:23:33 +08:00
#清理垃圾
Remove-Item " C:\nezha.zip "
Remove-Item " C:\temp " -Recurse
#安装部分
2024-05-21 23:11:43 +08:00
C: \ nezha \ nezha-agent . exe service install -s $server -p $key $tls
2022-04-29 03:23:33 +08:00
#enjoy
2024-05-21 23:11:43 +08:00
Write-Host " Enjoy It! " -BackgroundColor DarkGreen -ForegroundColor Red