Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
413 views
in Technique[技术] by (71.8m points)

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

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:	estMasterFileWatcher.ps1"
     $repeat = (New-TimeSpan -Minutes 1)
     $action = New-ScheduledTaskAction –Execute "$pshomepowershell.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	estuser"
     $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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...