Export/Import Scheduled Tasks in bulk

Page 3 of 3 FirstFirst 123

  1. Posts : 1,203
    11 Home
       #21

    avada said:
    Not sure what the "\\?" was meant to do,
    https://learn.microsoft.com/en-us/do...s-device-paths

    This is done to work around the limitation of most Windows built-in commands and utilities not fully supporting all NTFS file and folder naming conventions, like I have explained in my post. Changing the codepage to 65001 also ensures that any special characters (UTF-8) that may occur in the names of the XML files do not get converted to something that looks like Japanese.

    https://learn.microsoft.com/en-us/wi...ge-identifiers

      My Computers


  2. Posts : 168
    10 (1909)
       #22

    hdmi said:
    https://learn.microsoft.com/en-us/do...s-device-paths

    This is done to work around the limitation of most Windows built-in commands and utilities not fully supporting all NTFS file and folder naming conventions, like I have explained in my post. Changing the codepage to 65001 also ensures that any special characters (UTF-8) that may occur in the names of the XML files do not get converted to something that looks like Japanese.

    https://learn.microsoft.com/en-us/wi...ge-identifiers

    Well, that question mark slash prefix didn't work here for sure.

    I understand the codepage, but not what ">nul" is meant to do.
      My Computer


  3. Posts : 1,203
    11 Home
       #23

    avada said:
    Well, that question mark slash prefix didn't work here for sure.
    Try moving the line chcp 65001>nul before the line set d=b:\OS\tasks\Saját. The á in Saját gets converted to├í otherwise. Also note there is no backslash at the end of the folderpath here.
    I understand the codepage, but not what ">nul" is meant to do.
    nul - Windows CMD - SS64.com
      My Computers


  4. Posts : 456
    Windows 10
       #24

    >nul redirects the output to "Windows Black Hole" all you send to there disappears. So you wouldn't see a message like: "The codepage has been changed to 65001".

    But it is better to put a space between the codepage and >nul otherwhise 1>nul could mean that you want to send the default output to nul and the actuall codepage may be 6500 instead of 65001. So: chcp 65001 >nul
      My Computer


  5. Posts : 1,203
    11 Home
       #25

    ricardobohner said:
    But it is better to put a space between the codepage and >nul otherwhise 1>nul could mean that you want to send the default output to nul and the actuall codepage may be 6500 instead of 65001. So: chcp 65001 >nul
    No, the space character in front of a redirection isn't required after a numerical character string if this string consists of multiple numerical characters. Had there been a space character in front of the 1 character, then yes, you would've been correct with your remark, but codepages all consist of multiple digits so in this case it's no problemo. For further reading about this particular subject, https://devblogs.microsoft.com/oldne...01-00/?p=96725
      My Computers


  6. Posts : 25
    Windows 10 Enterprise LTSC 21H1
       #26

    hdmi said:
    To copy all the tasks to your folder E:\Backup\My Tasks
    Code:
    @echo off
    set d=E:\Backup\My Tasks
    chcp 65001>nul
    copy \\?\%Windir%\System32\Tasks\*.* "\\?\%d%"
    (You could decide to simply copy these with File Explorer instead, but that won't work if one or more tasks have a name that ends with a period [.] character, as File Explorer refuses to recognize filenames that end with a period even though such filenames are nonetheless valid NTFS filenames... I know it's funny, but you can verify this yourself by creating a new task named test. if you don't believe.)

    Next, on the other computer, assuming that your folder to which you have copied the tasks is now F:\Backup\My Tasks
    Code:
    @echo off
    set d=F:\Backup\My Tasks
    chcp 65001>nul
    for /f "usebackq delims=|" %%f in (`dir /b "\\?\%d%"`) do (
      schtasks /create /tn "%%~f" /xml "\\?\%d%\%%~f"
    )
    del "\\?\%d%\*.*"
    (The last command will delete all the files from your folder if you choose to confirm, File Explorer can't delete a file if the filename ends with a period so, to delete a file F:\Backup\My Tasks\test. you could run del "\\?\F:\Backup\My Tasks\test." )
    hello dear friend@hdmi

    First of all, I hope you and your family are doing well.

    I was able to back it up and post the import by making some minor changes to your scripts and then it worked... my versions of them are below:

    Backup:
    Code:
    @ECHO OFF
    cd /d "%~dp0"
    Color 0E
    setlocal EnableExtensions
    setlocal EnableDelayedExpansion
    set "params=%*"
    cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )
    cls
    @ECHO OFF
    set d=O:\@llOptionalApps\0-AutomatedInstallations\Library\Tarefas_do_Agendador\BackUps\
    @CHCP 1252 >NUL
    XCOPY /E /H /C /Q /Y %Windir%\System32\Tasks\*.* "%d%"
    timeout /t 3
    rmdir /s /q BackUps\Microsoft
    rmdir /s /q BackUps\Mozilla
    Del /Q BackUps\*.*
    ECHO.   Tarefas Copiadas...
    timeout /t 3
    EXIT
    In my case the tasks I backed up are in a separate folder from the other tasks, "%Windir%\System32\Tasks\Network_Controls" so I used Xcopy and added commands to delete the unwanted folders, I also added getadmin.vbs to raise the script execution for ease of use...

    Import:
    Code:
    @ECHO OFF
    cd /d "%~dp0"
    Color 0E
    setlocal EnableExtensions
    setlocal EnableDelayedExpansion
    set "params=%*"
    cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )
    cls
    @ECHO OFF
    set d=O:\@llOptionalApps\0-AutomatedInstallations\Library\Tarefas_do_Agendador\BackUps\Network_Controls\
    @CHCP 1252 >NUL
    for /f "usebackq delims=|" %%f in (`dir /b "%d%"`) do (
      schtasks /create /tn "Network_Controls\%%~f" /xml "%d%\%%~f"
    )
    timeout /t 3
    EXIT
    Before importing the tasks, I edited them and deleted the lines of code <UserId>Dummy</UserId> indicated by our friend @Try3 and <Author>SSDESKTOP\Dummy</Author> by myself and for me it worked perfectly not even asking for user password.

    Thanks for share your knowlegde with us!

    Best Regards,

    @JeepWillys58 - Marcelo
      My Computer


  7. Posts : 1,203
    11 Home
       #27

    JeepWillys58 said:
    In my case the tasks I backed up are in a separate folder from the other tasks, "%Windir%\System32\Tasks\Network_Controls" so I used Xcopy
    There's no need to use Xcopy. You can just substitute the path and still use the copy command instead.
    I also added getadmin.vbs to raise the script execution for ease of use...
    I have no idea why you want to raise it. Copying the files from the folder does not require admin rights.
    Code:
    setlocal EnableExtensions
    setlocal EnableDelayedExpansion
    set "params=%*"
    But why?
    Code:
    @CHCP 1252 >NUL
    This breaks it if the task names use special characters that are not part of the extended ASCII set. That in fact is why the 65001 code page is a necessity, it is to prevent M$ from converting all your batch command output to Heptapod language like shown in the animated gif that I posted up thread.
    Before importing the tasks, I edited them and deleted the lines of code <UserId>Dummy</UserId> indicated by our friend @Try3 and <Author>SSDESKTOP\Dummy</Author> by myself and for me it worked perfectly not even asking for user password.
    Just keep in mind that doing this does not merely import tasks, rather it imports edited copies of tasks. Some processes/programs/tasks might actually depend on the UserId and/or Author, and, such modifications were not part of the OP's question, which was about how to import numerous tasks in bulk so manually editing the files was a bit off topic, as it kind of defeats the purpose of using a batchfile to import the tasks in bulk. For those who need to convert tasks between different user accounts and/or between different computers and/or whatnot, it shouldn't be that hard to adjust the batchfile to make it automatically substitute or remove those fields from the (copied) xml files before it imports these tasks to Task Scheduler.
      My Computers


  8. Posts : 25
    Windows 10 Enterprise LTSC 21H1
       #28

    I my dear friend @hdmi

    I hope you and all yours be weel always with the blessem of God!

    hdmi said:
    There's no need to use Xcopy. You can just substitute the path and still use the copy command instead.
    It's because I try to copy the tasks with the "copy" command, but it didn't copy the other task subfolders and I always put the tasks I create in separate folders as you can see above, for network tasks, the Network_Controls folder and the " Xcopy" does it.

    hdmi said:
    I have no idea why you want to raise it. Copying the files from the folder does not require admin rights.
    Because when I try to access the "%systemroot%\System32\Tasks" it show me that I don't have permission to access this folder and tells that to have permanent access to this folder I have to click in "Continue", but I don't like to change the system folders permissions, so I use the getadmin.vbs to raise the script execution. If I wantted to access the folder I use Altap Salamander as TrustedInstaller (Enable All Privileges), by NSudo...

    hdmi said:
    But why?
    This breaks it if the task names use special characters that are not part of the extended ASCII set. That in fact is why the 65001 code page is a necessity, it is to prevent M$ from converting all your batch command output to Heptapod language like shown in the animated gif that I posted up thread. .
    It's may mistake, it's from another script... I forget to delete it.

    hdmi said:
    Just keep in mind that doing this does not merely import tasks, rather it imports edited copies of tasks. Some processes/programs/tasks might actually depend on the UserId and/or Author, and, such modifications were not part of the OP's question, which was about how to import numerous tasks in bulk so manually editing the files was a bit off topic, as it kind of defeats the purpose of using a batchfile to import the tasks in bulk. For those who need to convert tasks between different user accounts and/or between different computers and/or whatnot, it shouldn't be that hard to adjust the batchfile to make it automatically substitute or remove those fields from the (copied) xml files before it imports these tasks to Task Scheduler.
    Sorry but I don't know how to make the bachfile clean this infomations before import, maybe you should show me how to if it's not ask to much?


    Thanks in advanced,

    Best Regards
    @JeepWillys58
      My Computer


  9. Posts : 1,203
    11 Home
       #29

    JeepWillys58 said:
    Sorry but I don't know how to make the bachfile clean this infomations before import, maybe you should show me how to if it's not ask to much?@JeepWillys58
    Here's a PowerShell script that exports (or copies) tasks from a folder to another folder while removing the UserId element from each task. Be sure to substitute the path specifications at the top of the script with the correct ones.

    Code:
    $src = "$($env:windir)\System32\Tasks"
    $dest = "Z:\My Tasks"
    $log = "Z:\Log file.txt"
    
    $l = (!($null -eq $log))
    if ($l) { try { Out-File $log -Encoding UTF8 } catch { Exit } }
    
    if (!(Test-Path -PathType Container $src)) {
      if ($l) { "ERROR: The source folder `"$($src)`" does not exist." | Out-File $log -Encoding UTF8 }
      Exit }
    
    if (!(Test-Path -PathType Container $dest)) {
      $null = (md -Force $dest)
      if (!(Test-Path -PathType Container $dest)) {
        if ($l) { "ERROR: Can`'t create the destination folderpath `"$($dest)`"." | Out-File $log -Encoding UTF8 }
        Exit } }
    
    if ($l) { "$($src)`n" | Out-File $log -Encoding UTF8 -Append }
    
    ls $src -af | % {
      $i = gc $_.FullName -Encoding Unicode
    
      if ($i[0] -eq '<?xml version="1.0" encoding="UTF-16"?>' -and `
          $i[1].Substring(0, 15) -eq '<Task version="' -and `
          $i[1].Substring($i[1].Length - 64) -eq '" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">') {
    
        $XmlData = [xml]$i
        $XmlData | Select-Xml -XPath `
        '(/*[local-name()=''Task'']/*[local-name()=''Principals'']/*[local-name()=''Principal''][@id=''Author''])[1]' | % {
    
          if ($l) {
            "`"$($XmlData.Task.RegistrationInfo.URI)`" , " | Out-File $log -Encoding UTF8 -Append -NoNewLine
            $uid = "`"$($_.Node.UserId)`"" }
          try {
    
            $null = $_.Node.RemoveChild($_.Node.Item('UserId'))
    
            if ($l) { $uid | Out-File $log -Encoding UTF8 -Append }
          } catch { if ($l) { "NULL" | Out-File $log -Encoding UTF8 -Append } } }
    
        $settings = New-Object System.Xml.XmlWriterSettings
        $settings.Indent = $true
        $settings.Encoding = [System.Text.Encoding]::Unicode
        $XmlWriter = [System.XML.XmlWriter]::Create("$($dest)\$($_.Name)", $settings)
        $XmlData.Save($XmlWriter)
        $XmlWriter.Close()
    
      } else { if ($l) { "`"$($_.Name)`"" | Out-File $log -Encoding UTF8 -Append } } }
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:23.
Find Us




Windows 10 Forums