You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Choco+PowerShell安装软件时自动重启登录及脚本续跑咨询

好的,要实现Choco安装脚本的完全自动化(包含自动重启、自动登录、断点续跑),可以分这几个核心步骤来做,我给你详细拆解每个环节的实现方式:

1. 让脚本支持断点续跑

首先得让脚本知道哪些软件已经安装过,避免重复执行。最简单的方式是用一个日志文件记录已完成的安装项:

  • 脚本开头先创建(如果不存在)一个日志文件,比如C:\ChocoInstallLogs\InstalledApps.log
  • 每次成功安装一个软件后,把软件名写入这个日志
  • 下次脚本启动时,先读取日志里的内容,过滤掉已经安装的软件,只处理剩余项

示例代码片段:

# 初始化日志目录和文件
$logPath = "C:\ChocoInstallLogs"
$logFile = Join-Path $logPath "InstalledApps.log"
if (-not (Test-Path $logPath)) { New-Item -ItemType Directory -Path $logPath | Out-Null }
if (-not (Test-Path $logFile)) { New-Item -ItemType File -Path $logFile | Out-Null }

# 读取已安装的软件列表
$installedApps = Get-Content $logFile | Where-Object { $_ -ne "" }

# 定义要安装的软件列表
$appsToInstall = @("vagrant", "virtualbox", "git", "python")

# 过滤出未安装的软件
$pendingApps = $appsToInstall | Where-Object { $_ -notin $installedApps }
2. 检测重启需求并配置重启后继续执行脚本

Choco安装部分软件(比如Vagrant)会返回退出码3010表示需要重启。我们可以捕获这个退出码,然后配置系统重启后自动运行脚本:

  • 使用Choco的--no-restart参数,禁止它自动触发重启,把控制权握在自己手里
  • 捕获每个软件安装后的退出码,如果是3010,标记需要重启
  • 把当前脚本添加到开机启动项,让系统重启后自动运行
  • 最后调用Restart-Computer触发重启

示例代码片段:

$needsRestart = $false
$runKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Run"

foreach ($app in $pendingApps) {
    Write-Host "开始安装 $app..."
    choco install $app -y --no-restart
    $exitCode = $LASTEXITCODE

    if ($exitCode -eq 3010) {
        Write-Host "$app 安装完成,需要重启系统"
        $needsRestart = $true
        $app | Out-File -FilePath $logFile -Append -Encoding utf8
    } elseif ($exitCode -eq 0) {
        Write-Host "$app 安装成功"
        $app | Out-File -FilePath $logFile -Append -Encoding utf8
    } else {
        Write-Warning "$app 安装失败,退出码:$exitCode"
    }
}

# 如果需要重启,配置开机自启脚本并重启
if ($needsRestart) {
    # 获取当前脚本的路径
    $scriptPath = $MyInvocation.MyCommand.Path

    # 添加到开机启动项(针对当前用户)
    Set-ItemProperty -Path $runKey -Name "ChocoAutoInstall" -Value "powershell.exe -ExecutionPolicy Bypass -File `"$scriptPath`""

    Write-Host "即将重启系统,重启后将继续执行剩余安装任务..."
    Restart-Computer -Force
} else {
    # 所有安装完成,移除开机启动项
    if (Get-ItemProperty -Path $runKey -Name "ChocoAutoInstall" -ErrorAction SilentlyContinue) {
        Remove-ItemProperty -Path $runKey -Name "ChocoAutoInstall"
    }
    Write-Host "所有软件安装完成,无需重启"
}
3. 设置自动登录(仅限测试/隔离环境!)

要让系统重启后自动登录,需要修改Windows注册表,但强烈提醒:这个操作会把密码明文存储在注册表中,只适合测试环境或临时使用,绝对不能在生产环境操作!

添加以下代码到重启前的流程里:

# 配置自动登录(替换为你的用户名和密码)
$autoLogonKey = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Set-ItemProperty -Path $autoLogonKey -Name "AutoAdminLogon" -Value "1"
Set-ItemProperty -Path $autoLogonKey -Name "DefaultUserName" -Value "你的用户名"
Set-ItemProperty -Path $autoLogonKey -Name "DefaultPassword" -Value "你的密码"

# 可选:设置自动登录次数(比如只自动登录1次,完成后自动关闭)
Set-ItemProperty -Path $autoLogonKey -Name "AutoLogonCount" -Value 1
4. 完整脚本注意事项
  • 脚本必须以管理员身份运行,否则无法修改注册表、添加开机启动项
  • 确保PowerShell的执行策略允许运行脚本(可以用Set-ExecutionPolicy Bypass -Scope Process临时设置)
  • 所有任务完成后,记得手动清理自动登录的注册表项,避免留下安全隐患

内容的提问来源于stack exchange,提问作者Axel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:40:43