How to get Microsoft Custom Script Extension that runs a ps1 file during vm setup to create a task in task scheduler.

Jerry Huo 0 Reputation points
2024-05-13T00:41:13.3266667+00:00

So during setup of a vm, I'm using Microsoft Custom Script Extension to run a ps1 file to install necessary files (like vscode). I also want it to set a task in task scheduler that cleans up files periodically, but the task never gets set up.

example task setup code

$command = 'del /S /Q C:\Users...\AppData\Local\Temp* && for /D %x in (C:\Users...\AppData\Local\Temp*) do @rd /S /Q "%x"'

$trigger = New-ScheduledTaskTrigger -Daily -At "00:00"

$action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument "/c $command"

$taskName = "CleanupTempFiles"

try {

Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -RunLevel Highest -ErrorAction Stop

Write-Host "Scheduled task '$taskName' created successfully."

} catch {

Write-Host "Failed to create scheduled task '$taskName'. Error: $_"

}

However, if I run this chunk in powershell after I log into the vm, it sets up the task successfully.

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,289 questions
{count} votes

1 answer

Sort by: Most helpful
  1. vipullag-MSFT 25,041 Reputation points
    2024-05-13T06:01:43.4966667+00:00

    Hello Jerry Huo

    Welcome to Microsoft Q&A Platform, thanks for posting your query here.

    If the task is created successfully when you run the PowerShell script manually after logging into the VM, but not during VM setup using the Custom Script Extension, it's possible that the task is being created in the wrong context or at the wrong time during VM setup.

    To troubleshoot this issue, you can try the following steps:

    1. Check the output of the Custom Script Extension to see if there are any errors or warnings related to the task creation.

    Check the Task Scheduler to see if the task was created but disabled or not triggered correctly. You can open Task Scheduler by searching for "Task Scheduler" in the Start menu or by running the taskschd.msc command.

    Try running the PowerShell script with the -Verbose parameter to get more detailed output. For example:

    $command = 'del /S /Q C:\Users...\AppData\Local\Temp* && for /D %x in (C:\Users...\AppData\Local\Temp*) do @rd /S /Q "%x"'
    
    $trigger = New-ScheduledTaskTrigger -Daily -At "00:00"
    
    $action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument "/c $command"
    
    $taskName = "CleanupTempFiles"
    
    try {
    
    Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -RunLevel Highest -Verbose -ErrorAction Stop
    
    Write-Host "Scheduled task '$taskName' created successfully."
    
    } catch {
    
    Write-Host "Failed to create scheduled task '$taskName'. Error: $_"
    
    }
    

    This script adds the -Verbose parameter to the Register-ScheduledTask cmdlet to get more detailed output about the task creation process.

    1. Try running the PowerShell script with a delay or a retry mechanism to ensure that the Task Scheduler service is running and ready to accept new tasks. For example:

    $command = 'del /S /Q C:\Users...\AppData\Local\Temp* && for /D %x in (C:\Users...\AppData\Local\Temp*) do @rd /S /Q "%x"'

    $trigger = New-ScheduledTaskTrigger -Daily -At "00:00"

    $action = New-ScheduledTaskAction -Execute 'cmd.exe' -Argument "/c $command"

    Hope this helps.

    1 person found this answer helpful.