LoginSignup
3
4

More than 5 years have passed since last update.

Windows起動時に実行するPowerShellスクリプトをタスクスケジューラにPowershellで登録する

Posted at

確認した環境:Windows Server 2012 R2, PowerShell 4


PSScheduledJobモジュールを使って登録したタスクがスタートアップ時(Windows起動時)の実行に失敗する。タスクの実行時に使うユーザーアカウントをローカルシステムアカウントにすれば成功する。

タスクの開始が失敗しました
 タスク スケジューラは、ユーザー "[コンピューター名]\Administrator" の "\Microsoft\Windows\PowerShell\ScheduledJobs\[タスク名]" タスクを開始できませんでした。追加データ: エラー値: 2147943711。

タスクスケジューラでPowerShellスクリプトを実行するタスクはPSScheduledJob モジュールを使ってスケジュールされたジョブとして登録する。PSScheduledJobモジュールにはスケジュールされたジョブの実行時に使うユーザーアカウントを指定する方法がない。スケジュールされたジョブはタスクスケジューラのタスクでもあるのでScheduled Tasks Cmdletsを使って指定する。

Register-ScheduledJob -FilePath $filepath -Name $name -Trigger @{Frequency="AtStartup"}

Get-ScheduledTask -TaskName $name | Set-ScheduledTask -User "NT AUTHORITY\System"
3
4
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
3
4