Trying to use Azure Custom Script Extension to run a background job or a powershell task scheduler

非 Y 不嫁゛ 提交于 2019-12-13 03:35:20

问题


Hi after my vm gets created I run the Azure Custom Script Extension which runs a powershell script. So the script does some basic tasks like create a share create some files and creates a powershell script and stored it on the vm. the last step in the custom vm extension script I have it attempt to setup a task scheduler job to call a powershell script that was created above and the script should end fromt he extension.

The problem isthe extension gets stuck in a running state and never stops. I wrote a log to find out the progress of the script and it shows it went through the whole script fine but it gets stuck in running and the task scheduler script does not run the job.

When i log in the vm manually I ran the script manually and it works fine. So I am not sure what is causing it to hang and what user it is running as the vm extension that is.I tried to run a powershell background job instead of the scheduler and I got the same symptoms above. The below code is the task scheduler I tried to run the vm custom extension that does not work when trying to set it up through the custom extension but runs fine manually setup from the vm.

     $jobname = "MasterFileWatcher"
     $script =  "c:\test\MasterFileWatcher.ps1"
     $repeat = (New-TimeSpan -Minutes 1)
     $action = New-ScheduledTaskAction –Execute "$pshome\powershell.exe" -Argument  "$script; quit"
     $duration = ([timeSpan]::maxvalue)
     $trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).Date -
     RepetitionInterval $repeat -RepetitionDuration $duration


     $msg = "Enter the username and password that will run the task"; 
     $credential = $Host.UI.PromptForCredential("Task username and 
     password",$msg,"$env:userdomain\$env:username",$env:userdomain)
     $username = $credential.UserName
     $password = $credential.GetNetworkCredential().Password
     $username = "$env:userdomain\testuser"
     $password = "testpass"
     $settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable -DontStopOnIdleEnd

     Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User $username -Password $password -Settings $settings

回答1:


I found out the issue.

I had to set the username as system so the task scheduler could run as Localsystem account

Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -User "System" -Settings $settings


来源:https://stackoverflow.com/questions/46329875/trying-to-use-azure-custom-script-extension-to-run-a-background-job-or-a-powersh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!