none
Need to convert Power Shell script to VB .net RRS feed

  • Question

  • Hi,

    I need to check if windows scheduled task is running and return true of false.

    I have the follwing PS to do it (I can run the PS from VB), but I think it will be more elegant to write it in VB,  but I can't find any VB .net command or lib for that.

    if (((Get-ScheduledTask | where taskname -EQ 'XblGameSaveTask').State) -eq 'Running') {0} else {1}

    please help.

    thanks,

    Benny

    Thursday, December 21, 2017 12:25 PM

Answers

  • You can generate and modify the code with WMICodeCreator

    For example =>

    Try
        Dim searcher As New ManagementObjectSearcher("root\Microsoft\Windows\TaskScheduler",
                "SELECT * FROM MSFT_ScheduledTask WHERE TaskName = 'XblGameSaveTask'")
        For Each queryObj As ManagementObject In searcher.Get()
            If (queryObj("State") = 4) Then
                ' Code for Running state
            End If
        Next
    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try

    • Marked as answer by bennymi Thursday, December 21, 2017 9:08 PM
    Thursday, December 21, 2017 1:15 PM
  • do you know what lib I need to import? the ManagementObject & ManagementObjectSearcher are not defined.

    Imports System.Management


    • Marked as answer by bennymi Thursday, December 21, 2017 9:07 PM
    Thursday, December 21, 2017 7:22 PM

All replies

  • You can generate and modify the code with WMICodeCreator

    For example =>

    Try
        Dim searcher As New ManagementObjectSearcher("root\Microsoft\Windows\TaskScheduler",
                "SELECT * FROM MSFT_ScheduledTask WHERE TaskName = 'XblGameSaveTask'")
        For Each queryObj As ManagementObject In searcher.Get()
            If (queryObj("State") = 4) Then
                ' Code for Running state
            End If
        Next
    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try

    • Marked as answer by bennymi Thursday, December 21, 2017 9:08 PM
    Thursday, December 21, 2017 1:15 PM
  • Thank you for your answer.

    That's a great tool..

    do you know what lib I need to import? the ManagementObject & ManagementObjectSearcher are not defined.

    Thanks,

    Benny

    You can generate and modify the code with WMICodeCreator

    For example =>

    Try
        Dim searcher As New ManagementObjectSearcher("root\Microsoft\Windows\TaskScheduler",
                "SELECT * FROM MSFT_ScheduledTask WHERE TaskName = 'XblGameSaveTask'")
        For Each queryObj As ManagementObject In searcher.Get()
            If (queryObj("State") = 4) Then
                ' Code for Running state
            End If
        Next
    Catch err As ManagementException
        MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
    End Try


    Thursday, December 21, 2017 5:59 PM
  • do you know what lib I need to import? the ManagementObject & ManagementObjectSearcher are not defined.

    Imports System.Management


    • Marked as answer by bennymi Thursday, December 21, 2017 9:07 PM
    Thursday, December 21, 2017 7:22 PM
  •  You should also be disposing the new instance of the ManagementObjectSearcher after you have acquired the information you want and are done using it.  It would be handier to use a Using / End Using statement to do it.

    searcher.Dispose()


    If you say it can`t be done then i`ll try it

    Thursday, December 21, 2017 9:27 PM