Creating a PowerShell Script to Post Deployment Updates for Azure DevOps in AppDynamics

Application monitoring is essential to any IT operation. From a DevOps perspective, it’s also important to know when releases fail or succeed. Yet, this functionality isn’t set up out of the box. You’ll need to set it up yourself. To do do, you can use AppDynamics, a tool for checking application health and performance. The tool also converts data into useful business analytics, which allows you to make informed decisions. Well, what if said you could also optimize the process using PowerShell?

Here, I’ll help you set up your application deployment monitoring. You’ll also learn how to use a single PowerShell script in your release pipeline. That’ll update the deployment status in AppDynamics and inform your development team. 

Finally, I’ll show you how to write a custom PowerShell script that creates a task in Azure DevOps. This task logs the deployment event and shows users through the release pipeline. This way, AppDynamics users know when the applications are ready across different environments. Let’s dive in and get started! 

Picture of a monitor showing lines of code in an open script.
Scripting makes the digital world go round!

Getting Started with Powershell Script

Ensure you read the AppDynamics documentation to understand all the parameters and options. Before writing the PowerShell script, you may also need to work with your security team. For example, you may need to get login credentials to work with the AppDynamics API and make post calls. Be sure you also get your credentials before trying to call the API. If you don’t, it’ll block you. 

This PowerShell script has 5 steps, which I’ll take you through one-by-one

  1. Initialize the variables 
  2. Update variables per environment
  3. Create an access token
  4. Set the access token and create an authentication header 
  5. Record the deployment event

1. Initialize the Variables 

Always write your code neatly, especially when several people are working on it. Use comments to state what each code section does. 

Image of the initializing variables code.
Initialize your variables.

In the code block above, you need to account for:

  • The environment: Dev, Test, Stage, or Prod
  • Your application name
  • The AppDynamics environment, which is either Prod or Non-Prod (np)
  • The clientName, which is the API you want to call
  • The client secret, which is your login to the API. You might get this from security or someone working with this API.  
  • AppDynamics assigns the AppId number to the application based on the environment. To get this, you’ll need to query AppDynamics. In my experience, Postman doesn’t work with the AppDynamics API

You’ll need to use a terminal and curl commands. Use the following command to look up the app ID:

curl -H “Authorization:Bearer <AUTH_TOKEN>” “https://np-appdynamics.<your url to rest apis>”

The EventSev is a list of different notifications that AppDynamics labels. Once you’ve set your variables, you can then move along to handling the environments. 

2. Update Variable Values per Environment   

To handle environment variables, you can write a switch case. Otherwise, you can use an if-else statement. Let’s look at the code block below to see how to handle this.

Image of the variable update code
Update your variable values.

In each environment, you’ll need to set the appID to the corresponding AppDynamics ID code. In the variables up top, I already set Dev to 8 as the default. Note that for production deployments, you’ll also need to use the prod AppDynamics environment and its own client secret. After you set up the environment handling, as shown above, you can create the access token to hit the API.

3. Create Access Token 

In this step, I’ll create the access token. This will allow you to access the API and make a post call. It’ll also create a bearer token that’s sent back to the API to return data. Check out the code block below to see what you’ll need to create the access token. 

Image of the access token creation code
Create your access token.

You’ll want your accessToken to use the cmdlet Invoke-RestMethod. Arguments will post and use JSON with a URI that points to the AppDynamics environment. The access token URL should also send the client secret, client name, and appID. Note the syntax for the URI. It only takes one mistake to cause an error, so you should be careful. Now, let’s get the bearer token and set it to go in the final call. 

4. Set the Access Token and Create Authentication Header 

Image of the access token and authentication header creation code.
Access token, Authentication header.

The access token in the last step above will now become the bearer token. This posts back in the final call to the API. Here, I set the accessToken equal to the token variable. Drop that into the head variable for authorization

Now, you’ve done this step, and you’re ready for the final call to make the post to AppDynamics to show that our application is working. 

5. Record the Deployment Event

Now, I’m ready to find out if my application has deployed or failed. In the code block below, I’ll set the Response to call the restMethod. This time, I’ll add it to the header which contains the bearer token to access the API. Notice your new URI also has a lot of the variables you created earlier. It also has the parameters that AppDynamics provides in its documentation.

Image of the deployment event code.
Finally, the big deployment event.

After the response comes back, you’ll need to handle it with an if-else statement stating whether the deployment succeeded or failed. 

Final Thoughts 

Each line is important for a successful implementation, so let’s recap our script. First, it’s important you set your variables to cut down the amount of code you need to write. After setting variables, you’ll then find your AppIDs for each environment. This enables you to assign deployments to all variables. 

After staging our environments and variables, you’ll move on to setting up your access tokens. Adding client secrets and setting the token in the header allows you to make the API call. Once you’re certain you can call the API, you can create the URI. You’ll also need the help of the AppDynamics documentation for that. This makes a post to the API, and then handles the successful or unsuccessful events. 

AppDynamics is a great tool to use to manage your applications’ performance. If you’re working in a large company and have a lot of applications to manage, it’s important to keep track of deployments. This script will be very useful in ensuring your applications are up and running.

FAQ

Why can’t I use Postman?

At the time of writing, AppDynamics has some bugs that won’t allow Postman to receive responses. The best way around this is to use curl in a terminal. This allows you to contact the Appdynamics API.

Why do I need to monitor my Apps?

If you don’t have many applications, then you might think that you don’t need any monitoring software. Yet, a tool like AppDynamics allows you to check your applications in real-time. You’ll want to know if a production outage is present. Here, you can monitor all your applications easily from a single dashboard.

Do I need to use Powershell?

No, you don’t. Still, it depends on what DevOps platform you’re using. If you’re using Azure, you’re using a Microsoft product, so PowerShell is the right scripting language.

Should I write my code inline or pull it from a repo?

In Azure DevOps, if you’re using the classic pipelines, you have the option to insert the code inline. The best practice is to create a DevOps-related PowerShell scripts repository. Then, you can pull in code from there.  

Can I use something other than AppDynamics?

Sure, you can use another monitoring application, like Prometheus. That said, you’ll have to modify the code in this tutorial to work with another application. 

Resources 

Highly Available Applications

Find out about what highly available applications can offer you here.

How to Check your Windows Server Uptime with Powershell

Learn how to keep track of your server uptime using Powershell here

How to Schedule Powershell Scripts

Learn more on how to schedule your powershell scripts to run when you want them here.

Mobile App Performance Metrics: Why they can Help you Succeed 

Discover how to monitor your mobile apps here.

Bulk Group Management in Azure AD and Microsoft 365

Check out some cool ways to manage bulk group AD changes here.

About The Author

2 thoughts on “Creating a PowerShell Script to Post Deployment Updates for Azure DevOps in AppDynamics”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top