I'm trying to make a batch file that creates an xml file then imports that file into scheduled tasks, plus a few other things. I know how to import the xml and do everything else I need, but I can't get the creation of the xml file to work right.
Here's the content of the xml file exported from scheduled tasks:
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-01-11T04:56:22.2785148</Date>
<Author>Computer_Name\User</Author>
<URI>\Display Off When Locked (Battery Only)</URI>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
</Exec>
</Actions>
</Task>
And here's the contents of a batch file whose sole purpose is to create an identical xml file:
setlocal enableDelayedExpansion
echo ^<?xml version="1.0" encoding="UTF-16"?^>> task.xml
echo ^<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"^>>> task.xml
echo ^<RegistrationInfo^>>> task.xml
echo ^<Date^>2021-01-11T04:56:22.2785148^</Date^>>> task.xml
echo ^<Author^>Computer_Name\User^</Author^>>> task.xml
echo ^<URI^>\Display Off When Locked (Battery Only)^</URI^>>> task.xml
echo ^</RegistrationInfo^>>> task.xml
echo ^<Triggers^>>> task.xml
echo ^<SessionStateChangeTrigger^>>> task.xml
echo ^<Enabled^>true^</Enabled^>>> task.xml
echo ^<StateChange^>SessionLock^</StateChange^>>> task.xml
echo ^</SessionStateChangeTrigger^>>> task.xml
echo ^</Triggers^>>> task.xml
echo ^<Principals^>>> task.xml
echo ^<Principal id="Author"^>>> task.xml
echo ^<GroupId^>S-1-5-32-544^</GroupId^>>> task.xml
echo ^<RunLevel^>HighestAvailable^</RunLevel^>>> task.xml
echo ^</Principal^>>> task.xml
echo ^</Principals^>>> task.xml
echo ^<Settings^>>> task.xml
echo ^<MultipleInstancesPolicy^>StopExisting^</MultipleInstancesPolicy^>>> task.xml
echo ^<DisallowStartIfOnBatteries^>false^</DisallowStartIfOnBatteries^>>> task.xml
echo ^<StopIfGoingOnBatteries^>true^</StopIfGoingOnBatteries^>>> task.xml
echo ^<AllowHardTerminate^>true^</AllowHardTerminate^>>> task.xml
echo ^<StartWhenAvailable^>false^</StartWhenAvailable^>>> task.xml
echo ^<RunOnlyIfNetworkAvailable^>false^</RunOnlyIfNetworkAvailable^>>> task.xml
echo ^<IdleSettings^>>> task.xml
echo ^<StopOnIdleEnd^>true^</StopOnIdleEnd^>>> task.xml
echo ^<RestartOnIdle^>false^</RestartOnIdle^>>> task.xml
echo ^</IdleSettings^>>> task.xml
echo ^<AllowStartOnDemand^>true^</AllowStartOnDemand^>>> task.xml
echo ^<Enabled^>true^</Enabled^>>> task.xml
echo ^<Hidden^>false^</Hidden^>>> task.xml
echo ^<RunOnlyIfIdle^>false^</RunOnlyIfIdle^>>> task.xml
echo ^<DisallowStartOnRemoteAppSession^>false^</DisallowStartOnRemoteAppSession^>>> task.xml
echo ^<UseUnifiedSchedulingEngine^>true^</UseUnifiedSchedulingEngine^>>> task.xml
echo ^<WakeToRun^>false^</WakeToRun^>>> task.xml
echo ^<ExecutionTimeLimit^>PT1H^</ExecutionTimeLimit^>>> task.xml
echo ^<Priority^>7^</Priority^>>> task.xml
echo ^</Settings^>>> task.xml
echo ^<Actions Context="Author"^>>> task.xml
echo ^<Exec^>>> task.xml
echo ^<Command^>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"^</Command^>>> task.xml
echo ^</Exec^>>> task.xml
echo ^</Actions^>>> task.xml
echo ^</Task^>>> task.xml
endlocal & exit /b
When I view both of them in a text editor, they're identical. But task.xml is only 2KB vs 4KB for the original, and viewing them in WinMerge shows the content in the original but task.xml just shows a bunch of Asian-looking characters on one long line:
㼼浸敶獲潩㵮ㄢ〮•湥潣楤杮∽呕ⵆ㘱㼢ാ㰊慔歳瘠牥楳湯∽⸱∴砠汭獮∽瑨灴⼺猯档浥獡洮捩潲潳瑦挮浯眯湩潤獷㈯〰⼴㈰洯瑩琯獡≫ാ 㰠敒楧瑳慲楴湯湉潦ാ †㰠慄整㈾㈰ⴱㄭ吱㐰㔺㨶㈲㈮㠷ㄵ㠴⼼慄整ാ †㰠畁桴牯䔾协卜整敶⼼畁桴牯ാ †㰠剕㹉䑜獩汰祡传晦圠敨潌正摥⠠慂瑴牥⁹湏祬㰩唯䥒ാ 㰠刯来獩牴瑡潩䥮普㹯†吼楲杧牥㹳††匼獥楳湯瑓瑡䍥慨杮呥楲杧牥ാ ††㰠湅扡敬㹤牴敵⼼湅扡敬㹤†††匼慴整桃湡敧匾獥楳湯潌正⼼瑓瑡䍥慨杮㹥††⼼敓獳潩卮慴整桃湡敧牔杩敧㹲†⼼牔杩敧獲ാ 㰠牐湩楣慰獬ാ †㰠牐湩楣慰摩∽畁桴牯㸢†††䜼潲灵摉匾ㄭ㔭㌭ⴲ㐵㰴䜯潲灵摉ാ ††㰠畒䱮癥汥䠾杩敨瑳癁楡慬汢㱥刯湵敌敶㹬††⼼牐湩楣慰㹬†⼼牐湩楣慰獬ാ 㰠敓瑴湩獧ാ †㰠畍瑬灩敬湉瑳湡散偳汯捩㹹瑓灯硅獩楴杮⼼畍瑬灩敬湉瑳湡散偳汯捩㹹††䐼獩污潬卷慴瑲晉湏慂瑴牥敩㹳慦獬㱥䐯獩污潬卷慴瑲晉湏慂瑴牥敩㹳††匼潴䥰䝦楯杮湏慂瑴牥敩㹳牴敵⼼瑓灯晉潇湩佧䉮瑡整楲獥ാ †㰠汁潬䡷牡呤牥業慮整琾畲㱥䄯汬睯慈摲敔浲湩瑡㹥††匼慴瑲桗湥癁楡慬汢㹥慦獬㱥匯慴瑲桗湥癁楡慬汢㹥††刼湵湏祬晉敎睴牯䅫慶汩扡敬显污敳⼼畒佮汮䥹书瑥潷歲癁楡慬汢㹥††䤼汤卥瑥楴杮㹳†††匼潴佰䥮汤䕥摮琾畲㱥匯潴佰䥮汤䕥摮ാ ††㰠敒瑳牡佴䥮汤㹥慦獬㱥刯獥慴瑲湏摉敬ാ †㰠䤯汤卥瑥楴杮㹳††䄼汬睯瑓牡佴䑮浥湡㹤牴敵⼼汁潬卷慴瑲湏敄慭摮ാ †㰠湅扡敬㹤牴敵⼼湅扡敬㹤††䠼摩敤㹮慦獬㱥䠯摩敤㹮††刼湵湏祬晉摉敬显污敳⼼畒佮汮䥹䥦汤㹥††䐼獩污潬卷慴瑲湏敒潭整灁印獥楳湯显污敳⼼楄慳汬睯瑓牡佴剮浥瑯䅥灰敓獳潩㹮††唼敳湕晩敩卤档摥汵湩䕧杮湩㹥牴敵⼼獕啥楮楦摥捓敨畤楬杮湅楧敮ാ †㰠慗敫潔畒㹮慦獬㱥圯歡呥副湵ാ †㰠硅捥瑵潩呮浩䱥浩瑩倾ㅔ㱈䔯數畣楴湯楔敭楌業㹴††值楲牯瑩㹹㰷倯楲牯瑩㹹†⼼敓瑴湩獧ാ 㰠捁楴湯潃瑮硥㵴䄢瑵潨≲ാ †㰠硅捥ാ ††㰠潃浭湡㹤䌢尺潐瑲扡敬䄠灰屳潍佮晦⠠楄灳慬⁹景桗湥䰠捯敫⥤䵜湯晏䈨瑡整祲传汮⥹戮瑡㰢䌯浯慭摮ാ †㰠䔯數㹣†⼼捁楴湯㹳⼼慔歳ാ
And, of course, this newly created xml doesn't import into task scheduler, which presents the following error:
The format of the task is not valid. The following error was reported: (1,2)::ERROR: incorrect document syntax
Also, HxD shows very different content between the two files:
Original:
ÿþ<�?�x�m�l� �v�e�r�s�i�o�n�=�"�1�.�0�"� �e�n�c�o�d�i�n�g�=�"�U�T�F�-�1�6�"�?�>�
�
�<�T�a�s�k� �v�e�r�s�i�o�n�=�"�1�.�4�"� �x�m�l�n�s�=�"�h�t�t�p�:�/�/�s�c�h�e�m�a�s�.�m�i�c�r�o�s�o�f�t�.�c�o�m�/�w�i�n�d�o�w�s�/�2�0�0�4�/�0�2�/�m�i�t�/�t�a�s�k�"�>�
�
� � �<�R�e�g�i�s�t�r�a�t�i�o�n�I�n�f�o�>�
�
� � � � �<�D�a�t�e�>�2�0�2�1�-�0�1�-�1�1�T�0�4�:�5�6�:�2�2�.�2�7�8�5�1�4�8�<�/�D�a�t�e�>�
�
� � � � �<�A�u�t�h�o�r�>�C�o�m�p�u�t�e�r�_�N�a�m�e�\�U�s�e�r�<�/�A�u�t�h�o�r�>�
�
� � � � �<�U�R�I�>�\�D�i�s�p�l�a�y� �O�f�f� �W�h�e�n� �L�o�c�k�e�d� �(�B�a�t�t�e�r�y� �O�n�l�y�)�<�/�U�R�I�>�
�
� � �<�/�R�e�g�i�s�t�r�a�t�i�o�n�I�n�f�o�>�
�
� � �<�T�r�i�g�g�e�r�s�>�
�
� � � � �<�S�e�s�s�i�o�n�S�t�a�t�e�C�h�a�n�g�e�T�r�i�g�g�e�r�>�
�
� � � � � � �<�E�n�a�b�l�e�d�>�t�r�u�e�<�/�E�n�a�b�l�e�d�>�
�
� � � � � � �<�S�t�a�t�e�C�h�a�n�g�e�>�S�e�s�s�i�o�n�L�o�c�k�<�/�S�t�a�t�e�C�h�a�n�g�e�>�
�
� � � � �<�/�S�e�s�s�i�o�n�S�t�a�t�e�C�h�a�n�g�e�T�r�i�g�g�e�r�>�
�
� � �<�/�T�r�i�g�g�e�r�s�>�
�
� � �<�P�r�i�n�c�i�p�a�l�s�>�
�
� � � � �<�P�r�i�n�c�i�p�a�l� �i�d�=�"�A�u�t�h�o�r�"�>�
�
� � � � � � �<�G�r�o�u�p�I�d�>�S�-�1�-�5�-�3�2�-�5�4�4�<�/�G�r�o�u�p�I�d�>�
�
� � � � � � �<�R�u�n�L�e�v�e�l�>�H�i�g�h�e�s�t�A�v�a�i�l�a�b�l�e�<�/�R�u�n�L�e�v�e�l�>�
�
� � � � �<�/�P�r�i�n�c�i�p�a�l�>�
�
� � �<�/�P�r�i�n�c�i�p�a�l�s�>�
�
� � �<�S�e�t�t�i�n�g�s�>�
�
� � � � �<�M�u�l�t�i�p�l�e�I�n�s�t�a�n�c�e�s�P�o�l�i�c�y�>�S�t�o�p�E�x�i�s�t�i�n�g�<�/�M�u�l�t�i�p�l�e�I�n�s�t�a�n�c�e�s�P�o�l�i�c�y�>�
�
� � � � �<�D�i�s�a�l�l�o�w�S�t�a�r�t�I�f�O�n�B�a�t�t�e�r�i�e�s�>�f�a�l�s�e�<�/�D�i�s�a�l�l�o�w�S�t�a�r�t�I�f�O�n�B�a�t�t�e�r�i�e�s�>�
�
� � � � �<�S�t�o�p�I�f�G�o�i�n�g�O�n�B�a�t�t�e�r�i�e�s�>�t�r�u�e�<�/�S�t�o�p�I�f�G�o�i�n�g�O�n�B�a�t�t�e�r�i�e�s�>�
�
� � � � �<�A�l�l�o�w�H�a�r�d�T�e�r�m�i�n�a�t�e�>�t�r�u�e�<�/�A�l�l�o�w�H�a�r�d�T�e�r�m�i�n�a�t�e�>�
�
� � � � �<�S�t�a�r�t�W�h�e�n�A�v�a�i�l�a�b�l�e�>�f�a�l�s�e�<�/�S�t�a�r�t�W�h�e�n�A�v�a�i�l�a�b�l�e�>�
�
� � � � �<�R�u�n�O�n�l�y�I�f�N�e�t�w�o�r�k�A�v�a�i�l�a�b�l�e�>�f�a�l�s�e�<�/�R�u�n�O�n�l�y�I�f�N�e�t�w�o�r�k�A�v�a�i�l�a�b�l�e�>�
�
� � � � �<�I�d�l�e�S�e�t�t�i�n�g�s�>�
�
� � � � � � �<�S�t�o�p�O�n�I�d�l�e�E�n�d�>�t�r�u�e�<�/�S�t�o�p�O�n�I�d�l�e�E�n�d�>�
�
� � � � � � �<�R�e�s�t�a�r�t�O�n�I�d�l�e�>�f�a�l�s�e�<�/�R�e�s�t�a�r�t�O�n�I�d�l�e�>�
�
� � � � �<�/�I�d�l�e�S�e�t�t�i�n�g�s�>�
�
� � � � �<�A�l�l�o�w�S�t�a�r�t�O�n�D�e�m�a�n�d�>�t�r�u�e�<�/�A�l�l�o�w�S�t�a�r�t�O�n�D�e�m�a�n�d�>�
�
� � � � �<�E�n�a�b�l�e�d�>�t�r�u�e�<�/�E�n�a�b�l�e�d�>�
�
� � � � �<�H�i�d�d�e�n�>�f�a�l�s�e�<�/�H�i�d�d�e�n�>�
�
� � � � �<�R�u�n�O�n�l�y�I�f�I�d�l�e�>�f�a�l�s�e�<�/�R�u�n�O�n�l�y�I�f�I�d�l�e�>�
�
� � � � �<�D�i�s�a�l�l�o�w�S�t�a�r�t�O�n�R�e�m�o�t�e�A�p�p�S�e�s�s�i�o�n�>�f�a�l�s�e�<�/�D�i�s�a�l�l�o�w�S�t�a�r�t�O�n�R�e�m�o�t�e�A�p�p�S�e�s�s�i�o�n�>�
�
� � � � �<�U�s�e�U�n�i�f�i�e�d�S�c�h�e�d�u�l�i�n�g�E�n�g�i�n�e�>�t�r�u�e�<�/�U�s�e�U�n�i�f�i�e�d�S�c�h�e�d�u�l�i�n�g�E�n�g�i�n�e�>�
�
� � � � �<�W�a�k�e�T�o�R�u�n�>�f�a�l�s�e�<�/�W�a�k�e�T�o�R�u�n�>�
�
� � � � �<�E�x�e�c�u�t�i�o�n�T�i�m�e�L�i�m�i�t�>�P�T�1�H�<�/�E�x�e�c�u�t�i�o�n�T�i�m�e�L�i�m�i�t�>�
�
� � � � �<�P�r�i�o�r�i�t�y�>�7�<�/�P�r�i�o�r�i�t�y�>�
�
� � �<�/�S�e�t�t�i�n�g�s�>�
�
� � �<�A�c�t�i�o�n�s� �C�o�n�t�e�x�t�=�"�A�u�t�h�o�r�"�>�
�
� � � � �<�E�x�e�c�>�
�
� � � � � � �<�C�o�m�m�a�n�d�>�"�C�:�\�P�o�r�t�a�b�l�e� �A�p�p�s�\�M�o�n�O�f�f� �(�D�i�s�p�l�a�y� �o�f�f� �W�h�e�n� �L�o�c�k�e�d�)�\�M�o�n�O�f�f� �(�B�a�t�t�e�r�y� �O�n�l�y�)�.�b�a�t�"�<�/�C�o�m�m�a�n�d�>�
�
� � � � �<�/�E�x�e�c�>�
�
� � �<�/�A�c�t�i�o�n�s�>�
�
�<�/�T�a�s�k�>�
Cleaned up version (�'s removed):
ÿþ<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-01-11T04:56:22.2785148</Date>
<Author>Computer_Name\User</Author>
<URI>\Display Off When Locked (Battery Only)</URI>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
</Exec>
</Actions>
</Task>
task.xml:
ÿþ<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2021-01-11T04:56:22.2785148</Date>
<Author>Computer_Name\User</Author>
<URI>\Display Off When Locked (Battery Only)</URI>
</RegistrationInfo>
<Triggers>
<SessionStateChangeTrigger>
<Enabled>true</Enabled>
<StateChange>SessionLock</StateChange>
</SessionStateChangeTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<GroupId>S-1-5-32-544</GroupId>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>StopExisting</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
<UseUnifiedSchedulingEngine>true</UseUnifiedSchedulingEngine>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT1H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>"C:\Portable Apps\MonOff (Display off When Locked)\MonOff (Battery Only).bat"</Command>
</Exec>
</Actions>
</Task>
As you can see, the xml exported from scheduled tasks has a lot of extra stuff (Unicode characters?) in it. The only thing I can think of at this point is to try copy/pasting the stuff from HxD into the batch file and having all those extra characters echo'd into the xml file, assuming it will even do so, which is what I'm going to try next. I need to do this in order to be able to have the scheduled task created automatically based on certain variables that may differ between computers. Any help would be much appreciated. Thanks!