Recently, as I was trying out Azure Stack (which, by the way, looks promising in my sight), I had to repeat installation more than twice. At one time I had to format the whole machine because only 2 of the 11 required roles (Virtual Machines) could startup. Since the original setup had already succeeded, including the ability to send usage information to Azure using Azure Stack Bridge, there were two things I needed to clean up before a fresh setup; the Azure AD service principals and the Azure Bridge connection.
Disconnecting the Azure Bridge subscription can be done using a PowerShell command
Remove-AzsRegistration as directed here. However, if you cannot run the command because certain roles are not running or do have not access to the host machine at all. You can still delete the resource group.
Unfortunately, as of the time of this writing, there are no instructions about removing the service principals created in the Azure AD tenant. Did Microsoft forget this?
The first time I did the cleanup manually by deleting them one by one. The second time I could not stomach it so I wrote a script.
- The script requires a user with administrator role in the tenant
- The service principals created have multi-tenancy enabled. Azure AD does not allow deleting applications with multi-tenancy enabled so you have to first disable it.
- If you had opened the Azure Active Directory tab in azure portal, you will need to refresh the whole page after running the script.
- The script can be run from any machine with PowerShell and Azure tools installed. This can be your development machine or your host machine.
Assuming you have saved the script locally on your machine:
- Open a PowerShell window
- Navigate to the folder where you saved the script
- Execute the command below:
You will be prompted to sign in to your tenant with an account having administrative rights. You should expect the following output:
That’s all. Happy hacking