List Job Variables defined for all groups, jobs, and steps
This script creates a tab-delimited file listing the Job Variables defined for all groups, jobs, and steps.
This script lists the variables explicitly defined (or overridden) at each level; it does not list inherited variables. Note that jobs may be inheriting variables defined at other levels that are not included here:
- Server
- Queue
- Remote Agent
StringBuilder variableList=new StringBuilder();
void Main()
{
variableList.AppendLine("Group\tJob\tStep\tVariable\tValue");
using (var session = Scheduler.Connect(".", LoginAuthenticationType.Windows, "", ""))
{
using (var context = session.NewDataContext())
{
//fetch the root group
var root = context.GetJobGroup(null);
EnumerateVariables(root);
}
}
using(var file=File.CreateText(@"d:\temp\variablelist.txt"))
{
file.Write(variableList);
}
}
void EnumerateVariables(JobGroup group)
{
ListVariables(group, null, null, group.JobVariables);
//get all jobs in the group. Use FullFetch so their variables and steps are all retrieved in a single server call
var jobs=group.GetJobs(ObjectFetchOptions.FullFetch,false);
foreach(var item in jobs)
{
EnumerateVariables(group, item);
}
//get all the child groups. Use FullFetch so their variables and steps are all retrieved in a single server call
var childGroups=group.GetGroups(ObjectFetchOptions.FullFetch,false);
foreach(var item in childGroups)
{
EnumerateVariables(item);
}
}
void EnumerateVariables(JobGroup group, Job job)
{
ListVariables(group, job, null, job.JobVariables);
foreach (var step in job.Steps)
{
ListVariables(group, job, step, step.JobVariables);
}
}
void ListVariables(JobGroup group, Job job, JobStep step, ArcanaDevelopment.adTempus.Client.Collections.JobVariableCollection variables)
{
foreach(var item in variables)
{
WriteVariable(group, job, step, item);
}
}
void WriteVariable(JobGroup group, Job job, JobStep step, JobVariable variable)
{
variableList.AppendFormat("{0}\t{1}\t{2}\t{3}\t{4}\r\n", group?.Name ?? "", job?.Name ?? "", step?.StepNumber.ToString() ?? "", variable.Name,variable.Value);
}