Search code examples
azurepowershellterraformazure-vm-extension

Unable to create Task Scheduler using Azure VMExtention CustomScriptExtension


I am working on creating a task to setup a Task Scheduler job on Windows machine. I have a script that register Task scheduler, when I run the script from Terraform Customscript extension it is not giving any error or creating the schedule Task. Though I have tried to wright the stdout to a file, command is not giving any output after execution.

I have tried to run the same command on by login to machine and able to create task successfully.

Powershell command script name "TaskScheduler.ps1"

#Setup Task scheduler
echo "Settingup Schedule task" | Out-File -FilePath C:\Users\Script_output.log -Append
$taskTrigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Friday -At 8pm
$taskAction = New-ScheduledTaskAction -Execute "PowerShell" -Argument '-NoProfile -ExecutionPolicy Bypass -File "C:\Users\BackupScripts\backup.ps1"' -WorkingDirectory "C:\Users\BackupScripts" 

From Terraform I am executing the command as follow :

powershell.exe -ExecutionPolicy Bypass -File C:/Users/BackupScripts/TaskScheduler.ps1

Solution

  • This method tends to offer more consistent results. Let me outline the basic concept, steps involved, and provide an example of PowerShell script logic to demonstrate how this approach can be effectively utilized.

    • Note: Although I don't specifically use Terraform, I can confirm this works with PowerShell utilizing startup scripts for setting up the Task Scheduler job effectively on hundreds of computers.

    Essentially, you'll need to copy the exported XML file of the Task Scheduler job to the target machine and then execute PowerShell on that machine for it to function correctly. If you can accomplish these steps in your environment, this approach should work seamlessly—that's the basic concept.

    Steps Involved

    1. Create the Task Scheduler job on a machine, configuring all necessary options and properties.
    2. Export the created Task Scheduler job to an XML file, either manually through the UI or using PowerShell.
    3. Copy the XML file to a location accessible by the target machines, ensuring it can be transferred to the local file system by the process.
      • The example PowerShell uses a UNC path for the file copy operation on the target machine. If your process differs, omit using that specific logic.
        • I assume you're already experienced in copying files to target machines in your environment, but happy to help further there if needed.
    4. Execute PowerShell logic to import the Task Scheduler job from the XML file, and ensure it is enabled additional PowerShell logic.

    PowerShell

    $TN = "My Task Scheduler Job XYZ";
    If( !( Test-Path "C:\ProgramData\My Task Scheduler Job XYZ.xml" ) ){ Copy-Item "\\serverxyz\packages\SchedTasks\My Task Scheduler Job XYZ.xml" -Destination "C:\ProgramData" -Force; };
    $job = Get-ScheduledTask | ForEach-Object { Process { If ( $_.TaskName -eq $TN ) { $_ } }};
    If ( !$job ) { schtasks /CREATE /xml "C:\ProgramData\My Task Scheduler Job XYZ.xml" /TN $TN; };
    If ( (Get-ScheduledTask | ? { $_.TaskName -eq $TN }).State -ne "Ready" ) { Enable-ScheduledTask -TaskName $TN };
    Exit;