Tutorial: Create, update, and list Environment Management Settings (preview)

[This article is pre-release documentation and is subject to change.]

This tutorial demonstrates how to use the Power Platform API (preview) to create, update, and list Environment Management Settings.

In this tutorial, learn how to:

  1. Authenticate using Power Platform API.
  2. Create a new setting value.
  3. List all management setting values for the environment.
  4. Update a setting value.

As an example of this scenario, a customer may want to turn on Storage Shared Access Signature (SAS) IP restrictions and logging of SAS calls.

Important

  • This is a preview feature.
  • Preview features aren't meant for production use and may have restricted functionality. These features are available before an official release so that customers can get early access and provide feedback.

Step 1. Authenticate using Power Platform API

Use the following PowerShell script to authenticate using Power Platform API.

Import-Module "MSAL.PS"
$AuthResult = Get-MsalToken -ClientId '00001111-aaaa-2222-bbbb-3333cccc4444' -Scope 'https://api.powerplatform.com/.default'
$Headers = @{Authorization = "Bearer $($AuthResult.AccessToken)"}

Step 2. Create a new setting value

Use the following PowerShell script to create a new setting value for Storage Shared Access Signature (SAS) IP restrictions, and, the related audit logging capability. These two settings are off, however, we'll later update them to turn them on.

#Set your environment ID
$environmentId = "ENV_ID_HERE"

# Please uncomment the values that need to be updated
$EnvironmentManagementSettings = @{
    "EnableIpBasedStorageAccessSignatureRule" = $false
    "LoggingEnabledForIpBasedStorageAccessSignature" = $false
}

$body = $json = $EnvironmentManagementSettings | ConvertTo-Json

try 
{
    # Create the new setting value
    Write-Host "Invoking Create Management Setting for Environment $environmentId with body $body"
    $apiResponse = Invoke-WebRequest -Method Post -Uri "https://api.powerplatform.com/environmentmanagement/environments/$environmentId/settings/?api-version=2022-03-01-preview" -Headers $Headers -Body $body

    Write-Host "Operation Status: $apiResponse.StatusDescription"
} 
catch 
{
    # Dig into the exception to get the Response details.
    Write-Host "Response CorrelationId:" $_.Exception.Response.Headers["x-ms-correlation-id"]
    Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
    Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
    $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();

        Write-Host $responseBody
}

Learn more about the Power Platform API reference in Environment Management Settings - Create Environment Management Settings.

Step 3. List all management settings for the environment

Use the following PowerShell script to list all of the previously created settings for this environment.

#Set your environment ID
$environmentId = "ENV_ID_HERE"

try 
{
    # Create the new setting value
    Write-Host "Invoking List Management Settings for Environment $environmentId"
    $apiResponse = Invoke-WebRequest -Method Get -Uri "https://api.powerplatform.com/environmentmanagement/environments/$environmentId/settings/?api-version=2022-03-01-preview&$select=EnableIpBasedStorageAccessSignatureRule,LoggingEnabledForIpBasedStorageAccessSignature" -Headers $Headers

    Write-Host $apiResponse
} 
catch 
{
    # Dig into the exception to get the Response details.
    Write-Host "Response CorrelationId:" $_.Exception.Response.Headers["x-ms-correlation-id"]
    Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
    Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
    $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();

        Write-Host $responseBody
}

Learn more about the Power Platform API reference in Environment Management Settings - List Environment Management Settings.

Step 4. Update a setting value

Use the following PowerShell script to update a previously defined setting value. In this step, you turn on the logging for Storage Shared Access Signature (SAS).

#Set your environment ID
$environmentId = "ENV_ID_HERE"

# Please uncomment the values that need to be updated
$EnvironmentManagementSettings = @{
    "LoggingEnabledForIpBasedStorageAccessSignature" = $true
}

$body = $json = $EnvironmentManagementSettings | ConvertTo-Json

try 
{
    # Updating the setting value
    Write-Host "Invoking Update Management Setting for Environment $environmentId with body $body"
    $apiResponse = Invoke-WebRequest -Method Patch -Uri "https://api.powerplatform.com/environmentmanagement/environments/$environmentId/settings/?api-version=2022-03-01-preview" -Headers $Headers -Body $body

    Write-Host "Operation Status: $apiResponse.StatusDescription"
} 
catch 
{
    # Dig into the exception to get the Response details.
    Write-Host "Response CorrelationId:" $_.Exception.Response.Headers["x-ms-correlation-id"]
    Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__ 
    Write-Host "StatusDescription:" $_.Exception.Response.StatusDescription
    $result = $_.Exception.Response.GetResponseStream()
        $reader = New-Object System.IO.StreamReader($result)
        $reader.BaseStream.Position = 0
        $reader.DiscardBufferedData()
        $responseBody = $reader.ReadToEnd();

        Write-Host $responseBody
}

Learn more about the Power Platform API reference in Environment Management Settings - Update Environment Management Settings.

Environment Management Settings