PowerShell脚本可批量启动Windows服务器核心网络运维服务,需按实际角色精准启用dhcpserver、dns、mpssvc等8项服务,跳过Disabled项并记录状态,执行前须以管理员身份运行、确认角色匹配、建议备份配置。
powershell 脚本可以批量启动 windows 服务器上与网络运维强相关的系统服务,但需注意:不存在“所有必需服务”的绝对清单,必须按实际角色(如 dns 服务器、dhcp 服务器、域控制器等)精准启用,避免盲目开启带来安全风险或资源浪费。
明确核心网络运维服务范围
以下服务在多数网络管理场景中高频依赖,可作为脚本启用的基准目标(仅限 Windows Server,默认已安装):
- DHCP Server(dhcpserver)— 分配 IP 地址
- DNS Server(dns)— 域名解析
- Windows Firewall(mpssvc)— 网络边界防护(建议保持运行)
- Remote Registry(remoteregistry)— 远程读取注册表(运维排查常用)
- Windows Management Instrumentation(winmgmt)— WMI 查询与远程管理基础
- Server(lanmanserver)和 Workstation(lanmanworkstation)— 文件共享与网络连接基础
- Netlogon(netlogon)— 域环境登录验证(仅域成员/域控制器需要)
编写安全可控的一键启动脚本
脚本应检查服务状态、跳过已运行项、记录操作结果,并拒绝强制启动禁用服务(如被设为 Disabled 的服务需先设为 Manual/Automatic):
$RequiredServices = @("dhcpserver", "dns", "mpssvc", "remoteregistry", "winmgmt", "lanmanserver", "lanmanworkstation", "netlogon")
<p>Write-Host "正在检查并启动网络运维相关服务..." -ForegroundColor Cyan</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill10594" title="windows-screenshot-ocr"><img
src="https://img.php.cn/upload/skill/001/246/273/6adaf20d3191a227.jpg" alt="windows-screenshot-ocr" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill10594" title="windows-screenshot-ocr" class="overflowclass">windows-screenshot-ocr</a>
<p class="overflowclass">Windows全屏截图(自动标记鼠标位置)+ 原生OCR文字识别。本地运行,无需联网和API Key,适用于截图分析和自动化OCR场景。</p>
</div>
<a rel="nofollow" href="/xiazai/skill10594" title="windows-screenshot-ocr" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div><p>foreach ($svc in $RequiredServices) {
$service = Get-Service -Name $svc -ErrorAction SilentlyContinue
if (-not $service) {
Write-Warning "服务 '$svc' 未安装或不存在"
continue
}
if ($service.Status -eq 'Running') {
Write-Host "✓ $svc 已运行" -ForegroundColor Green
} else {</p><h1>若服务启动类型为 Disabled,则跳过(需人工确认)</h1><pre class="brush:php;toolbar:false;"> if ($service.StartType -eq 'Disabled') {
Write-Warning "⚠ $svc 启动类型为 Disabled,跳过启动(请手动检查必要性)"
continue
}
try {
Start-Service -Name $svc -ErrorAction Stop
Write-Host "→ $svc 已启动" -ForegroundColor Yellow
} catch {
Write-Error "✗ 启动 $svc 失败:$($_.Exception.Message)"
}
}
}
执行前必须完成的三件事
- 以管理员身份运行 PowerShell:右键“Windows PowerShell(管理员)”,否则多数服务无法操作
- 确认服务器角色匹配服务需求:例如非域环境无需 netlogon;未部署 DNS 就不要强行启 dns 服务(可能报错或监听失败)
-
备份当前服务配置(可选但推荐):运行
Get-Service | Where-Object {$_.StartType -ne 'Disabled'} | Export-Csv .\svc-config-backup.csv -NoTypeInformation
后续建议:转为自动恢复 + 监控
单纯“一键启动”是临时手段。生产环境应进一步:
- 将关键服务启动类型设为 Automatic(使用
Set-Service -Name xxx -StartupType Automatic) - 配置 Windows Event Log 订阅,监控服务意外停止事件(ID 7036)
- 结合 Task Scheduler 或监控工具(如 Zabbix、PRTG),定期调用
Get-Service xxx | Where Status -ne Running并告警










