Task creation issue and login issue

Topics: Errors, Setup
Dec 23, 2015 at 6:07 AM
What is happening and I don't understand is when the user installs my app it doesn't use the user name but goes to "System" and with the settings "Run whether user is logged on or not."

When I delete the task using your code which works and I re create the task using the code below It then create the task using my Login name instead of System and is set to Run only when user is logged on.


The task is created at first run in my program.
I don't understand why this is happening.


Here is the code that I am using. is below
Using ts As New TaskService

            ' Create a new task definition and assign properties
            Dim td As TaskDefinition = ts.NewTask
            td.RegistrationInfo.Description = "Test Startup"
            'Create trigger that fires 1 minutes after the system starts.
            td.Principal.UserId = Environment.UserName
            td.Principal.LogonType = TaskLogonType.InteractiveToken
            td.Triggers.Add(New LogonTrigger())
            td.Actions.Add(New ExecAction("C:\test.exe")
            td.Principal.RunLevel = TaskRunLevel.Highest
            td.Settings.Compatibility = TaskCompatibility.V2_1
            ts.RootFolder.RegisterTaskDefinition("test app", td)

        End Using
thank you for your help and support
Brock
Dec 23, 2015 at 12:40 PM
also forgot to mention I'm running windows 10 Pro 64 bit and the latest version for you DLL I believe its 2,5,8 I think.
Coordinator
Dec 23, 2015 at 3:24 PM
When no user information is provided to the RegisterTaskDefinition method, it first looks to those provided to the Principal property and then uses the credentials of those provided to the TaskService constructor. It cannot give privileges higher than those provided to the TaskService instance. When no user information is provided to the TaskService constructor, it uses those of the executing assembly. Unless you have specifically setup a manifest file for your executable or run it using "Run as Administrator", then UAT will step in and reduce the account privileges dramatically.

I believe the following will correct your code:
' Create a new task definition and assign properties
Dim td As TaskDefinition = TaskService.Instance.NewTask
td.RegistrationInfo.Description = "Test Startup"
'Create trigger that fires 1 minutes after the system starts.
Dim lt As LogonTrigger = New LogonTrigger()
lt.Delay = TimeSpan.FromMinutes(1)
'Set trigger to only fire for this account so elevated permissions are not needed
lt.UserId = Environment.UserName
td.Triggers.Add(lt)
'The Add method assumes an ExecAction when the parameters are strings
td.Actions.Add("C:\test.exe")
td.Principal.RunLevel = TaskRunLevel.Highest
td.Settings.Compatibility = TaskCompatibility.V2_1
TaskService.Instance.RootFolder.RegisterTaskDefinition("test app", td)
Marked as answer by dahall on 12/30/2015 at 8:03 AM
Dec 24, 2015 at 3:30 AM
What does this Exception mean ?

(17,8):userId:

I'm getting it at creation of a new task.
Dec 24, 2015 at 6:11 PM
Here is the exception text. I've even tried my old code and still get the error after my program is installed.
System.Runtime.InteropServices.COMException (0x80070534): (17,8):UserId:
   at Microsoft.Win32.TaskScheduler.V2Interop.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TaskLogonType LogonType, Object sddl)
   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition, TaskCreation createType, String UserId, String password, TaskLogonType LogonType, String sddl)
   at Microsoft.Win32.TaskScheduler.TaskFolder.RegisterTaskDefinition(String Path, TaskDefinition definition)
Coordinator
Dec 24, 2015 at 9:18 PM
My guess is that there is something wrong with the format of the supplied LogonTrigger.UserId property value. The following is information from the Microsoft support page:
The identifier of the user. For example, "MyDomain\MyName" or for a local account, "Administrator".
This property can be in one of the following formats:
• User name or SID: The task is started when the user logs on to the computer.
• Group name or SID string: The task is started when a member of the user group logs on to the computer.
• NULL: The task is started when any user logs on to the computer.
Marked as answer by dahall on 12/30/2015 at 8:03 AM
Mar 23, 2016 at 2:02 AM
dahall wrote:
When no user information is provided to the RegisterTaskDefinition method, it first looks to those provided to the Principal property and then uses the credentials of those provided to the TaskService constructor. It cannot give privileges higher than those provided to the TaskService instance. When no user information is provided to the TaskService constructor, it uses those of the executing assembly. Unless you have specifically setup a manifest file for your executable or run it using "Run as Administrator", then UAT will step in and reduce the account privileges dramatically.

I believe the following will correct your code:
' Create a new task definition and assign properties
Dim td As TaskDefinition = TaskService.Instance.NewTask
td.RegistrationInfo.Description = "Test Startup"
'Create trigger that fires 1 minutes after the system starts.
Dim lt As LogonTrigger = New LogonTrigger()
lt.Delay = TimeSpan.FromMinutes(1)
'Set trigger to only fire for this account so elevated permissions are not needed
lt.UserId = Environment.UserName
td.Triggers.Add(lt)
'The Add method assumes an ExecAction when the parameters are strings
td.Actions.Add("C:\test.exe")
td.Principal.RunLevel = TaskRunLevel.Highest
td.Settings.Compatibility = TaskCompatibility.V2_1
TaskService.Instance.RootFolder.RegisterTaskDefinition("test app", td)
Hi dahl,

I'm still having the issue Where the task is registering the as System and not the username, I am using the code you supplied but still no go.

When people install the App it asks for UAC prompt so it does install under administrator. but when I check the task it says system. If I restart my app and register the task it works as it should with the username and not SYSTEM.

It seems at install I'm having the issue. I'm using the a manifest file. I'll post it as well for you too see., It just a simple msi installer.
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
        <!-- UAC Manifest Options
            If you want to change the Windows User Account Control level replace the 
            requestedExecutionLevel node with one of the following.

        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

            Specifying requestedExecutionLevel node will disable file and registry virtualization.
            If you want to utilize File and Registry Virtualization for backward 
            compatibility then delete the requestedExecutionLevel node.
        -->
        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
      </requestedPrivileges>
    </security>
  </trustInfo>
  
  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
    <application>
      <!-- A list of all Windows versions that this application is designed to work with. Windows will automatically select the most compatible environment.-->

      <!-- If your application is designed to work with Windows 7, uncomment the following supportedOS node-->
      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>-->
      
    </application>
  </compatibility>
  
  <!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
  <!-- <dependency>
    <dependentAssembly>
      <assemblyIdentity
          type="win32"
          name="Microsoft.Windows.Common-Controls"
          version="6.0.0.0"
          processorArchitecture="*"
          publicKeyToken="6595b64144ccf1df"
          language="*"
        />
    </dependentAssembly>
  </dependency>-->

</asmv1:assembly>


thank you for your help
Brock
Coordinator
Mar 28, 2016 at 3:42 PM
Do you want the task to run when any user logs on or just when the current user logs on? If you're doing something like having some code check for updates or ensure something is running in the user session, then I would suggest you create the task as part of your installation that will run with elevate permissions.