none
Task Scheduler query returns result in localized language RRS feed

  • Question

  • I have created a task in windows task scheduler and have schduled it for particular day and time programmatically in C#. The OS language is German. It is localized. When I query the task by running the following command from my application: schtasks.exe /query /v /fo LIST /tn "<TaskName>"

    It returns the details of the task but in German language and not in english. How can I get the details in English language and not in localized language?


    Himanshu Saxena

    Thursday, April 19, 2018 9:17 AM

All replies

  • If your OS is in German then that is what task scheduler is going to run as and therefore the app in question. You cannot automagically translate arbitrary text from one language to another so you're going to get back Germany. If you want the task to run in English (and the appropriate language pack is installed) then you'd need to configure the process to run in English. Most apps don't support that out of the box. If it is your own app then you can force English in your code.

    The only other alternative is to get the results in German and then call out to a translation service to translate the message as best as possible.


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 19, 2018 5:10 PM
    Moderator
  • My application is developed in C# in English Language. It does not support localization. This application runs a command using System.Diagnostics.Process class to create a task in windows task scheduler. The OS on customer's machine is localized in German Language. The task is created in English. I could verify this by opening the task scheduler. When I run another query to get task details from my application using the same System.Diagnostics.Process class, it returns the result in German instead of English. I cannot rely on the online service since the customer might run my application on a machine that does not have an internet connection.

    Himanshu Saxena

    I
    • Edited by hsanshu Friday, April 20, 2018 9:45 AM
    Friday, April 20, 2018 9:44 AM
  • If the process that is running is English then any strings it generates will be in English. There is no auto-translation layer in .NET. However the framework itself is localized so if you run it on a non-English machine then the framework strings will be localized to the Windows culture.

    Please provide an example of the complete output you're getting where you're seeing German from your English-only app. Also post the code you're using to get that output.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 20, 2018 1:57 PM
    Moderator
  • Hi,

    Here is the xml that I use to create the task in task scheduler:

    <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
      <!--Defines the xml used to create a scheduled task-->
      <RegistrationInfo>
        <Date>2018-03-09T12:15:23</Date>
        <Author>Download Manager </Author>
        <Description>Configuring updates for product releases</Description>
      </RegistrationInfo>
      <Triggers>
        <CalendarTrigger>
          <StartBoundary>2018-03-09T08:08:00</StartBoundary>
          <Enabled>true</Enabled>
          <ScheduleByWeek>
            <DaysOfWeek>
              <Friday />
            </DaysOfWeek>
            <WeeksInterval>4</WeeksInterval>
          </ScheduleByWeek>
        </CalendarTrigger>
      </Triggers>
      <Principals>
        <Principal id="Author">
          <RunLevel>HighestAvailable</RunLevel>
        </Principal>
      </Principals>
      <Settings>
        <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
        <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
        <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
        <AllowHardTerminate>true</AllowHardTerminate>
        <StartWhenAvailable>false</StartWhenAvailable>
        <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
        <IdleSettings>
          <Duration>PT10M</Duration>
          <WaitTimeout>PT1H</WaitTimeout>
          <StopOnIdleEnd>true</StopOnIdleEnd>
          <RestartOnIdle>false</RestartOnIdle>
        </IdleSettings>
        <AllowStartOnDemand>true</AllowStartOnDemand>
        <Enabled>true</Enabled>
        <Hidden>false</Hidden>
        <RunOnlyIfIdle>false</RunOnlyIfIdle>
        <WakeToRun>false</WakeToRun>
        <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
        <Priority>7</Priority>
      </Settings>
      <Actions Context="Author">
        <Exec>
          <Command>C:\temp\DwmUpdatecheck.exe</Command>
          <WorkingDirectory>C:\temp</WorkingDirectory>
        </Exec>
      </Actions>
    </Task>

    Here is the code to create the scheduled task:

    string strScheduledTaskArgument = "/Create /TN " + "\"" + Constants.UPDATE_TASK_NAME + "_" + username + "\"" + " /xml ";
                    
    string AppdataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                                          
    strScheduledTaskArgument = strScheduledTaskArgument + "\"" + System.IO.Path.Combine(AppdataPath, "SchTask.xml");
                           
    Utilities.StartProcess("SCHTASKS.exe", strScheduledTaskArgument, true);
    

    This creates  the task in English.

    Here is athe code to read the task:

    username = (username.IndexOf("\\") != -1) ? username.Substring(username.IndexOf("\\") + 1) : username;
    
    string taskNameToBeSearched = Constants.UPDATE_TASK_NAME + "_" + username;
    
    string schtasksArgument = "/query /v /fo LIST /tn " + "\"" + taskNameToBeSearched + "\"";
    
    Process process = new Process();
    process.StartInfo.FileName = "cmd.exe";
    process.StartInfo.Arguments = "/c schtasks.exe" + schtasksArgument; 
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    process.Start();
    process.WaitForExit();
    // Read the output (or the error)
    string output = process.StandardOutput.ReadToEnd();
    string[] stringSeparators = new string[] { "\r\n" };
    string[] lines = output.Split(stringSeparators, StringSplitOptions.None);
    Logger.WriteToLog("Output from schtasks.exe ");
    for (int ctr = 0; ctr < lines.Length; ctr++)
    {
        Logger.WriteToLog("Line " + ctr + " : " + lines[ctr]);
    }

    On an English OS, the query returns an array of string with the task details in English.
    Whereas on a German OS, the output is as follows:

    Output from schtasks.exe 
    Line 0 : 
    Line 1 : Ordner: \
    Line 2 : Hostname:                                                    WS005
    Line 3 : Aufgabenname:                                                \MyTask_aaa.bbb
    Line 4 : N„chste Laufzeit:                                            16.04.2018 10:10:00
    Line 5 : Status:                                                      Bereit
    Line 6 : Anmeldemodus:                                                Nur interaktiv
    Line 7 : Letzte Laufzeit:                                             10.04.2018 10:10:00
    Line 8 : Letztes Ergebnis:                                            0
    Line 9 : Autor:                                                       Download Manager
    Line 10 : Auszufhrende Aufgabe:                                       C:\temp\DwmUpdatecheck.exe 
    Line 11 : Starten in:                                                  C:\temp
    Line 12 : Kommentar:                                                   Configuring updates product Releases
    Line 13 : Status der geplanten Aufgabe:                                Aktiviert
    Line 14 : Leerlaufzeit:                                                Deaktiviert
    Line 15 : Energieverwaltung:                                           Bei Batteriebetrieb beenden, Bei Batteriebetrieb nicht starten
    Line 16 : Als Benutzer ausfhren:                                      Domain\aaa.bbb
    Line 17 : Aufgabe l”schen, falls nicht verlegt:                        Aktiviert
    Line 18 : Aufgabe beenden, falls sie X St. und X Min. ausgefhrt wird: 72:00:00
    Line 19 : Zeitplan:                                                    Zeitplaninformationen sind in diesem Format nicht verfgbar.
    Line 20 : Zeitplantyp:                                                 W”chentlich
    Line 21 : Startzeit:                                                   10:10:00
    Line 22 : Startdatum:                                                  03.04.2018
    Line 23 : Enddatum:                                                    Nicht zutreffend
    Line 24 : Tage:                                                        MO, DI
    Line 25 : Monate:                                                      Alle 1 Woche(n)
    Line 26 : Wiederholung: Jede(n):                                       Deaktiviert
    Line 27 : Wiederholung: Bis: Zeit:                                     Deaktiviert
    Line 28 : Wiederholung: Bis: Dauer:                                    Deaktiviert
    Line 29 : Wiederholung: Beenden, falls immer noch ausgefhrt:          Deaktiviert
    Line 30 : 

    Regards

    Himanshu


    Himanshu Saxena


    • Edited by hsanshu Monday, April 23, 2018 7:43 AM
    Monday, April 23, 2018 7:41 AM
  • That output is correct. The various fields (before the colon) are in the localized language of Windows because they come from Windows. The values however would be from your app and mostly should be in English (because you are probably writing them in English). However any messages that come from the system (say exceptions) would be in German since you likely have the .NET language pack installed.  Some of the messages are in German as well which leads me to believe they are coming from the system.

    The best solution, in my opinion, is to force the task to use English. Since it is a scheduled task it shouldn't matter but to do that you'd set the UICulture and Culture properties of the thread to English.

    //Do this early in your scheduled task startup code
    var englishCulture = new CultureInfo("en-US");
    
    //.NET 4.5 and above for all threads
    CultureInfo.DefaultThreadCurrentCulture  = englishCulture;
    
    //I'd start with not specifying this in case it is run in the UI directly - if your messages are still coming out in German when querying the task then set it
    //CultureInfo.DefaultThreadCurrentUICulture = englishCulture;

    Now it shouldn't matter what culture the app is run under. 


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, April 23, 2018 2:03 PM
    Moderator
  • Hi,

    Thanks for your reply. Unfortunately it does not work. It still returns the task details in localized language. What else can I try?

    Regards


    Himanshu Saxena

    Thursday, April 26, 2018 10:38 AM
  • If your app you're running on the remote machine is configured for English then it will produce English results. The Task Scheduler (TSAPI) may return its info in German (because you cannot control its localization) but your messages are English. If they aren't then that would seem to indicate something wasn't deployed properly. There is no way your app would produce non-English messages if it isn't configured to do so. You should be able to easily verify this by going to that remote machine and running your app from the command line. It should be in English.

    Again, however, note that Windows itself is German so any messages coming from the OS will still be localized. This includes some of the info coming back to you from TSAPI. There isn't anything you can do about that short of running on an English Windows machine. This would the information about the times, runtime version, etc. 


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, April 26, 2018 1:37 PM
    Moderator
  • Can you please try writing a sample application from the code that I have provided and see if setting thread culture resolves the issue on a localized OS. I am unable to fix the issue.

    Regards


    Himanshu Saxena

    Friday, April 27, 2018 10:41 AM
  • I'm afraid I don't have a localized version of Windows to test. As I mentioned before some of the data is going to come back in German since that is the OS it is running on. This includes any messages/text from the OS and framework. Only your English-only messages would be in English. This is by design.

    The history of a task is stored in the event viewer. The event viewer is localized to the target platform. You can confirm that the information is correct by reading the event log. Note that there was a question a while back about getting the info from the event viewer such that it isn't localized and the concensus is that it isn't possible (unless the event entry was written using message resources) so, again, only your English messages are going to be in English.

    https://superuser.com/questions/921096/how-to-get-windows-event-logs-in-english-though-the-default-language-is-spanish

    https://stackoverflow.com/questions/18076382/how-can-i-always-get-windows-eventlog-information-in-english

    Perhaps someone else can provide you a better solution but my gut instinct is that only your apps specific text will be in English. Of course I'm assuming you aren't shipping a German resource file with your app.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 27, 2018 1:52 PM
    Moderator