使用 xml 从 powershell 创建计划任务

使用 xml 从 powershell 创建计划任务

我能够使用以下方法导出所有计划任务:

我需要做的就是从 xml 文件导入计划任务

我有大约 100 个这样的,因此,任何帮助都将不胜感激。

$savefolder = "C:\stxml"

Get-ScheduledTask | 
    Foreach-Object { $_ | Export-ScheduledTask | 
        Out-File (Join-Path $savefolder "$($_.TaskName).xml") }

我现在正尝试使用 powershell 5.1 导入它们

我声明:

Register-ScheduledTask -xml (Get-Content 'C:\stxml\test.xml' | Out-String) -TaskName "TEst Sch Task" -TaskPath "\" -User .\testuser -Force
Register-ScheduledTask : No mapping between account names and security IDs was done.

这让我想到:

$pre = new-scheduledtaskprincipal -userid testuser$ -logontype password; Register-ScheduledTask -xml (Get-Content 'C:\stxml\test.xml' | Out-String) -TaskName "TEst Sch Task" -TaskPath "\" -principal $pre
Register-ScheduledTask : Parameter set cannot be resolved using the specified named parameters.

我可以将其归结为不喜欢 -xml 参数。

我的 C:\stxml\test.xml 文件:

<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.3" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2019-01-02T11:57:15.9628723</Date>
    <Author>testuser</Author>
    <Description>Does Stuff</Description>
    <SecurityDescriptor></SecurityDescriptor>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2019-01-02T12:00:00</StartBoundary>
      <Enabled>false</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
    <CalendarTrigger>
      <StartBoundary>2019-01-02T18:00:00</StartBoundary>
      <Enabled>false</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>.\testuser</UserId>
      <LogonType>Password</LogonType>
      <RunLevel>LeastPrivilege</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>
    <DisallowStartOnRemoteAppSession>false</DisallowStartOnRemoteAppSession>
    <UseUnifiedSchedulingEngine>false</UseUnifiedSchedulingEngine>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions>
    <Exec>
      <Command>D:\some.vbs</Command>
    </Exec>
  </Actions>
</Task>

相关内容