Kategorie: Developer

  • Creating a Power BI Streaming Dataset from the command line

    The Problem

    Setting up a Power BI Streaming DataSet in the portal can be a cumbersome task prone to errors due to misspelling and incorrect casing. In a recent customer project, I encountered this challenge and developed a PowerShell script to automate and streamline the process.

    The Solution: Automate Power BI DataSet Creation With PowerShell

    ```ps
    param(
        [Parameter(Mandatory=$true)]
        [string]$newWorkspaceName,
        [Parameter(Mandatory=$true)]
        [string]$user,
        [Parameter(Mandatory=$true)]
        [SecureString]$password
    )
    
    # User credentials for
    $credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $user, $password
    
    # Connect to AAD
    $azureProfile = Connect-AzAccount -Credential $credential
    
    # Get an AccessToken to the Power BI service
    $accessTokenDetails = Get-AzAccessToken -ResourceUrl 'https://analysis.windows.net/powerbi/api' -DefaultProfile $azureProfile
    $headers = @{
        'Authorization' = "Bearer $($accessTokenDetails.Token)"
    }
    
    # Create a new workspace
    $workspaceName = "Workspace $newWorkspaceName"
    $url = "https://api.powerbi.com/v1.0/myorg/groups?workspaceV2=true"
    $body = '{
        "name": "' + $workspaceName + '"
    }'
    $response = Invoke-RestMethod -Method 'Post' -Headers $headers -Uri $url -Body $body -ContentType 'application/json'
    if ($null -ne $response.error) {
        Write-Host "Error creating workspace: $($response.error.code) - $($response.error.message)"
        exit
    }
    
    $workspaceId = $response.id
    Write-Host "Workspace created: $($workspaceId)" 
    # Create Internal dataset
    $url = "https://api.powerbi.com/v1.0/myorg/groups/"+ $workspaceId + "/datasets"
    $body = '{
        "name": "' + $newWorkspaceName + ' (internal)",
        "defaultMode": "Streaming",
        "tables": [
            {
                "name": "roll",
                "columns": [
                    {"name": "timestamp", "dataType": "DateTime" },
                    {"name": "field1", "dataType": "String" },
                    {"name": "field2", "dataType": "Int64" },
                    {"name": "field3", "dataType": "Int64" }
                ]
            }
        ]
    }'
    $response = Invoke-RestMethod -Method 'Post' -Headers $headers -Uri $url -Body $body -ContentType 'application/json'
    $internalDataSetId = $response.id
    Write-Host "Internal dataset created: $($internalDataSetId)"

    ## Summarize

    You can utilize this code to automate and replicate the creation of Power BI Datasets directly from the command line, eliminating the need for manual intervention.

  • Clone your Azure Resource Group with ARM and Bicep

    One common task for cloud engineers is setting up an environment in Azure for solution development. During the development and prototyping phases of an architecture, various manual tasks are performed to test different aspects. However, when it comes to recreating this environment in another resource group, the manual approach becomes impractical. This is where the use of Infrastructure as Code (IaC) becomes crucial, employing tools such as ARM, Terraform, or Bicep.

    Retrieving all the settings from your „click-and-test“ runs isn’t a straightforward process, and there’s a risk of forgetting crucial elements. To streamline this, I often rely on a simple script. This script allows me to extract the entire ARM/Bicep environment from one resource group and apply it to another for testing purposes. Alternatively, I can extract specific settings directly from the file to incorporate them into new Bicep files.

    # Resourcegroup 
    $resourceGroup = 'myresourcegroup'
    
    # Path to ARM Template
    $armTemplatePath = "./armexport.json"
    
    # Path to bicep file
    $bicepOutputPath = "./main.bicep"
    
    az group export --name $resourceGroup --output json > $armTemplatePath
    
    # ARM Template in Bicep dekompilieren
    az bicep decompile --file $armTemplatePath --force > $bicepOutputPath
    
    az deployment group create --resource-group $resourceGroup --template-file $bicepOutputPath --what-if

    If you haven’t installed the Azure CLI (az cli) before, you can do this quite simple with the following command.

    Install-Module -Name Az -AllowClobber -Force -Scope CurrentUser
    Install-AzCLI

    This complex code sample is also on GitHub: <https://github.com/oliverscheer/copy-resource-group>

    Happy environment cloning.

  • Calling an Azure Functions from the command line

    Calling an Azure Functions from the command line

    The Problem

    During a recent project I developed a solution that contains Azure Functions, that are deployed through a Azure DevOps Pipeline right after the infrastructure is created with bicep. For the final test in an Azure DevOps Pipeline/GitHub Actions, I aim to execute one of the newly installed Azure Functions to validate the installed/updated solution. If it runs successful, the deployment is validated and the pipeline can continue to run. Because all names are dynamically created through bicep, the path/name of the functions and all keys are also dynamic and randomized, and only available as bicep outputs.

    The Solution

    To obtain the URL and key of the Azure Functions Methods for testing purposes, I need to utilize the Azure CLI. The process involves retrieving the URL and key of the specific function to be called.

    The following PowerShell script streamlines this task:

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [Alias('g')]
        [string]$resourceGroup,
    
        [Parameter(Mandatory=$true)]
        [Alias('fan')]
        [string]$functionAppName,
    
        [Parameter(Mandatory=$true)]
        [Alias('fn')]
        [string]$functionName
    )
    
    Write-Host "Test Solution"
    Write-Host "- resourceGroup         : $resourceGroup"
    Write-Host "- functionAppName       : $functionAppName"
    Write-Host "- functionName          : $functionName"
    
    # Get Url
    $jsonResponse=az functionapp function show `
        --name $functionAppName `
        --resource-group $resourceGroup `
        --function-name $functionName
    $decodedObject = $jsonResponse | ConvertFrom-Json
    $url = $decodedObject.invokeUrlTemplate
    
    # Get Key
    $jsonResponse=az functionapp function keys list `
        --name $functionAppName `
        --resource-group $resourceGroup `
        --function-name $functionName
    $decodedObject = $jsonResponse | ConvertFrom-Json
    $key = $decodedObject.default
    
    # Invoke
    $invokeUrl=$url+"?code="+$key
    
    $response = Invoke-RestMethod -Uri $invokeUrl -Method Post
    Write-Host $response
    Write-Host $response.StatusCode
    
    return $response

    Invoke this function in PowerShell using:

    .\Test-Azure-Function.ps1 `
        -resourceGroup <myresourcegroup> `
        -functionAppName <myfunctionapp> `
        -functionName <myfunction>

    `This script enables me to check the solution by calling the an Azure Function that runs some internal checks. Hope that helps you.

  • Fixing the Missing Azure Functions Runtime Error

    Fixing the Missing Azure Functions Runtime Error

    During a current project I came across this error because my Azure Functions are planned to run on .net 8. There is by default the runtime for .net 7 available, but this leads to the following error, which blocked me a moment.

    „There is no Functions runtime available that matches the version in the project.“

    This is a handicap if you want to test Azure Functions locally.



    A small investigation later, I found a solution which is simple but hidden in the depth of Visual Studio. The fix for the problem is hidden in Visual Studio options dialog.


    Just click Download & Install and hit F5. And enjoy the beauty of Azure Functions running locally.