locked
Issue with Unregistered-ScheduledTask with -TaskPath RRS feed

  • Question

  • I have a script that will check for a Scheduled Task.  If it finds it, it will unregisters  it (ultimately will unregisters it and recreates it  with new parameters).  If it doesnt find it, it will create it.   If it does not exists, the code creates it with no issue.  If it does find it, the unregister command runs, but it errors out.  The error points to an issue with the -TaskPath parameter.  It states that it does not find the Task at microsoft\\windows.  I have tried both passing the path as a variable and typing the value out.  Both ways, it is adding an extra '\'  What would be causing the Unregister-ScheduledTask  to add an extra slash?  The command Register-ScheduledTask runs fine.  Any help is appreciated.

    $VDIPre = ((Read-Host -Prompt 'Input VDI Pool Computer Name Prefix') + "*")
    
    Write-Host "Computers are as follows:"
    $Computers = Get-ADComputer -Filter {Name -like $vdipre} | Select Name 
    
    foreach ($CompName in $Computers.Name) {
       Write-Host "C0nnecting to $CompName"
    
     
    ###################################################
    ##                                                 ##
    ##       Create Reboot Schedule                    ##
    ##                                                 ##
     ###################################################
     
        $ScriptBlock = {
            $Action = New-ScheduledTaskAction -Execute shutdown -Argument '/r /t 600'
            $Trigger = New-ScheduledTaskTrigger -Weekly -WeeksInterval 1 -RandomDelay (New-TimeSpan -Minutes 60) -DaysOfWeek Monday -At 7:00pm
            $Princ = New-ScheduledTaskPrincipal -UserID "LOCALSERVICE" -LogonType ServiceAccount
            $Settings = New-ScheduledTaskSettingsSet
            $TaskPath = "Microsoft\Windows"
            $TaskName = "Test Reboot Schedule"
            $Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings -Principal $Princ
            $TaskExists = Get-ScheduledTask | Where-Object {$_.TaskName -eq $TaskName}
            
            if ($TaskExists) {
                   Write-Host "GPC Reboot Schedule Exists - DELETING"
                   Unregister-ScheduledTask -TaskPath $TaskPath -TaskName $TaskName -Confirm:$false
                } else {
                    Write-Host "GPC Reboot Schedule Does Not Exist - Creating"
                    Register-ScheduledTask  -TaskName $TaskName -TaskPath $TaskPath -InputObject $Task -Force
                         }
                 }
    
                Invoke-Command -ComputerName $CompName -ScriptBlock $Scriptblock
    
       }

    Output and Error

    PS C:\Windows\system32> C:\Scripts\Updates\Set Updates\SchedTest.ps1
    Input VDI Pool Computer Name Prefix: compname-03
    Computers are as follows:
    C0nnecting to compname
    GPC Reboot Schedule Exists - DELETING

    No matching MSFT_ScheduledTask objects found by CIM query for instances of the Root/Microsoft/Windows/TaskScheduler/MSFT_ScheduledTask class on the  CIM server: SELECT * FROM MSFT_ScheduledTask  WHERE ((TaskName LIKE 
    'Test Reboot Schedule')) AND ((TaskPath LIKE 'Microsoft\\Windows')). Verify query parameters and retry.
        + CategoryInfo          : ObjectNotFound: (MSFT_ScheduledTask:String) [Unregister-ScheduledTask], CimJobException
        + FullyQualifiedErrorId : CmdletizationQuery_NotFound,Unregister-ScheduledTask
        + PSComputerName        : VPOALTIRIS-03

    Sunday, February 9, 2020 9:58 PM

All replies

  • Paths do not have double  slashes.

    $taskPath = '\Microsoft\Windows\'

    Must contain ending '\' or a wildcard.

    $taskPath = '\Microsoft\Windows\*'

    or a full task path name name

    Get-ScheduledTask -TaskPath \Microsoft\Windows\WCN\WiFiTask


    \_(ツ)_/

    Sunday, February 9, 2020 10:43 PM