none
Querying WMI in order to get a Windows Task in my local box RRS feed

  • Question

  • Hi there,

    My aim is just check one Windows Tasks scheduled in my laptop and see if it ran or not  from VSTA (one of my SSIS packages)

    The name of this Windows Task is "Daily_R2"

    any input would be greatly appreciated

    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Runtime;
    using System.Windows.Forms;
    using System.Management;

    string wmiQuery = string.Format("SELECT Name, caption FROM Win32_ScheduledJob WHERE Name LIKE '{0}'","Daily_R2"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery); ManagementObjectCollection retObjectCollection = searcher.Get(); foreach (ManagementObject retObject in retObjectCollection) { Console.WriteLine("[{0}]\tName: {1}", retObject["Name"], retObject["caption"]); }

                Dts.TaskResult = (int)ScriptResults.Success;

    So, debugging this snippet of code never ever enter in foreach

    Friday, April 3, 2020 2:32 PM

Answers

  • Win32_ScheduledJOb is the wrong type. If you refer to the documentation for this it explicitly states this is not a scheduled task in Task Scheduler. This is the old pre-TS object that was deprecated a long time ago. Hence you won't find any scheduled tasks in here. You can use WMI Code Creator to quickly query WMI and generate the code you want.

    The recommended approach for working with scheduled tasks is using the Task Scheduler API. This example shows you how to do that. All you need to do is add a reference to the TaskScheduler COM object in your project, grab an instance of TaskService and then you can query. Alternatively a lot of people have created wrappers around this API so you can just use theirs.

    At this time I'm not aware of any WMI object that can get scheduled tasks from the Task Scheduler.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Enric Vives Tuesday, May 12, 2020 9:10 AM
    Friday, April 10, 2020 1:45 PM
    Moderator

All replies

  • Did you check if it displays the names after removing the WHERE part?

    Friday, April 3, 2020 5:03 PM
  • Hi Viorel, thanks for your reply.

    Even removing the criteria nothing is returned

    Saturday, April 4, 2020 9:59 AM
  • Hi Enric Vives, 
    Based on your description,I suggest you can refer to IAbstract's answer.
    And here is a document about querying WMI you can refer to.
    Hope this could be helpful.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 10, 2020 7:55 AM
  • Win32_ScheduledJOb is the wrong type. If you refer to the documentation for this it explicitly states this is not a scheduled task in Task Scheduler. This is the old pre-TS object that was deprecated a long time ago. Hence you won't find any scheduled tasks in here. You can use WMI Code Creator to quickly query WMI and generate the code you want.

    The recommended approach for working with scheduled tasks is using the Task Scheduler API. This example shows you how to do that. All you need to do is add a reference to the TaskScheduler COM object in your project, grab an instance of TaskService and then you can query. Alternatively a lot of people have created wrappers around this API so you can just use theirs.

    At this time I'm not aware of any WMI object that can get scheduled tasks from the Task Scheduler.


    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by Enric Vives Tuesday, May 12, 2020 9:10 AM
    Friday, April 10, 2020 1:45 PM
    Moderator