Install updates when nobody is logged in

jellekamma 221 Reputation points
2022-01-12T11:51:29.013+00:00

Hello,

We currently update our systems with the use of a scheduled task which updates the system with the get-wuinstall cmdlets. We have 2 types, manual and automatic updates.
The Automatic runs fine.
However I would like to be able to kick off the manual task when nobody is logged on. How do I do this? the query user commands work fine. But how do I integrate this in my script?

So basically I want to be able to check if there is anybody logged on, if not, I want to kick of my script.

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-01-12T16:02:28.657+00:00

    Do you want to "kick off my script" or cause a scheduled task to run? They aren't the same.

    You can use Start-ScheduledTask if you're going to start a task.


  2. Limitless Technology 39,931 Reputation points
    2022-01-12T21:13:53.71+00:00

    Hello @jellekamma

    You can use the next snippet for that purpose:

    function Get-RemoteLogonStatus{  
        [CmdletBinding()]  
        param(  
            [string]$ComputerName = 'LocalHost'  
            )  
      
        if( Test-Connection -ComputerName $ComputerName -Count 2 -Quiet ){  
            try{  
                Invoke-Command -ScriptBlock { quser } -ComputerName $ComputerName -ErrorAction Stop | Out-Null  
                }  
            catch{  
                Write-Output 'No user logged in.'  
                return  
                }  
      
            Write-Output 'Computer in use.'  
            }  
        else{  
            Write-Output 'Computer offline.'  
            }  
        }  
    

    Hope this helps with your query,

    ------
    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

  3. jellekamma 221 Reputation points
    2022-01-13T08:13:37.363+00:00

    Hi, thanks for this snippet, How do I combine it my script? I basically need a "oneliner" which combines your snippet, when the output is No user logged in I need to it to run Start-ScheduledTask -TaskName ""Start task X"

    I run this command manually.

    So my current task is something simple like running a powershell command directly. I Still want that But I want my script to run only if no one is logged on. So my script runs locally. Not via remote powershell.

    The only thing I do via remote powershell will be starting the task.

    0 comments No comments

  4. jellekamma 221 Reputation points
    2022-01-13T09:02:44.413+00:00

    I also tried this, but the task starts on both computers in the txt, one of them had a logged in session.

    if( Test-Connection -ComputerName (get-content c:\temp\patch.txt) -Count 2 -Quiet ){
    try{
    Invoke-Command -ScriptBlock { quser } -ComputerName (get-content c:\temp\patch.txt) -ErrorAction Stop | Out-Null
    }
    catch{
    Invoke-Command -ScriptBlock { Start-ScheduledTask -TaskName "patch task" } -ComputerName (get-content c:\temp\patch.txt) -ErrorAction Stop | Out-Null
    return
    }

                  Write-Output 'Computer in use.'
         }
     else{
         Write-Output 'Computer offline.'
         }
    
    0 comments No comments

  5. MotoX80 36,401 Reputation points
    2022-01-13T17:03:24.21+00:00

    However I would like to be able to kick off the manual task when nobody is logged on.

    Start the script normally. But put a delay at the beginning to wait until the user logs off.

        $wait = $true 
        $count = 0
        $waittime = 60             # How long to wait between checks  
        $limit = 10                # how many times to test     
        while ($Wait) {
            $q = quser.exe 2>$null
            if ($q) {
                $q 
                $count++
                if ($count -gt $limit) {
                    "Wait limit exceeded. Giving up. Maybe notify someone that this process didn't work."
                    return 
                }
                Start-Sleep -Seconds $waittime
            } else {
                "No user is logged on.  We can now proceed."
                $wait = $false 
            }
        }
        "We can patch now."
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.