2022-04-28 15:23:33 -04:00
#Get server and key
2022-05-14 10:02:08 -04:00
param ( $server , $key , $tls )
2022-04-28 15:23:33 -04:00
# Download latest release from github
2022-06-23 02:03:03 -04: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-10 21:39:38 -04:00
$agentrepo = " nezhahq/agent "
2022-06-23 02:03:03 -04:00
$nssmrepo = " nezhahq/nssm-backup "
2022-04-28 15:23:33 -04:00
# x86 or x64
if ( [ System.Environment ] :: Is64BitOperatingSystem ) {
$file = " nezha-agent_windows_amd64.zip "
}
else {
$file = " nezha-agent_windows_386.zip "
}
2022-06-23 02:03:03 -04:00
$agentreleases = " https://api.github.com/repos/ $agentrepo /releases "
$nssmreleases = " https://api.github.com/repos/ $nssmrepo /releases "
2022-05-10 00:37:30 -04:00
#重复运行自动更新
if ( Test-Path " C:\nezha " ) {
Write-Host " Nezha monitoring already exists, delete and reinstall " -BackgroundColor DarkGreen -ForegroundColor White
C: / nezha / nssm . exe stop nezha
C: / nezha / nssm . exe remove nezha
Remove-Item " C:\nezha " -Recurse
}
#TLS/SSL
2022-04-28 15:23:33 -04:00
Write-Host " Determining latest nezha release " -BackgroundColor DarkGreen -ForegroundColor White
[ Net.ServicePointManager ] :: SecurityProtocol = [ Net.SecurityProtocolType ] :: Tls12
2022-06-23 02:03:03 -04:00
$agenttag = ( Invoke-WebRequest -Uri $agentreleases -UseBasicParsing | ConvertFrom-Json ) [ 0 ] . tag_name
$nssmtag = ( Invoke-WebRequest -Uri $nssmreleases -UseBasicParsing | ConvertFrom-Json ) [ 0 ] . tag_name
2022-04-29 07:00:45 -04:00
#Region判断
$ipapi = Invoke-RestMethod -Uri " https://api.myip.com/ " -UserAgent " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1 "
$region = $ipapi . cc
echo $ipapi
if ( $region -ne " CN " ) {
2022-06-23 02:03:03 -04:00
$download = " https://github.com/ $agentrepo /releases/download/ $agenttag / $file "
$nssmdownload = " https://github.com/ $nssmrepo /releases/download/ $nssmtag /nssm.zip "
Write-Host " Location: $region ,connect directly! " -BackgroundColor DarkRed -ForegroundColor Green
2022-04-29 07:00:45 -04:00
} else {
2022-06-23 02:03:03 -04:00
$download = " https://dn-dao-github-mirror.daocloud.io/ $agentrepo /releases/download/ $agenttag / $file "
$nssmdownload = " https://dn-dao-github-mirror.daocloud.io/ $nssmrepo /releases/download/ $nssmtag /nssm.zip "
Write-Host " Location:CN,use mirror address " -BackgroundColor DarkRed -ForegroundColor Green
2022-04-29 07:00:45 -04:00
}
2022-06-23 02:03:03 -04:00
echo $download
echo $nssmdownload
2022-04-28 15:23:33 -04:00
Invoke-WebRequest $download -OutFile " C:\nezha.zip "
#使用nssm安装服务
2022-06-23 02:03:03 -04:00
Invoke-WebRequest $nssmdownload -OutFile " C:\nssm.zip "
2022-04-28 15:23:33 -04:00
#解压
Expand-Archive " C:\nezha.zip " -DestinationPath " C:\temp " -Force
Expand-Archive " C:\nssm.zip " -DestinationPath " C:\temp " -Force
if ( ! ( Test-Path " C:\nezha " ) ) { New-Item -Path " C:\nezha " -type directory }
#整理文件
2022-05-10 00:56:37 -04:00
Move-Item -Path " C:\temp\nezha-agent.exe " -Destination " C:\nezha\nezha-agent.exe "
2022-04-28 15:23:33 -04:00
if ( $file = " nezha-agent_windows_amd64.zip " ) {
Move-Item -Path " C:\temp\nssm-2.24\win64\nssm.exe " -Destination " C:\nezha\nssm.exe "
}
else {
Move-Item -Path " C:\temp\nssm-2.24\win32\nssm.exe " -Destination " C:\nezha\nssm.exe "
}
#清理垃圾
Remove-Item " C:\nezha.zip "
Remove-Item " C:\nssm.zip "
Remove-Item " C:\temp " -Recurse
#安装部分
2022-05-14 10:02:08 -04:00
C: \ nezha \ nssm . exe install nezha C: \ nezha \ nezha-agent . exe -s $server -p $key $tls -d
2022-04-28 15:23:33 -04:00
C: \ nezha \ nssm . exe start nezha
#enjoy
Write-Host " Enjoy It! " -BackgroundColor DarkGreen -ForegroundColor Red