Applies to: Windows PowerShell 3.0,4.0,5.0
While writing the DSC configuration for some Jenkins slaves, I discovered the Register-ScheduledTask cmdlet only accepts string variables. This forced me to store my service account password as clear text, which made me cringe. I knew there had to be a better way, even if the cmdlet did not allow a credential object to be passed to it. In this post you’ll learn how to extract the password from a PS Credential object. I’ll store those properties in variables and then provide them to the Register-ScheduledTask cmdlet.
Create the Action
The below section of code creates the trigger for the ScheduledTask, I’ll use this later when I create the ScheduledTask. It simply calls the script named PowerShellscript.ps1 from C:\script and execute it.
Define the Trigger
Next I’m defining the trigger and storing it into a variable called $Trigger. I’m having it run upon startup, but delaying it 5 minutes after the machine is online.
Configure the Settings
This was the trickiest part, trying to determine which parameters matched the GUI settings. Below I’m telling the task to; not stop when the idle ends, setting the restart interval to 1 minute, setting the reset count to 10 tries, starting when available, and removing the idle timeout option. $Settings.ExecutionTimeLimit = “PT0S” is removing the idle timeout, that took a bit to figure out.
New PS Credential Object
Now I have to create a PS Credential object that I can extract out the password from. This was the fun part as I hadn’t dealt with this to much before. Mainly because most cmdlets allow for encrypted credentials to be passed to them… at any rate. Lines 1-3 create the PS credential object and line 4 creates a variable with the password in it. It’s a string, but at least it’s not stored in the script. Perhaps I should of titled the post secureish? If you don’t like the read-host prompt, you can turn this code into an advanced function and require a credential parameter. Once I finish mine, I’ll be sure to link it here.
Register the ScheduledTask
Lastly I have to create the ScheduledTask and then register it. If you just create it, it will only be stored in memory. Registering it is what adds it to the task scheduler.