74 lines
2.4 KiB
PowerShell
74 lines
2.4 KiB
PowerShell
#Requires -Version 5.1
|
|
|
|
[CmdletBinding(SupportsShouldProcess = $true)]
|
|
param(
|
|
[ValidatePattern('^([01]\d|2[0-3]):[0-5]\d$')]
|
|
[string] $Time = '10:00',
|
|
|
|
[string] $TaskName = 'CaiWuXiTong-WangshangBankDaily'
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$cmdPath = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot 'run-wangshang-daily.cmd')).Path
|
|
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
|
|
$arguments = @(
|
|
'/d',
|
|
'/c',
|
|
('""' + $cmdPath + '""')
|
|
) -join ' '
|
|
|
|
if (-not $PSCmdlet.ShouldProcess($TaskName, "register daily Wangshang Bank export at $Time")) {
|
|
return
|
|
}
|
|
|
|
$service = New-Object -ComObject 'Schedule.Service'
|
|
$rootFolder = $null
|
|
$definition = $null
|
|
try {
|
|
$service.Connect()
|
|
$rootFolder = $service.GetFolder('\')
|
|
$definition = $service.NewTask(0)
|
|
$definition.RegistrationInfo.Description = 'CaiWuXiTong 网商银行前一日交易明细同步'
|
|
$definition.Principal.UserId = $currentUser
|
|
$definition.Principal.LogonType = 3
|
|
$definition.Principal.RunLevel = 0
|
|
$definition.Settings.Enabled = $true
|
|
$definition.Settings.StartWhenAvailable = $true
|
|
$definition.Settings.DisallowStartIfOnBatteries = $false
|
|
$definition.Settings.StopIfGoingOnBatteries = $false
|
|
$definition.Settings.ExecutionTimeLimit = 'PT2H'
|
|
$definition.Settings.MultipleInstances = 2
|
|
$definition.Settings.RestartCount = 1
|
|
$definition.Settings.RestartInterval = 'PT15M'
|
|
|
|
$trigger = $definition.Triggers.Create(2)
|
|
$trigger.StartBoundary = (Get-Date).Date.AddDays(1).ToString('yyyy-MM-dd') + 'T' + $Time + ':00'
|
|
$trigger.DaysInterval = 1
|
|
$trigger.Enabled = $true
|
|
|
|
$action = $definition.Actions.Create(0)
|
|
$action.Path = $env:ComSpec
|
|
$action.Arguments = $arguments
|
|
$action.WorkingDirectory = $PSScriptRoot
|
|
|
|
$registered = $rootFolder.RegisterTaskDefinition(
|
|
$TaskName,
|
|
$definition,
|
|
6,
|
|
$null,
|
|
$null,
|
|
3,
|
|
$null
|
|
)
|
|
Write-Host "已安装每日网商银行账单任务:$TaskName,执行时间每天 $Time。"
|
|
[void] [Runtime.InteropServices.Marshal]::FinalReleaseComObject($registered)
|
|
} finally {
|
|
foreach ($item in @($definition, $rootFolder, $service)) {
|
|
if ($null -ne $item -and [Runtime.InteropServices.Marshal]::IsComObject($item)) {
|
|
[void] [Runtime.InteropServices.Marshal]::FinalReleaseComObject($item)
|
|
}
|
|
}
|
|
}
|