我在VSTS中找到了一个插件azure app service webjob,但是我无法启动/停止webjob。请帮助我们。我在VSTS中运行任务时遇到以下错误。
错误:
2017-09-25T12:26:37.3203696Z Preparing task execution handler.
2017-09-25T12:26:43.2660796Z Executing the powershell script:
d:\a\_tasks\Start_cbebb02b-11c6-4e24-b5a8-0b51366d51b7\0.2.5\Start.ps1
2017-09-25T12:26:43.6830868Z ##
[error]System.Management.Automation.ParentContainsErrorRecordException: At
D:\a\_tasks\Start_cbebb02b-11c6-4e24-b5a8-0b51366d51b7\0.2.5\Start.ps1:13
char:16
2017-09-25T12:26:43.6830868Z + $WebAppName
2017-09-25T12:26:43.6830868Z + ~
2017-09-25T12:26:43.6830868Z Missing ')' in function parameter list.
2017-09-25T12:26:43.6830868Z
2017-09-25T12:26:43.6830868Z At D:\a\_tasks\Start_cbebb02b-11c6-4e24-b5a8-
0b51366d51b7\0.2.5\Start.ps1:17 char:1
2017-09-25T12:26:43.6830868Z + )
2017-09-25T12:26:43.6830868Z + ~
2017-09-25T12:26:43.6830868Z Unexpected token ')' in expression or
statement.
答案 0 :(得分:3)
使用Invoke-AzureRMResourceAction
命令:
代码:
触发WebJob:
Invoke-AzureRmResourceAction -ResourceGroupName XXX -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName [web app name]/[web job name] -Action run -ApiVersion 2015-08-01 -force
连续WebJob:
Invoke-AzureRmResourceAction -ResourceGroupName XXX -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName [web app name]/[web job name] -Action [start/stop] -ApiVersion 2015-08-01 -Force
包含多个webjobs的简单示例脚本:
param(
[object[]]$webjobs,
[string]$website,
[string]$rg
)
foreach($wj in $webjobs){
if($wj.typeName -eq "continuous")
{
Invoke-AzureRmResourceAction -ResourceGroupName $rg -ResourceType Microsoft.Web/sites/ContinuousWebJobs -ResourceName "$website/$($wj.name)" -Action start -ApiVersion 2015-08-01 -Force
}
else{
Invoke-AzureRmResourceAction -ResourceGroupName $rg -ResourceType Microsoft.Web/sites/TriggeredWebJobs -ResourceName "$website/$($wj.name)" -Action run -ApiVersion 2015-08-01 -force
}
}
指定参数:
-webjobs @(@{"name"="webjob1";"typeName"='continuous'},@{"name"="webjob2";"typeName"='continuous'},@{"name"="webjob3";"typeName"='triggered '}) -website XX -rg XX
答案 1 :(得分:0)
public static string token = System.Configuration.ConfigurationManager.AppSettings["token"];
public static string continuouswebjobs = System.Configuration.ConfigurationManager.AppSettings["continuouswebjobs"];
public static string triggeredwebjobs = System.Configuration.ConfigurationManager.AppSettings["triggeredwebjobs"];
public static string hostName = System.Configuration.ConfigurationManager.AppSettings["DLUHostName"];
public static void StopContinuousWebJob(TextWriter log)
{
HttpClient request = new HttpClient();
request.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
request.DefaultRequestHeaders.Add("Authorization", token);
if (!string.IsNullOrEmpty(continuouswebjobs))
{
string[] jobsArray = continuouswebjobs.Split(',');
for (int i = 0; i < jobsArray.Length; i++)
{
HttpResponseMessage response = request.PostAsync(new Uri(hostName + "continuouswebjobs/" + jobsArray[i] + "/stop"), null).Result;
if (response.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Job " + jobsArray[i] + " stopped successfully.");
}
}
}
}