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

Windows自动安装脚本重复从头启动问题求助

问题解决:Windows自动安装脚本重启后重复执行的修复

问题现象

通过Shell启动Windows自动安装脚本后,脚本执行任务并重启,但每次重启后都会从头执行,无法完成完整的操作系统安装,无法达到手动安装后的登录效果,且无法使用unattend.xml。

问题根源

原脚本存在两个核心问题:

  • 未在每个任务执行完成后更新注册表的进度标记,导致重启后无法识别已完成的任务
  • 所有任务一次性执行后才重启,部分需要重启生效的操作(如修改计算机名)会打断流程,且无续跑逻辑

修改后的脚本

$regKey = "HKLM:\SOFTWARE\MySetupScript"
$regValueName = "CurrentTask"

# 初始化注册表项(如果不存在则创建)
if (-not (Test-Path $regKey)) {
    New-Item -Path $regKey -Force | Out-Null
    Set-ItemProperty -Path $regKey -Name $regValueName -Value "Start"
}

# 获取当前任务进度
$currentTask = (Get-ItemProperty -Path $regKey).$regValueName

# 任务续跑逻辑
switch ($currentTask) {
    "Start" {
        # Task 1: 创建本地用户
        Write-Host "执行任务1:创建本地用户..."
        $username = "TESTUSER"
        $password = ConvertTo-SecureString -String "test123" -AsPlainText -Force
        # 先检查用户是否已存在,避免重复创建报错
        if (-not (Get-LocalUser -Name $username -ErrorAction SilentlyContinue)) {
            New-LocalUser -Name $username -Password $password -FullName $username -Description "$username 用户已创建" -PasswordNeverExpires:$true
        }
        # 更新进度到注册表
        Set-ItemProperty -Path $regKey -Name $regValueName -Value "PrivacySettings"
        # 无需重启,直接进入下一个任务
        break
    }
    "PrivacySettings" {
        # Task 2: 禁用隐私设置
        Write-Host "执行任务2:禁用隐私设置..."
        # 确保注册表路径存在
        if (-not (Test-Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection")) {
            New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection" -Force | Out-Null
        }
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Privacy" -Name "TailoredExperiencesWithDiagnosticDataEnabled" -Value 0
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection" -Name "AllowTelemetry" -Value 0
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection" -Name "AllowTailoredExperiencesWithDiagnosticData" -Value 0
        Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection" -Name "DisableTailoredExperiencesWithDiagnosticData" -Value 1
        # 更新进度到注册表
        Set-ItemProperty -Path $regKey -Name $regValueName -Value "RegionKeyboard"
        # 无需重启,直接进入下一个任务
        break
    }
    "RegionKeyboard" {
        # Task 3: 修改区域和键盘布局
        Write-Host "执行任务3:修改区域和键盘布局..."
        Set-WinSystemLocale -SystemLocale "de-DE"
        Set-WinUserLanguageList -LanguageList "de-DE" -Force
        Set-WinUILanguageOverride -Language "de-DE" -Force
        Set-WinDefaultInputMethodOverride -InputTip "0407:00000407"
        # 更新进度到注册表
        Set-ItemProperty -Path $regKey -Name $regValueName -Value "ComputerName"
        # 区域设置修改需要重启生效,先重启
        Write-Host "区域设置已修改,重启计算机..."
        Restart-Computer -Force
        exit
    }
    "ComputerName" {
        # Task 4: 修改计算机名称
        Write-Host "执行任务4:修改计算机名称..."
        $newName = "Workstation9"
        # 检查当前计算机名是否已符合要求
        if ($env:COMPUTERNAME -ne $newName) {
            Rename-Computer -NewName $newName -Force
            # 计算机名修改需要重启,更新进度后重启
            Set-ItemProperty -Path $regKey -Name $regValueName -Value "Complete"
            Write-Host "计算机名称已修改,重启计算机..."
            Restart-Computer -Force
            exit
        } else {
            # 计算机名已正确,直接标记为完成
            Set-ItemProperty -Path $regKey -Name $regValueName -Value "Complete"
        }
        break
    }
    "Complete" {
        # 所有任务完成,清理注册表并退出
        Write-Host "所有安装任务已完成!"
        Remove-Item -Path $regKey -Force
        exit
    }
}

# 注:脚本需要设置为开机自启动,确保重启后自动运行

关键修复点说明

  • 进度持久化:每次完成任务后更新注册表的CurrentTask值,重启后脚本读取该值直接续跑对应任务
  • 幂等性处理:每个任务都增加了检查逻辑(如检查用户是否已存在、计算机名是否已修改),避免重复执行报错
  • 分阶段重启:针对需要重启生效的操作(区域设置、计算机名修改),在更新进度后触发重启,确保重启后从正确的步骤继续
  • 开机自启动配置:需要将脚本设置为开机自启动(可通过创建计划任务或添加到HKLM:\Software\Microsoft\Windows\CurrentVersion\Run注册表项),确保重启后自动执行脚本

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 21:43:10