Skip to content

Return service trigger info in Get-Service #13012

Closed

Description

Get-ScheduledTask returns task trigger info, but Get-Service does not return service trigger info. How are we supposed to get this information in Powershell?

It appears that WMI's Win32_Service object needs to have the trigger info added

Activity

  1. added
    Issue-Enhancement
    on Jun 23, 2020
  2. iSazonov commented on Jun 23, 2020

    @iSazonov
    Collaborator

    We use QueryServiceConfig2W in https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs#L657.
    The function can be used to get the trigger info.

    GitHub
    PowerShell for every system! Contribute to PowerShell/PowerShell development by creating an account on GitHub.
  3. added
    First-Time-Issue
    Up-for-Grabs
    on Jun 23, 2020
  4. kvprasoon commented on Jul 2, 2020

    @kvprasoon
    Contributor

    Ilya (@iSazonov) thanks for the tip, I'm in for this.

  5. BlakeWills commented on Jul 26, 2020

    @BlakeWills

    PRASOON KARUNAN V (@kvprasoon) it's been a while since you commented so hopefully it was safe to assume you aren't looking at this? If you are then I'm sorry for treading on your toes, but hopefully the questions below are useful. :)
    If not Ilya (@iSazonov) could I be assigned to this please?

    I've got a working implementation that retrieves the triggers, but before I submit a PR I'd like some guidance on how we should add them to the output, please.

    Firstly, any outputs that aren't already properties on the ServiceController object are added as NoteProperties to the PSObject. So far, all additional outputs are just single values, triggers would need to be an array of PSObjects. Is this acceptable? I did have a look at Get-ScheduledTask which returns the triggers as an array of CimInstance objects, but I don't think that helps us here.

    Each trigger has four properties: TriggerType, Subtype, Action and SpecificDataItems. (https://docs.microsoft.com/en-us/windows/win32/api/winsvc/ns-winsvc-service_trigger)

    Questions:

    1. Should we return the TriggerType as an integer or its string representation? I'm currently using the enum below to map to the string value. Another question is if we return the string value, should we trim the "SERVICE_TRIGGER_TYPE" prefix? My preference would be to return the string as defined in the docs, but I'm not sure if there is a precedent already set elsewhere.
    internal enum ServiceTriggerType : byte
    {
    	SERVICE_TRIGGER_TYPE_CUSTOM = 20,
    	SERVICE_TRIGGER_TYPE_DEVICE_INTERFACE_ARRIVAL = 1,
    	SERVICE_TRIGGER_TYPE_DOMAIN_JOIN = 3,
    	SERVICE_TRIGGER_TYPE_FIREWALL_PORT_EVENT = 4,
    	SERVICE_TRIGGER_TYPE_GROUP_POLICY = 5,
    	SERVICE_TRIGGER_TYPE_IP_ADDRESS_AVAILABILITY = 2,
    	SERVICE_TRIGGER_TYPE_NETWORK_ENDPOINT = 6
    }
    
    1. Not so much of a question, but I think we can just return the guid for the subtype.

    2. Same question as Q1 but for the action, should we return the integer value or string representation? Again, mapping enum below:

    internal enum ServiceTriggerAction : byte
    {
        SERVICE_TRIGGER_ACTION_SERVICE_START = 1,
        SERVICE_TRIGGER_ACTION_SERVICE_STOP = 2
    }
    
    1. Some triggers also have an array of SERVICE_TRIGGER_SPECIFIC_DATA_ITEM, however the type of data that is returned varies depending on the TriggerType. (https://docs.microsoft.com/en-us/windows/win32/api/winsvc/ns-winsvc-service_trigger_specific_data_item)
    • If the TriggerType is SERVICE_TRIGGER_TYPE_DEVICE_INTERFACE_ARRIVAL or SERVICE_TRIGGER_TYPE_NETWORK_ENDPOINT this is a string.
    • If the TriggerType is SERVICE_TRIGGER_TYPE_FIREWALL_PORT_EVENT this is a multi-string containing the port, protocol and potentially the executable path and service name.
    • Finally, if the TriggerType is SERVICE_TRIGGER_TYPE_CUSTOM then the data is defined by the ETW provider.

    Excluding the CUSTOM TriggerType (I'm not sure what to do with this yet and I don't have a service to test), one approach is to return a custom PSObject whose properties depend on the TriggerType, for example:

    • SERVICE_TRIGGER_TYPE_DEVICE_INTERFACE_ARRIVAL would have a single property called "DeviceId"
    • SERVICE_TRIGGER_TYPE_FIREWALL_PORT_EVENT would have "Port", "Protocol", "ExecutablePath" and "ServiceName" properties.
    • SERVICE_TRIGGER_TYPE_NETWORK_ENDPOINT could have a "NetworkEndpoint" property.

    To be honest, I don't really like this as it isn't user-friendly, especially if you're trying to script something and the output changes constantly. However I can't think of a way to standardise it. I'm open to suggestions.

    Also, should we support returning the data items for TriggerType SERVICE_TRIGGER_TYPE_CUSTOM? I think the answer to this may depend on what we do with the others.

    Thank You

    Represents a service trigger event. This structure is used by the SERVICE_TRIGGER_INFO structure.
    Contains trigger-specific data for a service trigger event.
  6. vexx32 commented on Jul 26, 2020

    @vexx32
    Collaborator

    I'm not fully acquainted with the finer details of what you're working with there Blake (@BlakeWills) but regarding the enum naming specifically, I think it'd be much more usable for PowerShell folks if you drew up the enum without the prefix and in PascalCase to match the majority of property/enum naming in .NET; $trigger.ServiceTriggerType.Custom where the ServiceTriggerType just becomes the property name, and the individual trigger types have a PascalCased name. Same for the Action ones.

    The disparity in naming between that and the interop naming can be laid out in docs if it's important, but it feels more on the level of an implementation detail to me at least on the surface. (Definitely at least until folks can use this information to actually set service triggers in future, if that ever happens.)

  7. kvprasoon commented on Jul 26, 2020

    @kvprasoon
    Contributor

    Blake (@BlakeWills) I started working on this, looks like I'm not gonna finish this soon. It's your now :-) and AFAIK, it need not to be assigned to you to work on this.

  8. BrianCatlin commented on Jul 26, 2020

    @BrianCatlin
    Author
  9. iSazonov commented on Jul 27, 2020

    @iSazonov
    Collaborator

    You could look sc.exe qtriggerinfo output to get more experience.

    Because we want only information (not full trigger management) I'd start only with top level.

  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. added
    Resolution-No Activity
    on Nov 16, 2023
  14. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    First-Time-IssueIssue-EnhancementResolution-No ActivityUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions