尝试使用Azure自定义脚本扩展来运行后台作业或Powershell任务计划程序 [英] Trying to use Azure Custom Script Extension to run a background job or a powershell task scheduler

查看:70
本文介绍了尝试使用Azure自定义脚本扩展来运行后台作业或Powershell任务计划程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建虚拟机后,您好,我运行运行Powershell脚本的Azure自定义脚本扩展.因此,该脚本执行一些基本任务,例如创建共享,创建一些文件,创建powershell脚本并将其存储在vm上.自定义vm扩展脚本的最后一步,我尝试设置任务调度程序作业以调用上面创建的powershell脚本,该脚本应从扩展名结束.

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.

当我手动登录虚拟机时,我手动运行了脚本,并且运行正常.所以我不确定是什么导致它挂起,以及它是作为vm扩展运行的用户.我试图运行Powershell后台作业而不是调度程序,但上面出现了相同的症状.下面的代码是我尝试运行vm自定义扩展的任务计划程序,当尝试通过自定义扩展对其进行设置时,该自定义扩展不起作用,但是可以从vm手动运行正常.

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

推荐答案

我发现了问题所在.

我必须将用户名设置为system,以便任务计划程序可以作为Localsystem帐户运行

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

这篇关于尝试使用Azure自定义脚本扩展来运行后台作业或Powershell任务计划程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆