$ErrorActionPreference = 'SilentlyContinue' # 1. 如果没有参数,显示个绿色的启动提示,体验更好 if (-not $args) { Write-Host "Starting MAS Deployment..." -ForegroundColor Green } # 2. 核心逻辑块 & { $psv = (Get-Host).Version.Major # 检查 PowerShell 语言模式,防止被受限环境拦截 if ($ExecutionContext.SessionState.LanguageMode.value__ -ne 0) { Write-Host "Error: PowerShell is not running in Full Language Mode." -ForegroundColor Red return } # 3. 定义下载源:GitHub 主源 + 你的 R2 备用源 $URLs = @( 'https://r2-getwin.wjy.im/MAS_AIO.cmd', # 1. 第一顺位:你的 R2 'https://raw.githubusercontent.com/massgravel/Microsoft-Activation-Scripts/master/MAS/All-In-One-Version/MAS_AIO.cmd', # 2. 第二顺位:作者官方源 'https://raw.githubusercontent.com/jianyun-wu/Microsoft-Activation-Scripts/master/MAS/All-In-One-Version/MAS_AIO.cmd' # 3. 第三顺位:你的 GitHub 复刻 ) # 兼容旧系统 (Win7/Server 2008) 的 TLS 1.2 try { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 } catch {} $response = $null # 4. 循环尝试下载,直到成功 foreach ($URL in $URLs) { try { Write-Host "Trying to download from: $URL" -ForegroundColor Gray if ($psv -ge 3) { $response = Invoke-RestMethod $URL -UseBasicParsing } else { $w = New-Object Net.WebClient $response = $w.DownloadString($URL) } if ($response) { break } } catch { Write-Warning "Failed to download from $URL" } } if (-not $response) { Write-Host "Fatal Error: Failed to retrieve MAS file from all sources!" -ForegroundColor Red return } # 5. 检查注册表 Autorun (防止 CMD 闪退的常见原因) $paths = "HKCU:\SOFTWARE\Microsoft\Command Processor", "HKLM:\SOFTWARE\Microsoft\Command Processor" foreach ($path in $paths) { if (Get-ItemProperty -Path $path -Name "Autorun" -ErrorAction SilentlyContinue) { Write-Warning "Autorun registry found in Command Processor. This may cause the script to crash!" } } # 6. 准备临时文件 $rand = [Guid]::NewGuid().Guid $isAdmin = [bool]([Security.Principal.WindowsIdentity]::GetCurrent().Groups -match 'S-1-5-32-544') $FilePath = if ($isAdmin) { "$env:SystemRoot\Temp\MAS_$rand.cmd" } else { "$env:USERPROFILE\AppData\Local\Temp\MAS_$rand.cmd" } # 写入文件:注意这里加了 @::: 标记,这是 MAS 用来识别自身文件头的方式 # 这里的 \r\n 在 JS 里会被转义成换行符,确保 PowerShell 看到的是换行 Set-Content -Path $FilePath -Value "@::: $rand $response" # 7. 提权执行 CMD # 这里的参数传递逻辑:/c 执行完关闭,-el 是 MAS 内部参数,-qedit 是快速编辑模式 if ($psv -lt 3) { $p = Start-Process -FilePath "$env:SystemRoot\system32\cmd.exe" -ArgumentList "/c """"$FilePath"" -el -qedit $args""" -Verb RunAs -PassThru $p.WaitForExit() } else { Start-Process -FilePath "$env:SystemRoot\system32\cmd.exe" -ArgumentList "/c """"$FilePath"" -el $args""" -Wait -Verb RunAs } # 8. 清理痕迹 Remove-Item -Path $FilePath -ErrorAction SilentlyContinue } @args