As part of a system build automation script (that I'm using to create AWS AMIs), I want the system to reboot and then run additional post-boot setup tasks - but only once (so that when the system is imaged and then the image used to start a new instance, it will not run the post-boot setup tasks again).
The way I started to implement this is that the initial setup script (written in Powershell) uses Register-ScheduleJob -Trigger (New-JobTrigger -AtStartup) ... -Name PostBootSetup
to setup another Powershell script run immediately after boot. In the post-boot script, I have:
Get-JobTrigger -Name PostBootSetup | ?{$_.Enabled} | Disable-JobTrigger
But when it is run I get this error (I'm logging all output of the post-boot script to a file):
Powershell : A scheduled job definition with Name PostBootSetup could not be found.
Which is weird because that is the output from the job that is currently running. I've also added just plain calls to Get-ScheduledJob
and Get-JobTrigger -Name PostBootSetup
that should output the relevant records, but there is absolutely no output from these - even though if I run these commands on a Powershell console after the machine started, I get the expected output.
Is it possible that a job can't access its own scheduled job record while running? If so, is there a workaround? or am I missing something? If this is not a good method to go about doing a one-time post-boot job, what would you suggest?
As a further note, I'm not very comfortable in batch, so I'd appreciate solutions that use Powershell or .Net or something that is more expressive than batch.
ScheduleJob
interface and once thePostBootSetup
finishes successfully it creates a file in a known path. When it runs again, it checks that know path and if a file exists - it exits without doing anything. Not ideal, but that's how we've done it on UNIX a long time ago (before they invented advanced crons, and systemd timers)