none
Retrieving Azure Runbook job status from c# application RRS feed

  • Question

  • Hi am currently making a test framework in visual studio, currently working with API, am making a post to an endpoint, this endpoint executes a Runbook through a webhook. I will like to validate that the runbook status should be running, is there a way to valdate the runbook its running from visual studio? 
    Friday, June 28, 2019 5:27 PM

Answers

All replies

  • Here is the link to the article which might help you in retrieving the Runbook job status.



    Monday, July 1, 2019 6:09 AM
    Moderator
  • Cool, but an example will be more than useful, am trying to find documentation with no success.
    Wednesday, July 17, 2019 6:24 PM
  • Here are some code snippets that might help you.

    1) Create a service principle in AD. And use the values of tenantId,ClientId and ClientKey from service principle to get authorization token.

    private static string GetAccessToken(string tenantId, string clientId, string clientKey)
            {
                Console.WriteLine("Begin GetAccessToken");
                string authContextURL = "https://login.windows.net/" + tenantId;
                var authenticationContext = new AuthenticationContext(authContextURL);
                var credential = new ClientCredential(clientId, clientKey);
                var result = authenticationContext.AcquireTokenAsync("https://management.azure.com/", credential);
                if (result == null)
                {
                    throw new InvalidOperationException("Failed to obtain the JWT token");
                }
                string token = result.Result.AccessToken;
                Console.Write(token);
                return token;
    
            }

    2) Below snippet helps you for quickstart in retrieving the list of jobs that are available in respective automation account. You can modify it as per your requirement.

    private static string RetrieveAzRunbookStatus(string token)
            {
    
                string URL = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{RGName}/providers/Microsoft.Automation/automationAccounts/{automationaccount}/jobs?api-version=2017-05-15-preview";
                HttpClient client = new HttpClient();
                client.DefaultRequestHeaders.Remove("Authorization");
                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                HttpResponseMessage httpResponseMessage = client.GetAsync(URL).Result;
    
    
                if (!httpResponseMessage.IsSuccessStatusCode)
                {
                    var response = httpResponseMessage.Content.ReadAsStringAsync().Result;
    
                }
                else
                {
                    var response = httpResponseMessage.Content.ReadAsStringAsync().Result;
                    var mylist = JsonConvert.DeserializeObject<ResponseClass>(response);
                }
    
                return httpResponseMessage.StatusCode.ToString();
    
            }

    Please note to get the details of specific job, you need send the job name or jobid .

    Hope this helps!




    Monday, July 22, 2019 6:28 AM
    Moderator
  • How do I get the token to send on the request?
    Thursday, August 22, 2019 6:20 PM
  • The first step contains GetAccessToken method that retrieves the token, you can pass the same token to the RetreiveAzRunbookStatus method.
    Friday, August 23, 2019 5:02 AM
    Moderator
  • Hi I found the answer on this video, I hope it might help anyone else in the future

    https://www.youtube.com/watch?v=ujzrq8Fg9Gc

                 
    Friday, August 23, 2019 8:02 PM