81 lines
2.8 KiB
PowerShell
81 lines
2.8 KiB
PowerShell
#Requires -Version 5.1
|
|
|
|
[CmdletBinding(SupportsShouldProcess = $true)]
|
|
param(
|
|
[string[]]$Times = @('00:01', '09:10'),
|
|
|
|
[string]$TaskName = 'CaiWuXiTong-HxbBankDaily'
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
$cmdPath = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot 'run-hxb-daily.cmd')).Path
|
|
$currentUser = [Security.Principal.WindowsIdentity]::GetCurrent().Name
|
|
$arguments = @('/d', '/c', ('""' + $cmdPath + '""')) -join ' '
|
|
$slots = @(
|
|
$Times |
|
|
ForEach-Object { $_ -split ',' } |
|
|
ForEach-Object { $_.Trim() } |
|
|
Where-Object { $_ } |
|
|
Select-Object -Unique
|
|
)
|
|
if ($slots.Count -eq 0 -or @($slots | Where-Object { $_ -notmatch '^([01]\d|2[0-3]):[0-5]\d$' }).Count -gt 0) {
|
|
throw '定时执行时间格式无效,必须为 HH:mm'
|
|
}
|
|
|
|
if (-not $PSCmdlet.ShouldProcess($TaskName, "register daily HXB import at $($slots -join ', ')")) {
|
|
return
|
|
}
|
|
|
|
$service = New-Object -ComObject 'Schedule.Service'
|
|
$rootFolder = $null
|
|
$definition = $null
|
|
try {
|
|
$service.Connect()
|
|
$rootFolder = $service.GetFolder('\')
|
|
$definition = $service.NewTask(0)
|
|
$definition.RegistrationInfo.Description = '每日 00:01、09:10 读取华夏银行昨天至今天流水并导入财务系统银行账单'
|
|
$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 = 'PT30M'
|
|
$definition.Settings.MultipleInstances = 2
|
|
$definition.Settings.RestartCount = 2
|
|
$definition.Settings.RestartInterval = 'PT10M'
|
|
|
|
foreach ($slot in $slots) {
|
|
$trigger = $definition.Triggers.Create(2)
|
|
$trigger.StartBoundary = (Get-Date).Date.ToString('yyyy-MM-dd') + 'T' + $slot + ':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,执行时间每天 $($slots -join '、')。"
|
|
[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)
|
|
}
|
|
}
|
|
}
|