I have an deployment template for an azure virtual machine scale with this extensionProfile:
"extensionProfile": {
"extensions": [
{
"name": "customScript",
"location": "[resourceGroup().location]",
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"settings": {
"fileUris": [
"[concat(parameters('customInstallScriptLocation'), parameters('customInstallScriptFileName'))]"
]
},
"typeHandlerVersion": "1.8",
"autoUpgradeMinorVersion": true,
"protectedSettings": {
"commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File \"', parameters('customInstallScriptFileName'), '\" -adminUsername ', parameters('adminUsername'), ' -adminPassword ', parameters('adminPassword'))]"
}
}
}
]
}
When deploying the vmss the custom script is executed, commands like Invoke-WebRequest and Start-Process work without a problem. But Register-ScheduledJob doesn't work. When I connect to the remote desktop and run this install script manually everything works properly.
This is the code that fails when not running it manually:
$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:45
Register-ScheduledJob -Trigger $trigger -FilePath $installDir\natPuncherStartupScript.ps1 -Name NPSSOnStartup >> $log
logAndClearErrors $true
The error is:
Register-ScheduledJob : An error occurred while registering scheduled jobdefinition NPSSOnInstall to the Windows Task Scheduler. The Task Scheduler error is: (32,4):UserId:.
I also tried:
$trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:45
Register-ScheduledJob -Credential $credential -Trigger $trigger -FilePath $installDir\natPuncherStartupScript.ps1 -Name NPSSOnStartup >> $log
logAndClearErrors $true
But now an error shows that the username/password is invalid. Also when running this manually from remote desktop this works fine.
What am I missing? Why can't I schedule a job from a customscriptextension?
According to your description, I could reproduce the errors you encountered by using
Register-ScheduledJob
.From the official document about Register-ScheduledJob.
I assumed that this issue is possibly casued by the user account that runs this scheduled job. After some trials, it could work as expected on my side and the scheduled job could be registered to my Azure VM by using ARM template. Here is the core command, you could refer to it.