LoginSignup
1
0

PowerShell 標準コマンドレットでは作成できない月次タスクを気合と根性で作成する

Last updated at Posted at 2024-03-21

PowerShell には New-ScheduledTask などのタスク スケジューラーを管理する便利なコマンドレット群があります。しかし、このコマンドレット群を利用しても、月次タスクを作成できません。なんと、New-ScheduledTaskTrigger には月次タスクを作成するオプションがないのです。え、そんなに需要ない?
(まぁ、事前にタスクを組んで XML に出力してインポートする、なんて方法もありますし、大半の人はそれで何とかしている気がしますが…。)

というわけで、頑張ってコードを書いてみました。
まるっと VScode あたりにコピペしてもらい必要なものをアンコメントすればよいというお手軽仕様です。
ちなみにそのまま実行すると、毎月第一土曜日の 0:00 と、毎月 1 日の 0:00 に、C:\temp\test.bat TaskTest というコマンドを実行してくれるタスクができあがります。

それからこのあたりをよく読んでもらえれば、その他のトリガーも使いこなせるはずです。興味のある方はぜひどうぞ。
https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-type

##################################################################################################
# PowerShell でタスク スケジューラにタスクを作成する
# 週次タスクは New-ScheduledTask -Weekly などがあるが、月次は組めないのでそれを補完する
##################################################################################################
# 日付フォーマット
# PnYnMnDTnHnMnS nY: 年数 nM: 月数 nD: 日数 T: 日付/時刻の区切り記号 nH: 時間 nM: 分 nS: 秒
# たとえば、PT5M は 5 分を指定し、P1M4DT2H5M は 1 か月と 4 日、2 時間、5 分を指定する
# 詳しくは W3C 参照
# https://www.w3.org/TR/xmlschema-2/#duration
##################################################################################################

$Service = New-Object -ComObject Schedule.Service
$Service.connect()
$TaskDefinition = $Service.NewTask(0)

##################################################################################################

# ■ 全般

# 名前
$Name = "Task Name"

# 場所
$Path = "\"

# 同名で存在するタスクはすべて削除する
Unregister-ScheduledTask -TaskPath $Path -TaskName $Name -Confirm:$false -ErrorAction SilentlyContinue

# 説明
# ヒアドキュメント形式で記載する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/registrationinfo-description
$TaskDefinition.RegistrationInfo.Description = @"
Task Name 
Description
"@

# タスクを有効化する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-enabled
$TaskDefinition.Settings.Enabled = $null  # 既定値 : 有効
$TaskDefinition.Settings.Enabled = $false #     : 無効
$TaskDefinition.Settings.Enabled = $true  #     : 有効

# ユーザー アカウント
$UserAccount = "SYSTEM"         # 管理者ユーザー権限 (Administrators)
#$UserAccount = "LOCALSERVICE"   # 一般ユーザー権限 (Users)
#$UserAccount = "NETWORKSERVICE" # 一般ユーザー権限かつネットワーク リソースへのアクセスを許可 (Users)
#$UserAccount = "TestUser"       # ローカル アカウントの権限

# パスワード
# ローカル アカウントを指定する場合に指定する。
# $UserAccount に SYSTEM、LOCALSERVICE、NETWORKSERVICE を使用する場合は指定しなくてよい。
# $LogonType に TASK_LOGON_INTERACTIVE_TOKEN を指定している場合は指定しなくてよい。
#$Password = "P@ssw0rd"

# セキュリティ オプション
# ローカル アカウントを指定する場合に以下のいずれかを指定する。
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_logon_type
#$LogonType = $null # 既定値 "TASK_LOGON_S4U"               : ユーザーがログオンしているかどうかにかかわらず実行する (パスワードを保存しない)
#$LogonType = 1     #     "TASK_LOGON_PASSWORD"          : ユーザーがログオンしているかどうかにかかわらず実行する (パスワードを保存する)
#$LogonType = 2     #     "TASK_LOGON_S4U"               : ユーザーがログオンしているかどうかにかかわらず実行する (パスワードを保存しない)
#$LogonType = 3     #     "TASK_LOGON_INTERACTIVE_TOKEN" : ユーザーがログオンしているときのみ実行する

# 最上位の特権で実行する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/principal-runlevel
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_runlevel_type
#$TaskDefinition.Principal.RunLevel = $null # 既定値 "TASK_RUNLEVEL_LUA"     : 無効
#$TaskDefinition.Principal.RunLevel = 0     #     "TASK_RUNLEVEL_LUA"     : 無効
#$TaskDefinition.Principal.RunLevel = 1     #     "TASK_RUNLEVEL_HIGHEST" : 有効 (HighestAvailable)

# 表示しない
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-hidden
#$TaskDefinition.Settings.Hidden = $null  # 既定値 : 無効
#$TaskDefinition.Settings.Hidden = $false #     : 無効
#$TaskDefinition.Settings.Hidden = $true  #     : 有効

# 構成 (Windows 11 の場合)
# 3 以上を指定すると UseUnifiedSchedulingEngine が $true になる
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-compatibility
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_compatibility
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/what-s-new-in-task-scheduler
#$TaskDefinition.Settings.Compatibility = $null # 既定値 "TASK_COMPATIBILITY_V2"   : Windows Vista, Windows Server 2008 (Vista)
#$TaskDefinition.Settings.Compatibility = 2     #     "TASK_COMPATIBILITY_V2"   : Windows Vista, Windows Server 2008 (Vista)
#$TaskDefinition.Settings.Compatibility = 3     #     "TASK_COMPATIBILITY_V2_1" : Windows 7, Windows Server 2008 R2 (Win7)
#$TaskDefinition.Settings.Compatibility = 4     #     "TASK_COMPATIBILITY_V2_2" : Windows 10 (Win8)

##################################################################################################

# ■ トリガー (スケジュールに従う/毎月/日) (TASK_TRIGGER_MONTHLY)
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-type
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/triggercollection-create
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_trigger_type2
$Trigger = $taskDefinition.Triggers.Create(4)
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlytrigger

# 開始
# Trigger 共通、必須パラメーター
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-startboundary
$Trigger.StartBoundary = ( Get-Date -Hour 0 -Second 0 -Minute 0 ).AddDays(1).ToString("s")

# 月
# TASK_TRIGGER_MONTHLY 専用、必須パラメーター
# 二進数で表現
# 000000000001 = 1 月、000000000010 = 2 月、111111111111 = 毎月、111111000000 = 7~12 月
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlytrigger-monthsofyear
$Trigger.MonthsOfYear = [convert]::ToInt32("111111111111", 2)

# 日
# TASK_TRIGGER_MONTHLY 専用
# RunOnLastDayOfMonth が $null または $false の場合に設定する必要あり
# 二進数で表現
# 0000000000000000000000000000000 = なし、0000000000000000000000000000001 = 1 日、
# 0000000000000000000000000000010 = 2 日、1000000000000000000000000000000 = 31 日
# RunOnLastDayOfMonth が False の場合、1 以上を指定する必要あり
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlytrigger-daysofmonth
$Trigger.DaysOfMonth = [convert]::ToInt32("0000000000000000000000000000001", 2)

# 最終日 = $true (既定値 = $false)
# TASK_TRIGGER_MONTHLY 専用
# DaysOfMonth が $null または 0000000000000000000000000000000 の場合、True にする必要あり
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlytrigger-runonlastdayofmonth
#$Trigger.RunOnLastDayOfMonth = $true

# 詳細設定
# 遅延開始時間を指定する
# TASK_TRIGGER_MONTHLY 専用
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlytrigger-randomdelay
#$Trigger.RandomDelay = $null  # 既定値 : 無効
#$Trigger.RandomDelay = "PT1H" #     : 有効 (日付フォーマットで指定した日数が設定される)

# 繰り返し時間
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/repetitionpattern-interval
#$Trigger.Repetition.Interval = $null  # 既定値 : 無効
#$Trigger.Repetition.Interval = "PT1H" #     : 有効 (日付フォーマットで指定した日数が設定される)

# 継続時間
# Trigger 共通
# 必ず Interval に日付値を指定する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/repetitionpattern-duration
#$Trigger.Repetition.Duration = $null   #     : 無期限
#$Trigger.Repetition.Duration = "PT24H" # 既定値 : 日付フォーマットで指定した日数が設定される

# 繰り返し継続時間の最後に実行中のすべてのタスクを停止する
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/repetitionpattern-stopatdurationend
#$Trigger.Repetition.StopAtDurationEnd = $null  # 既定値 : 無効
#$Trigger.Repetition.StopAtDurationEnd = $false #     : 無効
#$Trigger.Repetition.StopAtDurationEnd = $true  #     : 有効

# 停止するまでの時間
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-executiontimelimit
#$Trigger.ExecutionTimeLimit = $null   # 既定値 : 無効
#$Trigger.ExecutionTimeLimit = "PT72H" #     : 有効 (日付フォーマットで指定した日数が設定される)

# 有効期限
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-endboundary
#$Trigger.EndBoundary = $null # 既定値 : 無効
#$Trigger.EndBoundary = ( Get-Date -Hour 0 -Second 0 -Minute 0 ).AddYears(1).AddDays(1).ToString("s")

# 有効
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-enabled
#$Trigger.Enabled = $null  # 既定値 : 有効
#$Trigger.Enabled = $false #     : 無効
#$Trigger.Enabled = $true  #     : 有効

##################################################################################################

# ■ トリガー (スケジュールに従う/毎月/曜日) (TASK_TRIGGER_MONTHLYDOW)
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-type
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/triggercollection-create
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_trigger_type2
$Trigger = $taskDefinition.Triggers.Create(5)
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlydowtrigger

# 開始
# Trigger 共通、必須パラメーター
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-startboundary
$Trigger.StartBoundary = ( Get-Date -Hour 0 -Second 0 -Minute 0 ).AddDays(1).ToString("s")

# 月
# TASK_TRIGGER_MONTHLYDOW 専用、必須パラメーター
# 二進数で表現
# 000000000001 = 1 月、000000000010 = 2 月、111111111111 = 毎月、111111000000 = 7~12 月
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlydowtrigger-monthsofyear
$Trigger.MonthsOfYear = [convert]::ToInt32("111111111111", 2)

# 実行週
# TASK_TRIGGER_MONTHLYDOW 専用、必須パラメーター
# 二進数で表現
# 00001 = 第一週、00010 = 第二週、
# 10000 = 最終週 (WeeksOfMonth = 0、RunOnLastWeekOfMonth = true となる)、
# 11111 = すべての週
# 動作的には前 4 ビットが RunOnLastWeekOfMonth に、後 4 ビットが WeeksOfMonth に割り当たる様子
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/nf-taskschd-imonthlydowtrigger-get_weeksofmonth
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlydowtrigger-weeksofmonth
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlydowtrigger-runonlastweekofmonth
$Trigger.WeeksOfMonth = [convert]::ToInt32("00001", 2)

# 曜日
# TASK_TRIGGER_MONTHLYDOW 専用、必須パラメーター
# 二進数で表現
# 00000001 = 日、00000010 = 月、00000110 = 月、火
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlydowtrigger-daysofweek
$Trigger.DaysOfWeek = [convert]::ToInt32("1000000", 2)

# 詳細設定
# 遅延開始時間を指定する
# TASK_TRIGGER_MONTHLYDOW 専用
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/monthlydowtrigger-randomdelay
#$Trigger.RandomDelay = $null  # 既定値 : 無効
#$Trigger.RandomDelay = "PT1H" #     : 有効 (日付フォーマットで指定した日数が設定される)

# 繰り返し時間
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/repetitionpattern-interval
#$Trigger.Repetition.Interval = $null  # 既定値 : 無効
#$Trigger.Repetition.Interval = "PT1H" #     : 有効 (日付フォーマットで指定した日数が設定される)

# 継続時間
# Trigger 共通
# 必ず Interval に日付値を指定する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/repetitionpattern-duration
#$Trigger.Repetition.Duration = $null   #     : 無期限
#$Trigger.Repetition.Duration = "PT24H" # 既定値 : 日付フォーマットで指定した日数が設定される

# 繰り返し継続時間の最後に実行中のすべてのタスクを停止する
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/repetitionpattern-stopatdurationend
#$Trigger.Repetition.StopAtDurationEnd = $null  # 既定値 : 無効
#$Trigger.Repetition.StopAtDurationEnd = $false #     : 無効
#$Trigger.Repetition.StopAtDurationEnd = $true  #     : 有効

# 停止するまでの時間
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-executiontimelimit
#$Trigger.ExecutionTimeLimit = $null   # 既定値 : 無効
#$Trigger.ExecutionTimeLimit = "PT72H" #     : 有効 (日付フォーマットで指定した日数が設定される)

# 有効期限
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-endboundary
#$Trigger.EndBoundary = $null # 既定値 : 無効
#$Trigger.EndBoundary = ( Get-Date -Hour 0 -Second 0 -Minute 0 ).AddYears(1).AddDays(1).ToString("s")

# 有効
# Trigger 共通
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/trigger-enabled
#$Trigger.Enabled = $null  # 既定値 : 有効
#$Trigger.Enabled = $false #     : 無効
#$Trigger.Enabled = $true  #     : 有効

##################################################################################################

# ■ 操作 (プログラムの開始) "TASK_ACTION_EXEC"
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/action-type
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/actioncollection-create
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_action_type
$Action = $TaskDefinition.Actions.Create(0)
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/execaction

# プログラム/スクリプト
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/execaction-path
$Action.Path = "test.bat"

# 引数の追加
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/execaction-arguments
$Action.Arguments = "TaskTest"

# 開始
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/execaction-workingdirectory
$Action.WorkingDirectory = "C:\temp"

##################################################################################################

# ■ 条件

# アイドル
# 次の時間アイドル状態の場合のみタスクを開始する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-runonlyifidle
#$TaskDefinition.Settings.RunOnlyIfIdle = $null  # 既定値 : 無効
#$TaskDefinition.Settings.RunOnlyIfIdle = $false #     : 無効
#$TaskDefinition.Settings.RunOnlyIfIdle = $true  #     : 有効

# 次の時間アイドル状態の場合のみタスクを開始する
# RunOnlyIfIdle が有効の場合に動作
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/idlesettings-idleduration
#$TaskDefinition.Settings.IdleSettings.IdleDuration = $null   # 既定値 : PT10M (10 分間)
#$TaskDefinition.Settings.IdleSettings.IdleDuration = "PT10M" #     : 日付フォーマットで指定した日数が設定される

# アイドル状態になるのを待機する時間
# RunOnlyIfIdle が有効の場合に動作
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/idlesettings-waittimeout
#$TaskDefinition.Settings.IdleSettings.WaitTimeout = $null  # 既定値 : PT1H (1 時間)
#$TaskDefinition.Settings.IdleSettings.WaitTimeout = "PT1H" #     : 日付フォーマットで指定した日数が設定される

# コンピューターがアイドル状態でなくなった場合は停止する
# RunOnlyIfIdle が有効の場合に動作
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/idlesettings-stoponidleend
#$TaskDefinition.Settings.IdleSettings.StopOnIdleEnd = $null  # 既定値 : 有効
#$TaskDefinition.Settings.IdleSettings.StopOnIdleEnd = $false #     : 無効
#$TaskDefinition.Settings.IdleSettings.StopOnIdleEnd = $true  #     : 有効

# 再びアイドル状態になったら再開する
# RunOnlyIfIdle が有効の場合に動作
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/idlesettings-restartonidle
#$TaskDefinition.Settings.IdleSettings.RestartOnIdle = $null  # 既定値 : 無効
#$TaskDefinition.Settings.IdleSettings.RestartOnIdle = $false #     : 無効
#$TaskDefinition.Settings.IdleSettings.RestartOnIdle = $true  #     : 有効

# 電源
# コンピューターを AC 電源で使用している場合のみタスクを開始する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-disallowstartifonbatteries
#$TaskDefinition.Settings.DisallowStartIfOnBatteries = $null  # 既定値 : 有効
#$TaskDefinition.Settings.DisallowStartIfOnBatteries = $false #     : 無効
#$TaskDefinition.Settings.DisallowStartIfOnBatteries = $true  #     : 有効

# コンピューターの電源をバッテリに切り替える場合は停止する
# DisallowStartIfOnBatteries が有効の場合に動作
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-stopifgoingonbatteries
#$TaskDefinition.Settings.StopIfGoingOnBatteries = $null  # 既定値 : 有効
#$TaskDefinition.Settings.StopIfGoingOnBatteries = $false #     : 無効
#$TaskDefinition.Settings.StopIfGoingOnBatteries = $true  #     : 有効

# タスクを実行するためにスリープを解除する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-waketorun
#$TaskDefinition.Settings.WakeToRun = $null  # 既定値 : 無効
#$TaskDefinition.Settings.WakeToRun = $false #     : 無効
#$TaskDefinition.Settings.WakeToRun = $true  #     : 有効

# ネットワーク
# 次のネットワーク接続が使用可能な場合のみタスクを開始する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-runonlyifnetworkavailable
#$TaskDefinition.Settings.RunOnlyIfNetworkAvailable = $null  # 既定値 : 無効
#$TaskDefinition.Settings.RunOnlyIfNetworkAvailable = $false #     : 無効
#$TaskDefinition.Settings.RunOnlyIfNetworkAvailable = $true  #     : 有効

# ネットワークの指定
# RunOnlyIfNetworkAvailable が有効の場合に動作
# 設定できる値は以下のレジストリの ProfileName 列となる
# Get-ChildItem "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles" | Get-ItemProperty | Select ProfileName, PSChildName
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-networksettings
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/networksettings
#$TaskDefinition.Settings.NetworkSettings.Name = $null      # 既定値 : 任意の接続
#$TaskDefinition.Settings.NetworkSettings.Name = "hogehoge" #     : 指定した名称

##################################################################################################

# ■ 設定

# タスクを要求時に実行する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-allowdemandstart
#$TaskDefinition.Settings.AllowDemandStart = $null  # 既定値 : 有効
#$TaskDefinition.Settings.AllowDemandStart = $false #     : 無効
#$TaskDefinition.Settings.AllowDemandStart = $true  #     : 有効

# スケジュールされた時刻にタスクを開始できなかった場合、すぐにタスクを実行する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-startwhenavailable
#$TaskDefinition.Settings.StartWhenAvailable = $null  # 既定値 : 無効
#$TaskDefinition.Settings.StartWhenAvailable = $false #     : 無効
#$TaskDefinition.Settings.StartWhenAvailable = $true  #     : 有効

# タスクが失敗した場合の再起動の間隔
# RestartCount 値の設定が必要
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-restartinterval
#$TaskDefinition.Settings.RestartInterval = $null   # 既定値 : 無効
#$TaskDefinition.Settings.RestartInterval = "PT15M" #     : 有効 / PT15M (15 分間) (日付フォーマットで指定した日数が設定される)

# 再起動試行の最大数
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-restartcount
#$TaskDefinition.Settings.RestartCount = $null # 既定値 : 3 回
#$TaskDefinition.Settings.RestartCount = 3     #     : 指定した回数が設定される

# タスクを停止するまでの時間
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-executiontimelimit
#$TaskDefinition.Settings.ExecutionTimeLimit = $null   # 既定値 : 無効
#$TaskDefinition.Settings.ExecutionTimeLimit = "PT72H" #     : 有効 / PT72H (3 日間) (日付フォーマットで指定した日数が設定される)

# 要求時に実行中のタスクが終了しない場合、タスクを強制的に停止する
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-allowhardterminate
#$TaskDefinition.Settings.AllowHardTerminate = $null  # 既定値 : 有効
#$TaskDefinition.Settings.AllowHardTerminate = $false #     : 無効
#$TaskDefinition.Settings.AllowHardTerminate = $true  #     : 有効

# タスクの再実行がスケジュールされていない場合に削除されるまでの時間
# EndBoundary 値の設定が必要
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-deleteexpiredtaskafter
#$TaskDefinition.Settings.DeleteExpiredTaskAfter = $null  # 既定値 : 無効
#$TaskDefinition.Settings.DeleteExpiredTaskAfter = "P30D" #     : 有効 / P30D (30 日間) (日付フォーマットで指定した日数が設定される)

# タスクがすでに実行中の場合に適用される規則
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/tasksettings-multipleinstances
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_instances_policy
#$TaskDefinition.Settings.MultipleInstances = $null # 既定値 "TASK_INSTANCES_IGNORE_NEW"    : 新しいインスタンスを開始しない (IgnoreNew)
#$TaskDefinition.Settings.MultipleInstances = 0     #     "TASK_INSTANCES_PARALLEL"      : 新しいインスタンスを並行で実行 (Parallel)
#$TaskDefinition.Settings.MultipleInstances = 1     #     "TASK_INSTANCES_QUEUE"         : 新しいインスタンスをキューに追加 (Queue)
#$TaskDefinition.Settings.MultipleInstances = 2     #     "TASK_INSTANCES_IGNORE_NEW"    : 新しいインスタンスを開始しない (IgnoreNew)
#$TaskDefinition.Settings.MultipleInstances = 3     #     "TASK_INSTANCES_STOP_EXISTING" : 既存のインスタンスの停止 (StopExisting)

##################################################################################################

# タスク登録
$RootFolder = $Service.GetFolder($Path)
$RootFolder.RegisterTaskDefinition($Name, $TaskDefinition, 6, $UserAccount, $Password, $LogonType)
# https://learn.microsoft.com/ja-jp/windows/win32/taskschd/taskfolder-registertaskdefinition
# 第三引数の説明
# https://learn.microsoft.com/ja-jp/windows/win32/api/taskschd/ne-taskschd-task_creation

##################################################################################################

# 登録したタスクが実行できることを確認する
Start-ScheduledTask -TaskPath $Path -TaskName $Name

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0