Trigger Azure Automation with a Teams team request Form!

teams

Lee Ford’s blogpost on how to create a Teams team through Forms/Flow/Graph inspired me to take a closer look at the possibilities. I wanted to trigger an Azure Automation Runbook through a Form/Flow. I still wanted to use Graph API in the runbook. You may ask why? I think its easier to have an overview and update code/requests in a Runbook rather than small text fields in a Flow. In Azure Automation you also have version control for your code.

At first, I went down the rabbit hole and looked at using the Azure Rest API. Just finding the API link was complicated. There were even different version numbers. After some trial and error, I did a new Google search and stumbled upon Sarah Lean’s blogpost on how to trigger an Azure Automation Runbook with Flow. Now I had what I needed.

First of all, we will need to have an Azure application to authenticate trough when doing Graph API requests.

Go to portal.azure.com and select “All Services”.

flow1

Search for and select “App Registrations”.

flow2.jpg

Open the preview experience.

flow3.jpg

Press “New registration”.

flow4.jpg

Fill out the form like this and press “Register”.

flow5.jpg

Take note of Client ID for use in the script later on. Press “Add a Redirect URI”.

flow6.jpg

Select this URL and press “Save”.

flow7.jpg

Go to “API Permissions” and press “Add a Permission”.

flow8.jpg

Select “Microsoft Graph”.

flow9.jpg

Here I would choose “Application Permissions”. This because I don’t want to rely on a user object to authenticate against Azure AD with Graph API. This way we can also use an application secret instead of a password.
flow10.jpg

For creating a team and adding the requester as a owner, you will need these two permissions. Press “Add permissions”.

flow11.jpg

Press “Grant admin consent for …..”.

flow12.jpg

Press “Yes”.

flow13.jpg

Go to “Certificates & secrets”.
Press “New client secret”, enter description and press “Add”.
flow14.jpg

Copy the value of the secret.

flow15.jpg

Now we need to set up our Runbook in Azure Automation, so open “All services” and search for “Automation Accounts” and press it.

flow16.jpg

Press “Add”.

flow17.jpg

Give your Automation Account a name. Select Subscription, Resource group and Location.
Set “Create Azure Run As account” to Yes.. Press “Create”.

flow18.jpg

Press “Refresh” and open your newly created automation account.

flow19.jpg

Go to “Variables” and press “Add a variable”.  Here we are going to store our application secret as an encrypted variable. Fill out and press “Create”.
flow20.jpg

Go to “Runbooks” and press “Create a runbook”.
Give it a name, select PowerShell as type and press “Create”.

flow21.jpg

After pressing “Create” you get redirected into edit of your runbook.
Copy the script under into your runbook editor.

Here you see two parameters we need and that we enter client id, tenant id and get the client secret.
Press “Save” and “Publish”.

flow22.jpg
Go back into the editor of your runbook and press “Test pane”.
Now we are going to confirm that the script is working as it should.
Enter a team name and the UPN of a user.

flow24.jpg

Press “Start”. When you see the green text “Completed”, then everything should be OK. To be sure, you could log on to Teams and see if the team was created.

flow25.jpg

 

Now we need to create the Form to receive the request from, so go to https://forms.office.com/Pages/DesignPage.aspx.

Create a new form.

flow26.jpg

Give the form a name and create a text field with the name Team name that is set to required. Press “Share” and copy the URL for the form so you have it when testing the flow later on.

flow27

Go to https://flow.microsoft.com and after logging in go to “My flows”.
Press “New” and select “Create from blank”.

flow28.jpg

Press “Create from blank”.

flow29.jpg

Search for “When a new response is submitted” and select the Microsoft Forms trigger.

flow30.jpg

Select the Form you created and press “New step”.

flow31

Select “Get response details”.

flow32.jpg

 

Select Form Id and set Response Id to “List of response notifications Reponse Id”.

flow33.jpg

Press “Add an action” and select “Approval -> Start and wait for an approval”.

flow34.jpg

Set it up like this:

flow35.jpg

Press “Add an action” and select Condition. Fill out like this:

flow36.jpg

Under “If yes”. Select “Create job” (Azure Automation) fill out and press “Add an action”. Select “Send an email” and fill out.

flow37.jpg

Under “If no” you add an email response like this:

flow38.jpg

Save the Flow and press “Test” in the upper right corner.
Select “I’ll perform the trigger action” and press “Save & Test”.

flow39.jpg

Go to your Form, fill out and submit.
Now you should have received an email where you are asked to approve the team creation.

Press “Approve” and then “Submit”.flow40.jpg

Go back to your Flow test.
If everything went well, it should look like this:

flow41.jpg

Now you have automated the creation of teams through Forms/Flow/AzureAutomation/GraphAPI. This way you can ease the workload of your IT department and let a manager or a similar role decide on which teams are created.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s