248 lines
7.6 KiB
PowerShell
248 lines
7.6 KiB
PowerShell
# # Check if ZeroTier is installed, exit if not
|
||
# # if not exist "C:\Program Files\ZeroTier\One\ZeroTier One.exe" (
|
||
# # echo "ZeroTier not detected, exiting..."
|
||
# # exit /b 1
|
||
# # )
|
||
# # Check for administrator privileges
|
||
# # if not "%USERPROFILE%"=="C:\Users\Administrator" (
|
||
# # echo "Please run this script as Administrator."
|
||
# # exit /b 1
|
||
# # )
|
||
# # Check if curl is installed, exit if not
|
||
# # if not exist "C:\Program Files\curl\bin\curl.exe" (
|
||
# # echo "curl not detected, exiting..."
|
||
# # exit /b 1
|
||
# # )
|
||
|
||
# # Remove old planet file
|
||
# del "C:\ProgramData\ZeroTier\One\planet"
|
||
# # Download https://o.nmgjg.com.cn/install/zerotier/planet and overwrite to C:/ProgramData/ZeroTier/One/planet
|
||
# echo "Downloading planet file..."
|
||
# #curl.exe -s -o "C:/ProgramData/ZeroTier/One/planet" "http://150.158.212.94:3000/planet?key=60aa98087ae3a21f"
|
||
# curl.exe -s -o "C:/ProgramData/ZeroTier/One/planet" "https://o.nmgjg.com.cn/install/zerotier/planet"
|
||
# echo "Planet file downloaded, joining network..."
|
||
# Start-Sleep -Seconds 3
|
||
# zerotier-cli join af7c492762d601cd
|
||
# echo "Restarting ZeroTier service..."
|
||
# Restart-Service -Name "ZeroTierOneService"
|
||
# echo "Waiting for ZeroTier to start..."
|
||
# Start-Sleep -Seconds 5
|
||
# echo "Joining new network again..."
|
||
# zerotier-cli join af7c492762d601cd
|
||
# echo "Leaving old network..."
|
||
# zerotier-cli leave 12ac4a1e71a30025
|
||
|
||
# 检查管理员权限
|
||
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltinRole] "Administrator")) {
|
||
Write-Host "错误:请以管理员身份运行此脚本!" -ForegroundColor Red
|
||
Start-Sleep -Seconds 3
|
||
exit 1
|
||
}
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
# 定义彩色输出函数
|
||
function Write-Log {
|
||
param (
|
||
[string]$Message,
|
||
[string]$Color = "White"
|
||
)
|
||
Write-Host $Message -ForegroundColor $Color
|
||
}
|
||
|
||
# 检测是否已安装 ZeroTier 客户端
|
||
function Check-ZTInstalled {
|
||
$ztPath = "C:\Program Files\ZeroTier\One\ZeroTier One.exe"
|
||
if (Test-Path $ztPath) {
|
||
return $true
|
||
}
|
||
else {
|
||
return $false
|
||
}
|
||
}
|
||
|
||
# 卸载 ZeroTier 客户端
|
||
function Uninstall-ZT {
|
||
Write-Log "开始卸载 ZeroTier 客户端..." "Yellow"
|
||
$ztUninstallKey = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
|
||
$uninstallString = $null
|
||
|
||
# 查找 ZeroTier 卸载字符串
|
||
$keys = Get-ChildItem $ztUninstallKey
|
||
foreach ($key in $keys) {
|
||
$displayName = (Get-ItemProperty $key.PSPath).DisplayName 2>$null
|
||
if ($displayName -and $displayName -like "*ZeroTier*") {
|
||
$uninstallString = (Get-ItemProperty $key.PSPath).UninstallString
|
||
break
|
||
}
|
||
}
|
||
|
||
if ($uninstallString) {
|
||
# 有的卸载字符串带有参数,提取可执行文件路径
|
||
if ($uninstallString.StartsWith("msiexec")) {
|
||
# MSI卸载,带 /x 参数即可
|
||
$args = $uninstallString -replace 'msiexec.exe', ''
|
||
# 使用静默卸载
|
||
Write-Log "检测到 MSI 卸载程序,执行静默卸载..." "Yellow"
|
||
Start-Process msiexec.exe -ArgumentList "/x $args /qn" -Wait
|
||
}
|
||
else {
|
||
# 其他卸载程序
|
||
Write-Log "执行卸载程序: $uninstallString" "Yellow"
|
||
Start-Process -FilePath "cmd.exe" -ArgumentList "/c `"$uninstallString /quiet`"" -Wait
|
||
}
|
||
Write-Log "卸载完成。" "Green"
|
||
}
|
||
else {
|
||
Write-Log "未找到 ZeroTier 卸载程序,可能未安装或已卸载。" "Red"
|
||
}
|
||
|
||
# 等待服务和进程关闭
|
||
Start-Sleep -Seconds 5
|
||
}
|
||
|
||
# 下载文件函数
|
||
function Download-File($url, $outputPath) {
|
||
Write-Log "开始下载: $url" "Cyan"
|
||
try {
|
||
Invoke-WebRequest -Uri $url -OutFile $outputPath -UseBasicParsing
|
||
Write-Log "下载成功: $outputPath" "Green"
|
||
}
|
||
catch {
|
||
Write-Log "下载失败: $_" "Red"
|
||
exit 1
|
||
}
|
||
}
|
||
|
||
# 替换 planet 文件
|
||
function Replace-PlanetFile {
|
||
$planetPath = "$env:ProgramData\ZeroTier\One\planet"
|
||
$tempPath = "$env:TEMP\planet"
|
||
$planetUrl = "https://o.nmgjg.com.cn/install/zerotier/planet"
|
||
|
||
if (Test-Path $planetPath) {
|
||
try {
|
||
Stop-Service -Name "ZeroTierOne" -ErrorAction SilentlyContinue
|
||
}
|
||
catch {}
|
||
|
||
Write-Log "删除旧 planet 文件..." "Yellow"
|
||
Remove-Item -Path $planetPath -Force -ErrorAction SilentlyContinue
|
||
}
|
||
|
||
Write-Log "下载并替换 planet 文件..." "Cyan"
|
||
Download-File -url $planetUrl -outputPath $tempPath
|
||
|
||
Move-Item -Path $tempPath -Destination $planetPath -Force
|
||
Write-Log "planet 文件替换完成。" "Green"
|
||
}
|
||
|
||
# 加入 ZeroTier 网络
|
||
function Join-ZTNetwork {
|
||
$networkId = "af7c492762d601cd"
|
||
Write-Log "正在加入 ZeroTier 网络: $networkId" "Cyan"
|
||
$ztCli = "C:\Program Files\ZeroTier\One\zerotier-cli.exe"
|
||
if (-not (Test-Path $ztCli)) {
|
||
Write-Log "找不到 zerotier-cli.exe,无法加入网络。" "Red"
|
||
return
|
||
}
|
||
|
||
& $ztCli join $networkId
|
||
if ($LASTEXITCODE -eq 0) {
|
||
Write-Log "成功加入网络 $networkId" "Green"
|
||
}
|
||
else {
|
||
Write-Log "加入网络失败,错误码: $LASTEXITCODE" "Red"
|
||
}
|
||
}
|
||
|
||
# 安装 ZeroTier 客户端
|
||
function Install-ZT {
|
||
$installerDir = "C:\zerotier_installer"
|
||
$installerPath = Join-Path $installerDir "win64.msi"
|
||
$installerUrl = "https://o.nmgjg.com.cn/install/zerotier/win64.msi"
|
||
|
||
if (-not (Test-Path $installerDir)) {
|
||
New-Item -ItemType Directory -Path $installerDir | Out-Null
|
||
}
|
||
|
||
Download-File -url $installerUrl -outputPath $installerPath
|
||
|
||
Write-Log "开始静默安装 ZeroTier 客户端..." "Yellow"
|
||
Start-Process msiexec.exe -ArgumentList "/i `"$installerPath`" /qn /norestart" -Wait
|
||
|
||
Write-Log "ZeroTier 安装完成。" "Green"
|
||
}
|
||
|
||
# 安装 MeshCentral 客户端
|
||
function Install-MeshCentral {
|
||
$installerDir = "C:\zerotier_installer"
|
||
$installerPath = Join-Path $installerDir "win64.exe"
|
||
$installerUrl = "https://o.nmgjg.com.cn/install/meshcentral/win64.exe"
|
||
|
||
if (-not (Test-Path $installerDir)) {
|
||
New-Item -ItemType Directory -Path $installerDir | Out-Null
|
||
}
|
||
|
||
Download-File -url $installerUrl -outputPath $installerPath
|
||
|
||
Write-Log "开始安装 MeshCentral 客户端..." "Yellow"
|
||
Start-Process -FilePath $installerPath -ArgumentList "-install" -Wait
|
||
Write-Log "MeshCentral 安装完成。" "Green"
|
||
}
|
||
|
||
# 主流程
|
||
Write-Log "=== ZeroTier & MeshCentral 管理脚本 ===" "Magenta"
|
||
|
||
$installed = Check-ZTInstalled
|
||
|
||
if ($installed) {
|
||
$choice = Read-Host "检测到已安装 ZeroTier 客户端,是否卸载?(Y/N)"
|
||
if ($choice -match "^[Yy]$") {
|
||
Uninstall-ZT
|
||
Write-Log "请手动重启计算机以确保卸载完全后再继续操作。" "Yellow"
|
||
$restartNow = Read-Host "是否现在重启?(Y/N)"
|
||
if ($restartNow -match "^[Yy]$") {
|
||
Restart-Computer
|
||
exit
|
||
}
|
||
else {
|
||
Write-Log "请重启后重新运行此脚本。" "Red"
|
||
exit
|
||
}
|
||
}
|
||
else {
|
||
Write-Log "跳过卸载,继续下一步。" "Yellow"
|
||
}
|
||
}
|
||
else {
|
||
Write-Log "未检测到 ZeroTier 客户端安装。" "Green"
|
||
}
|
||
|
||
$installChoice = Read-Host "是否安装 ZeroTier 客户端?(Y/N)"
|
||
if ($installChoice -match "^[Yy]$") {
|
||
Install-ZT
|
||
} else {
|
||
Write-Log "跳过 ZeroTier 安装。" "Yellow"
|
||
}
|
||
|
||
Replace-PlanetFile
|
||
|
||
Join-ZTNetwork
|
||
|
||
$meshChoice = Read-Host "是否安装 MeshCentral 客户端?(Y/N)"
|
||
if ($meshChoice -match "^[Yy]$") {
|
||
Install-MeshCentral
|
||
} else {
|
||
Write-Log "跳过 MeshCentral 安装。" "Yellow"
|
||
}
|
||
|
||
$finalRestart = Read-Host "操作完成,是否立即重启计算机?(Y/N)"
|
||
if ($finalRestart -match "^[Yy]$") {
|
||
Restart-Computer
|
||
} else {
|
||
Write-Log "请根据需要手动重启计算机。" "Green"
|
||
}
|
||
|
||
Write-Log "脚本执行结束,感谢使用!" "Magenta"
|