Beneath script checks for available Windows Updates but only runs the update when a certain app/process is not running. I needed this last check on my converter servers. When a conversion is running, Windows Updates are not allowed, not even just a download.

$Servers = @("server1","server2","server3")
$Username = "<domain>\<admin-user>"  
$Servicename = "ZXConverter" 
$Processname = "ZXWorkers" 
$credfile= "c:\local\data\ps-cred.txt"

if (!(Test-Path $credfile) ) {
    Write-Warning "$credfile absent. Please enter password!"

    $cred = get-credential -Credential $Username
    $cred | export-clixml $credfile   
$cred = import-clixml $credfile

Invoke-Command -ComputerName $Servers -credential $cred -ScriptBlock {

    $session = New-Object -ComObject "Microsoft.Update.Session"
    $searcher = $session.CreateUpdateSearcher()

    $result = $searcher.Search("IsInstalled=0 and Type='Software'")

    if ($result.Updates.Count -gt 0) {

        $processes = (Get-CimInstance -ClassName "Cim_OperatingSystem").PSWindowsUpdate | Where-Object { $_.State -eq 'Downloading' -or $_.State -eq 'Installing' }
        if ($processes){
            Write-Output "$($env:COMPUTERNAME): Currently running Windows Update(s)"

            # check on process 
            $processes = Get-Process CMTProxy -ErrorAction SilentlyContinue

            # Output process information
            if (-NOT $processes) {           
                # process not running
                Write-Output "$($env:COMPUTERNAME): No running process, starting Windows Update(s)"

                # Create an update installer object and start the installation
                If ($null -eq (Get-Module -Name PSWindowsUpdate -ListAvailable) ) {
                    Install-PackageProvider -Name NuGet -MinimumVersion -Force
                    Install-Module PSWindowsUpdate -Force
                    Import-Module PSWindowsUpdate
                    Install-WindowsUpdate -AcceptAll -AutoReboot

            } else {
                Write-Output "$($env:COMPUTERNAME): Windows updates are available, but worker is busy."

    } else {
        # No updates available !!
        $updates = Invoke-Command  -ScriptBlock { Get-WmiObject -Class Win32_QuickFixEngineering }

        # Check if any updates are installed
        if ($updates.Count -gt 0) {
            # Check if a reboot is required
            $rebootRequired = $false
            foreach ($update in $updates) {
                if ($update.RebootRequired) {
                    $rebootRequired = $true

            if ($rebootRequired) {
                Write-Host "$($env:COMPUTERNAME): A reboot is required."

    $wprocesses = Get-Process $Processname -ErrorAction SilentlyContinue
    if (-NOT $wprocesses) {           
            # Application not running
            Write-Output "$($env:COMPUTERNAME): Attention: No running converter!!!!!"

Hits: 438

By angioni

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.