Hello!

Got a couple of longer posts that need a bit more research into them, but for now here is some random Azure RM PowerShell scripts I’ve been using:

Been using Bamboo lately, and there’s unlike VSTS where you can select a task that you can configure to use a specific service principal and select the subscription, you have to log in yourself. However it’s important to logout of the principal when you have finished as sessions seem to persist across build steps in completely different builds, which is a bit of a pain. So below is a simple script to continue to logout until there’s no more sessions. And then users can log back in. You’ll notice the script checks if there’s a context to Azure, and this is because you may want to log out for a build, but not locally, elsewise you have to log in every time, which is equally annoying.

if ($FirstLogoutOfAllSubscriptions) {
    do {
        $LogoutAzureRmAccountResponse = Logout-AzureRmAccount
    }
    until(
        $null -eq $LogoutAzureRmAccountResponse
    )
}

if ([string]::IsNullOrEmpty($(Get-AzureRmContext).Account)) {
    if ($PSBoundParameters.ContainsKey('clientID') -eq $true) {
        $SecurePassword = $key | ConvertTo-SecureString -AsPlainText -Force
        $cred = new-object -typename System.Management.Automation.PSCredential `
            -argumentlist $clientID, $SecurePassword
        Add-AzureRmAccount -tenantId $tenantID -Credential $cred -ServicePrincipal -subscriptionId $subscriptionId
    }
    else {
        Add-AzureRmAccount
    }
}