Set-StrictMode -Version Latest $ErrorActionPreference = "Stop" $PackageName = "adspower-browser" $IsWindowsRuntime = $env:OS -eq "Windows_NT" -or $PSVersionTable.Platform -eq "Win32NT" function Write-Log { param([string]$Message) Write-Host "[install] $Message" } function Test-Command { param([string]$Name) return [bool](Get-Command $Name -ErrorAction SilentlyContinue) } function Refresh-Path { $machinePath = [Environment]::GetEnvironmentVariable("Path", "Machine") $userPath = [Environment]::GetEnvironmentVariable("Path", "User") $env:Path = "$machinePath;$userPath" } function Install-NodeViaNvm { Write-Log "Installing Node.js LTS via nvm..." cmd.exe /c "nvm install lts" if ($LASTEXITCODE -ne 0) { throw "nvm install failed." } Write-Log "Switching to Node.js LTS via nvm..." cmd.exe /c "nvm use lts" if ($LASTEXITCODE -ne 0) { throw "nvm use failed. Try running PowerShell as Administrator." } } function Get-WindowsOperatingSystem { try { return Get-CimInstance -ClassName Win32_OperatingSystem } catch { return Get-WmiObject -Class Win32_OperatingSystem } } function Test-WindowsServer2016 { $operatingSystem = Get-WindowsOperatingSystem if ($operatingSystem.Caption -match "Windows Server 2016") { return $true } $version = [version]$operatingSystem.Version return $operatingSystem.ProductType -ne 1 -and $version.Major -eq 10 -and $version.Minor -eq 0 -and [int]$operatingSystem.BuildNumber -eq 14393 } function Get-NodeMsiVersion { if (Test-WindowsServer2016) { return "v20.20.2" } return "v18.20.8" } function Install-NodeViaMsi { $nodeVersion = Get-NodeMsiVersion $architecture = if ([Environment]::Is64BitOperatingSystem) { "x64" } else { "x86" } $installerPath = Join-Path $env:TEMP "node-$nodeVersion-$architecture.msi" $downloadUrl = "https://nodejs.org/dist/$nodeVersion/node-$nodeVersion-$architecture.msi" Write-Log "Downloading Node.js $nodeVersion installer..." [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 Invoke-WebRequest -Uri $downloadUrl -OutFile $installerPath -UseBasicParsing Write-Log "Installing Node.js $nodeVersion via MSI..." $installProcess = Start-Process msiexec.exe -Wait -PassThru -ArgumentList "/i `"$installerPath`" /qn /norestart" if ($installProcess.ExitCode -ne 0) { throw "Node.js MSI installation failed with exit code $($installProcess.ExitCode)." } } function Install-Node { if (-not (Test-Command "npm.cmd")) { if (Test-Command "nvm") { Install-NodeViaNvm } elseif (Test-Command "winget") { Write-Log "Installing Node.js via winget..." winget install OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements --silent *> $null } elseif (Test-Command "choco") { Write-Log "Installing Node.js via Chocolatey..." choco install nodejs-lts -y *> $null } elseif (Test-Command "scoop") { Write-Log "Installing Node.js via Scoop..." scoop install nodejs-lts *> $null } else { Install-NodeViaMsi } } Refresh-Path } function Install-Package { Write-Log "Installing $PackageName globally..." & npm.cmd install -g $PackageName Write-Log "Installation complete." } if (-not $IsWindowsRuntime) { throw "install.ps1 only supports Windows." } Install-Node Install-Package