locked
schtasks.exe within powershell script keeps throwing ERROR RRS feed

  • Question

  • Hi.

    I am running the following part within a script and i have tried a bunch of ways to make it stop showing the error however i'm currently failing to get rid of it.

    $taskCheck = schtasks.exe /query /s "ComputerName" /v /tn "SomeTaskName"
                        if ($taskCheck)
                        {
                            Write-Host "Task already present"
                            pause
                        }
                        else
                        {
                            Write-Host "Task Not present"
                            pause
                        }

    So whenever there is not a task present (which is actually what i want) it throws the error: ERROR: The system cannot find the file specified.


    I have tried to set the $ErrorActionPreference = 'SilentlyContinue' and the error no longer shows if I run the script in Powershell ISE without saving it. But when I run it normally it shows the error (although it is not red but just white and only has the "ERROR: The system cannot find the file specified.")

    I have to use the schtasks.exe - I cannot use the get-scheduledtask unfortunately

    I can continue through the error but it really annoys me that it still shows up and makes the output look ugly in the script.

    Any suggestions would be greatly appreciated!

    Sunday, June 2, 2019 12:04 AM

Answers

  • if(schtasks.exe /query /s "ComputerName" /v /tn "SomeTaskName" 2>null){
        Write-Host "Task already present"
    }else{
        Write-Host "Task Not present"
    }
    pause
    


    \_(ツ)_/

    • Proposed as answer by Hari VS_Kid Sunday, June 2, 2019 3:54 AM
    • Unproposed as answer by spadge1989 Sunday, June 2, 2019 10:54 AM
    • Marked as answer by spadge1989 Sunday, June 2, 2019 12:07 PM
    Sunday, June 2, 2019 1:25 AM
  • Note that you're just testing if $taskcheck is an empty string or not, which works fine.  schtasks doesn't return a true/false boolean to powershell like test-path.  You can also test $? or $LASTEXITCODE after running an external command like schtasks

    schtasks.exe /query /s "ComputerName" /v /tn "SomeTaskName" *> $null
    if ($?) {
    Write-Host "Task already present"
    pause
    }
    else {
    Write-Host "Task Not present"
    pause
    }
    • Edited by JS2010 Sunday, June 2, 2019 6:46 PM
    • Marked as answer by spadge1989 Sunday, June 2, 2019 8:11 PM
    Sunday, June 2, 2019 3:20 PM

All replies

  • if(schtasks.exe /query /s "ComputerName" /v /tn "SomeTaskName" 2>null){
        Write-Host "Task already present"
    }else{
        Write-Host "Task Not present"
    }
    pause
    


    \_(ツ)_/

    • Proposed as answer by Hari VS_Kid Sunday, June 2, 2019 3:54 AM
    • Unproposed as answer by spadge1989 Sunday, June 2, 2019 10:54 AM
    • Marked as answer by spadge1989 Sunday, June 2, 2019 12:07 PM
    Sunday, June 2, 2019 1:25 AM
  • I tried this and still the same result. When the task is not present it throws the ERROR, doesn't stop the script from doing anything but presents an ugly line in the output.
    Sunday, June 2, 2019 10:35 AM
  • Works fine for me.  The error goes to the null device and the output is empty.

    See:

    PS D:\scripts> if(schtasks.exe /query /s "ComputerName" /v /tn "SomeTaskName" 2>null){
    >>     Write-Host "Task already present"
    >> }else{
    >>     Write-Host "Task Not present"
    >> }
    Task Not present
    PS D:\scripts>
    
    


    \_(ツ)_/


    • Edited by jrv Sunday, June 2, 2019 11:24 AM
    Sunday, June 2, 2019 11:23 AM
  • If i run it in powershell directly it works fine. Its when i run it from the .ps1 script it throws the "ERROR: The system cannot find the file specified." Works fine in ISE as long as i don't save it too.
    Sunday, June 2, 2019 11:33 AM
  • Again - it works just fine for me in a ps1 file:

    PS D:\scripts> .\test.ps1
    Task Not present
    PS D:\scripts>
    


    \_(ツ)_/

    Sunday, June 2, 2019 11:39 AM
  • It worked. It was my fault i forgot that i had left the Variable in earlier in my main script that was throwing the error and not the if statement.
    Sunday, June 2, 2019 12:09 PM
  • If you need a variable then just assign it like this:

     if($taskinfo = schtasks.exe /query /s "ComputerName" /v /tn "SomeTaskName" 2>null){
         Write-Host "Task already present"
    }else{
       Write-Host "Task Not present"
    }
    
    

    It will always get reset when the line runs.


    \_(ツ)_/

    Sunday, June 2, 2019 12:20 PM
  • Note that you're just testing if $taskcheck is an empty string or not, which works fine.  schtasks doesn't return a true/false boolean to powershell like test-path.  You can also test $? or $LASTEXITCODE after running an external command like schtasks

    schtasks.exe /query /s "ComputerName" /v /tn "SomeTaskName" *> $null
    if ($?) {
    Write-Host "Task already present"
    pause
    }
    else {
    Write-Host "Task Not present"
    pause
    }
    • Edited by JS2010 Sunday, June 2, 2019 6:46 PM
    • Marked as answer by spadge1989 Sunday, June 2, 2019 8:11 PM
    Sunday, June 2, 2019 3:20 PM