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

PowerShell脚本重启后无法继续执行的问题求助

PowerShell脚本重启后无法继续执行的问题求助

各位大佬好,我刚接触PowerShell脚本不久,最近写了个给我们学徒用的新笔记本装机脚本,用来帮客户的新笔记本完成初始化:先重命名电脑、重启系统,之后继续安装各类软件——其中有些是我们内部的系统软件,没法通过winget获取,只能放在U盘里部署。

但现在遇到个棘手的问题:脚本执行到重启步骤后,就没办法继续往下运行了。我尝试用ScheduledJob来恢复暂停的任务,但好像没起作用。下面是我的完整脚本,麻烦各位帮忙看看哪里出问题了,万分感谢!

Set-ExecutionPolicy Unrestricted -force

Install-Module ThreadJob -Force
import-module ThreadJob

Install-Module PSWindowsUpdate -force
import-module pswindowsupdate

Import-Module ScheduledTasks

Install-Module -Name ScheduledJobTools -force
import-module ScheduledJobTools

function Install-SysInternalsTool{
    $targetDir = Join-Path $env:WinDir "System32\SysInternals"
    $tools = @{
        Autologon = "http://live.sysinternals.com/Autologon.exe"
    }
    Write-Verbose "Create Directory: $targetDir"
    New-Item -ItemType Directory -Path $targetDir -Force | Out-Null
    try{
        $wc = New-Object System.Net.WebClient
        foreach($tool in $tools.Values){
            $filePath = Join-Path $targetDir ([IO.Path]::GetFileName($tool))
            Write-Verbose "Downloading $tool"
            $wc.DownloadFile($tool,$filePath)
        }
    }
    finally{
        $wc.Dispose()
    }
}

function Enable-AutoLogon{
    param(
        [string] $UserName,
        [string] $Password
    )
    $exePath = Join-Path $env:WinDir "System32\SysInternals\AutoLogon.exe"
    if(!(Test-Path $exePath)){
        Write-Error "AutoLogon.exe is not found at $exePath"
    }
    $paths = $UserName.Split("\")
    $domain = $paths[0]
    $user   = $paths[1]
    Start-Process -FilePath  $exePath -ArgumentList "/accepteula", $user, $domain, $password -Wait
}

Install-SysInternalsTool
Enable-AutoLogon -UserName ".\admin" -Password "#bs15081!"

$baseUri = 'https://github.com/microsoft/winget-cli/releases/download'
$files = @(
    @{Uri = "$baseUri/v1.4.10173/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"
    OutFile = 'Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle'}
)

$jobs = @()
foreach ($file in $files) {
    $jobs += Start-ThreadJob -Name $file.OutFile -ScriptBlock {
        $params = $using:file
        Invoke-WebRequest @params
    }
}

Wait-Job -Job $jobs
foreach ($job in $jobs) {
    Receive-Job -Job $job
    .\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle
}

workflow renamereboot {
    $oldname = hostname
    $name = [Microsoft.VisualBasic.Interaction]::InputBox("Neuen Computernamen eingeben:", "Computer Name")
    Add-Type -AssemblyName Microsoft.VisualBasic
    Rename-Computer -NewName $name
    Register-ScheduledJob -Name renamerebootresume `
    -ScriptBlock {
        Get-Job -Name NewSetup -State Suspended `
        | Resume-Job
    }
    Restart-Computer -pscomputername $oldname -Wait -For PowerShell
    unregister-scheduledjob -name renamerebootresume
}

$laufwerk = get-volume | where drivetype -eq removable | foreach driveletter
$punkt=$laufwerk + ":"

<#if (-not(test-path -path $punkt)) {
    echo "Kein USB Stick angeschlossen"
} else {
    $labtech = Get-ChildItem "$punkt\Labtech" | where {$_.extension -eq ".msi"} | ForEach-Object {$_.FullName}
    start $labtech -NoNewWindow
    $sophos = Get-ChildItem "$punkt\Sophos" | where {$_.extension -eq ".exe"} | ForEach-Object {$_.FullName}
    start $sophos -NoNewWindow
    .\SwyxIt!German64.msi
    .\SophosConnect_2.2.75.msi
}#>

#winget import $punkt\winget.json --ignore-versions --accept-package-agreements --accept-source-agreements

winget upgrade Microsoft.DesktopAppInstaller_8wekyb3d8bbwe -h --force --disable-interactivity --accept-package-agreements --accept-source-agreements
winget install -e Mozilla.Firefox -h --force --disable-interactivity --accept-package-agreements --accept-source-agreements
winget install -e geeksoftwareGmbH.PDF24Creator -h --force --disable-interactivity --accept-package-agreements --accept-source-agreements
winget install -e 7zip.7zip -h --force --disable-interactivity --accept-package-agreements --accept-source-agreements
winget install -e Adobe.Acrobat.Reader.64-bit -h --force --disable-interactivity --accept-package-agreements --accept-source-agreements

Get-WindowsUpdate -AcceptAll -Install -autoreboot

renamereboot -JobName NewSetup 2> error.log

备注:内容来源于stack exchange,提问作者Slime

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 11:47:45