Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Reading Notes #308

2017-12-02_00-10-58Suggestion of the week


Cloud


Programming


Miscellaneous


Reading Notes #302

Autumn

Cloud


Programming


Data


Miscellaneous




Lessons learned when deploying multiple databases to Azure with VSTS

It's had been a while since I worked into Visual Studio Team Services (VSTS), and it was a real pleasure to get back in that area. For the solution I was working on, we need to keep the current database up and running while deploying a new version. For this purpose, we decided to append the release number to database name (ex: MyDatabase363). In our Build and Release processes, we needed to identify which databases are from the last release. In this post, I will show what I did using an inline PowerShell script to get that number and set it as an environment variable so it can be accessible by other tasks.

To get started let's add a Azure PowerShell task to our build definition. In this post, I use a build process but of course this is also valid for release process. To find the task quickly, use the search text box. I will add two of those, one to get the number, the second to validate that this value is now set as an environment variable and readable from other tasks.
AddPowerShellTask

Now it's time to set the first task. Fill-out all the properties and select Inline Script as the Script Type. It should look like this.

InlineScript

Let's examine the code.
Get last Release Number
$matchingResources = Find-AzureRmResource -ResourceNameContains "mydatabase" -ResourceType "Microsoft.Sql/servers/databases"

$lastRelease = 0

ForEach($resource in $matchingResources)
{
    if ($resource.ResourceName -match '(\d)+$') {
        if($lastRelease -lt $matches[0]){
            $lastRelease = $matches[0]
        }
    }
}
Write-Output "The last release number is:  $lastRelease"
Write-Output ("##vso[task.setvariable variable=lastReleaseNumber;]$lastRelease")
On the first line, I use the Azure PowerShell commandlet Find-AzureRmResource1 to get an array of all the databases currently online in my resource group that contains a specific string. In this case, it's the name of the database without the release number. Then I will loop through all returned resources and using a very simple Regex to extract the release number and keep the biggest one (the last release).

To close that script we have two outputs. The first one is to give feedback in the logs, because it's always good to have some information there. The second one look more complicated, but if you split it, it's easier to see what's happening. In fact, we are producing a VSTS (previously called Visual Studio Online this is why it's VSO) command to initialize a variable ##vso[task.setvariable variable=lastReleaseNumber;] And of course, assign to it our last release number $lastRelease

To validate that we really successfully found our last release number and assigned it to a variable, let's try to read it back but from another step. That will be easily done this code in the other step created:
Validate the last Release Number
$number = $env:lastReleaseNumber

Write-Output "Confirmation, the last Release Number is:  $number "
The only thing missing before we can run our test is to create that environment variable. To to it simply go in the Variables tab and add it there.

env-variable

It's all set, run the build and you should see something similar in your logs.

trace



References




Reading Notes #293

IMG_20170813_103816-EFFECTSCloud


Programming




Reading Notes #285

IMG_20170614_194330Cloud

Programming

Suggestion of the week

Miscellaneous



Reading Notes #284

IMG_20170609_092421Cloud

Programming

Miscellaneous




Reading Notes #283

June9Suggestion of the week


Cloud


Programming


Databases


Miscellaneous


Reading Notes #282

favicon[1]Cloud


Programming


Reading Notes #280

IMG_20170511_082902Cloud


Miscellaneous


Reading Notes #279

IMG_20170506_070903Cloud


Programming

  • Contributing to .NET for Dummies (Rion Williams) - Another post where the author shares his experience (I love those. That's the real life); this one about participating in an open-source project.

Databases


Miscellaneous



Reading Notes #267

IMG_20170208_201247Cloud


Programming


Miscellaneous




Reading Notes #264

2017-01-22_21-13-48Cloud


Programming

  • Introducing Docker 1.13 (Docker Core Engineering) - This post summarizes all the great features added in the new release and shows again why Docker is such a fantastic tool in the containers' world.

Databases


Miscellaneous


Need to Nuke an Azure Subscription?

(Ce billet en aussi disponible en français.)


I use very intensely my my.visualstudio (aka MSDN) Azure subscription, to create content for a demo or just to try new feature. So frequently I need to do some cleaning.

multi-resourceGroup-boom

Here a little script that will completely delete all resources of every resources group inside a specific subscription. To be able to execute this script you will need Azure PowerShell cmdlets.

The script asks you to login-in then list all the subscriptions that this account has access. Once you specify which one, it will list all the resource grouped by resource group. Then as a final warning, it will require one last validation before nuking everything.

Be careful.

#================================================================
#= Very dangerous interactive script that delete all rescources 
#= from all rescourcegroup in a specific subscription
#================================================================

# How to install and configure Azure PowerShell
# https://docs.microsoft.com/en-us/powershell/azureps-cmdlets-docs/

# Login
Login-AzureRmAccount 

# Get a list of all Azure subscript that the user can access
$allSubs = Get-AzureRmSubscription 

>$allSubs | Sort-Object Name | Format-Table -Property ame, SubscriptionId, State

$theSub = Read-Host "Enter the subscriptionId you want to clean"

Write-Host "You select the following subscription. (it will be display 15 sec.)" -ForegroundColor Cyan
Get-AzureRmSubscription -SubscriptionId $theSub | Select-AzureRmSubscription 

#Get all the resources groups
$allRG = Get-AzureRmResourceGroup

foreach ( $g in $allRG){
    Write-Host $g.ResourceGroupName -ForegroundColor Yellow 
    Write-Host "------------------------------------------------------`n" -ForegroundColor Yellow 
    $allResources = Find-AzureRmResource -ResourceGroupNameContains $g.ResourceGroupName
    if($allResources){
        $allResources | Format-Table -Property Name, ResourceName
    }else{
        Write-Host "-- empty--`n"
    } 
    Write-Host "`n`n------------------------------------------------------" -ForegroundColor Yellow 
}

$lastValidation = Read-Host "Do you wich to delete ALL the resouces previously listed? (YES/ NO)"
if($lastValidation.ToLower().Equals("yes")){
    foreach ( $g in $allRG){
        Write-Host "Deleting " $g.ResourceGroupName 
        Remove-AzureRmResourceGroup -Name $g.ResourceGroupName -Force -WhatIf
    }
}else{
    Write-Host "Aborded. Nothing was deleted." -ForegroundColor Cyan
}




The code is also available on Github: https://github.com/FBoucher/AzurePowerTools


Reading Notes #259

three-amigos-mainCloud


Programming




Reading Notes #255

microsoft_studioCloud


Programming


integration


Miscellaneous




Reading Notes #246

IMG_20160826_115405Cloud


Programming


Databases


Miscellaneous


Reading Notes #243

valutoBusinessSuggestion of the week


Cloud


Programming


Miscellaneous



Reading Notes #242

mapCloud


Programming

  • Exploring dotnet new with .NET Core (Scott Hanselman) - I discover the different types in dotnet new command during Julie Lerman's talk at DevTeach and now this post shows a list of incredible opportunities.

Miscellaneous


Reading Notes #236


WhyAzureCLISuggestion of the week


Cloud


Programming


Miscellaneous

  • Happiness is DevOps’ Cornerstone (Alexandre Brisebois) - Interesting post that asks a lot of questions... I would like to see some graph or pie chart about our answers.
  • 5 Habits that Help Code Quality (View all posts by Erik Dietrich) - Yet another post about how to code better, but this one is refreshing. It explains why the opposite would be harmful and also give us a training plan for better chance of success.
  • What’s in your highlights folder? (Marc Gagne) - Because life is not only mistakes and bad luck.Here good tips to help you giving some sunshine into your life when needed.

Reading Notes #235

ImaginationSuggestion of the week


Cloud