PowerShell,直到进入循环

时间:2019-01-23 18:21:08

标签: powershell

我正在尝试编写一个脚本,该脚本将使用PSWindowsUpdate更新Windows。我希望脚本继续运行PSWindowsUpdate,直到没有其他更新可供安装为止。该脚本在最后一个“ until”命令上失败,指示:

until : The term 'until' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At C:\Users\user\Desktop\Install-WindowsUpdates-Edit.ps1:63 char:2
+ } until ($updates -eq $null)
+   ~~~~~
    + CategoryInfo          : ObjectNotFound: (until:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

直到上面几行都没有问题。我可以看到的所有括号都以{}结尾。我不明白为什么会发生此错误。

<#
.SYNOPSIS
This script will automatically install all avaialable windows updates on a device and will automatically reboot if needed, after reboot, windows updates will continue to run until no more updates are available. Pilfered     from: https://www.altaro.com/msp-dojo/powershell-windows-updates/#comment-272
#>

"Installing NuGet Package Provider"

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

"Installing the PSWindowsUpdate module"

Install-Module PSWindowsUpdate -Force

# retrieves a list of available updates
"Checking for new updates"

$updates = Invoke-Command -ScriptBlock {Get-WUList -Verbose}

# counts how many updates are available
$updatenumber = ($updates.kb).Count

# if there are available updates proceed with installing the updates and then reboot
if ($updates -ne $null) {
    # Command to install windows updates, creates a scheduled task on the computer
    $Script = {
        Import-Module PSWindowsUpdate;
        Get-WindowsUpdate -AcceptAll -Install | Out-File C:\PSWindowsUpdate.log
    }

    Invoke-WUJob -Script $Script -Confirm:$false -RunNow

    # Show update status until the amount of installed updates equals the same as the amount of updates available

    sleep -Seconds 30

    do {
        $updatestatus = Get-Content c$\PSWindowsUpdate.log

        "Currently processing the following update:"
        Get-Content c$\PSWindowsUpdate.log | select-object -last 1

        sleep -Seconds 10

        $ErrorActionPreference = 'SilentlyContinue'
        $installednumber = ([regex]::Matches($updatestatus, "Installed" )).Count
        $ErrorActionPreference = 'Continue'
    } until ($installednumber -eq $updatenumber)

    # restarts the remote computer and waits till it starts up again

    "restarting computer"

    Restart-Computer -Wait -Force
} until ($updates -eq $null) #ERROR THROWN HERE

# removes schedule task from computer
#Invoke-Command -ScriptBlock {Unregister-ScheduledTask -TaskName PSWindowsUpdate -Confirm:$false}

Remove-Module -Name "PSWindowsUpdate"

"Windows is now up to date"

0 个答案:

没有答案