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.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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.
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.
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--
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.
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.'
}
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."