如何以管理员身份无提示运行PowerShell脚本修复Windows激活问题并远程执行?
解决Windows激活修复脚本的权限与弹窗问题及远程运行方案
1. 消除slmgr命令的弹窗
slmgr本质是VBS脚本,默认用wscript.exe执行会弹出GUI确认框。换成cscript.exe(控制台版脚本宿主)就能静默运行,完全无弹窗。修改后的脚本命令如下:
# 卸载产品密钥(静默模式) cscript //nologo $env:SystemRoot\System32\slmgr.vbs /upk Start-Sleep 3 # 清除产品密钥缓存 cscript //nologo $env:SystemRoot\System32\slmgr.vbs /cpky Start-Sleep 3 # 重置激活状态 cscript //nologo $env:SystemRoot\System32\slmgr.vbs /rearm
//nologo:去掉cscript的启动标识,输出更简洁$env:SystemRoot:用系统环境变量代替硬编码路径,适配不同盘符的Windows系统
2. 让脚本自动获取管理员权限
在脚本最顶部添加权限检查逻辑,若当前会话未以管理员身份运行,自动重新启动并提升权限:
# 检查是否拥有管理员权限 $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { # 以管理员身份重新启动脚本 Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs exit } # 以下是修复命令(已改为无弹窗版本) cscript //nologo $env:SystemRoot\System32\slmgr.vbs /upk Start-Sleep 3 cscript //nologo $env:SystemRoot\System32\slmgr.vbs /cpky Start-Sleep 3 cscript //nologo $env:SystemRoot\System32\slmgr.vbs /rearm
-NoProfile:避免加载用户配置文件,加快启动速度-ExecutionPolicy Bypass:绕过PowerShell的执行策略限制,确保脚本能正常运行-Verb RunAs:触发UAC权限提升,获取管理员身份
3. 不通过Defender for Endpoint远程运行脚本
完全可以,以下是几种常用方案:
方案一:PowerShell远程(WinRM)
前提:目标机器已启用WinRM(域环境通常默认配置,工作组机器需手动执行Enable-PSRemoting -Force启用)。
直接远程执行脚本文件:
Invoke-Command -ComputerName "目标机器名或IP" -FilePath "本地修复脚本路径.ps1" -Credential (Get-Credential)
或直接传递命令块:
Invoke-Command -ComputerName "目标机器名或IP" -Credential (Get-Credential) -ScriptBlock { cscript //nologo $env:SystemRoot\System32\slmgr.vbs /upk Start-Sleep 3 cscript //nologo $env:SystemRoot\System32\slmgr.vbs /cpky Start-Sleep 3 cscript //nologo $env:SystemRoot\System32\slmgr.vbs /rearm }
方案二:PsExec工具(Sysinternals)
下载PsExec后,通过以下命令远程执行:
.\PsExec.exe \\目标机器名 -u 管理员用户名 -p 密码 powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "cscript //nologo %SystemRoot%\System32\slmgr.vbs /upk; Start-Sleep 3; cscript //nologo %SystemRoot%\System32\slmgr.vbs /cpky; Start-Sleep 3; cscript //nologo %SystemRoot%\System32\slmgr.vbs /rearm"
方案三:组策略批量部署
将修复脚本放到网络共享路径(确保目标机器有读取权限),通过组策略的「计算机配置→脚本→启动脚本」添加该脚本,机器启动时会自动以系统权限执行,无需用户干预。
内容的提问来源于stack exchange,提问作者Mr. Nutcase
相关产品推荐
相关产品推荐

