Skip to content

Instantly share code, notes, and snippets.

@esell
Created July 21, 2016 19:38
Show Gist options
  • Save esell/63b69279ba38333658ce664f76d61380 to your computer and use it in GitHub Desktop.
Save esell/63b69279ba38333658ce664f76d61380 to your computer and use it in GitHub Desktop.
generate required data for Service Principal based Azure service connection
#!/bin/bash
##################################################
# Bash script to replace the powershell script referenced at:
# https://blogs.msdn.microsoft.com/visualstudioalm/2015/10/04/automating-azure-resource-group-deployment-using-a-service-principal-in-visual-studio-online-buildrelease-management/
# Assumptions are:
# - you have the xplat CLI installed and are logged into the subscription
# - you have the jq tool installed
# - you don't value safe bash scripts :)
##################################################
SUBNAME=$1
PASS=$2
SPNROLE="owner"
# from xplat cli
CONNECTIONNAME=$(azure account show --json "$1" | jq -r '.[0].name')
TENANTID=$(azure account show --json "$1" | jq -r '.[0].tenantId')
SUBID=$(azure account show --json "$1" | jq -r '.[0].id')
USERNAME=$USER
NEWGUID=$(uuidgen)
DISPLAYNAME="VSO.$USERNAME.$NEWGUID"
HOMEPAGE="http://$DISPLAYNAME"
IDENTIFIERURI=$HOMEPAGE
# Create a new AD Application
echo "Creating a new Application in AAD (App URI - $IDENTIFIERURI)"
APPID=$(azure ad app create -n "$DISPLAYNAME" --home-page "$HOMEPAGE" -i "$IDENTIFIERURI" -p "$PASS" --json | jq -r '.appId')
echo "APPID is: $APPID"
# Create new SPN
echo "Creating a new SPN"
SPNNAME=$(azure ad sp create --json $APPID | jq -r '.displayName')
echo "SPNNAME is: $SPNNAME"
# Assign role to SPN
echo "Waiting for SPN creation to reflect in Directory before Role assignment"
sleep 20
echo "Assigning role ($SPNROLE) to SPN App ($APPID)"
SPNASSIGN=$(azure role assignment create --roleName "$SPNROLE" --spn $APPID)
# Print the values
echo -e "\nCopy and Paste below values for Service Connection"
echo "***************************************************************************"
echo "Connection Name: $CONNECTIONNAME(SPN)"
echo "Subscription Id: $SUBID"
echo "Subscription Name: $CONNECTIONNAME"
echo "Service Principal Id: $APPID"
echo "Service Principal key: <Password that you typed in>"
echo "Tenant Id: $TENANTID"
echo "***************************************************************************"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment