教學課程:建立、更新和列出環境管理設定

本教程演示如何使用 Power Platform API (預覽版) 創建、更新和列出環境管理設置。

在本教學課程中,您將了解如何:

  1. 使用 Power Platform API 進行驗證。
  2. 建立一個新的設定值。
  3. 列出所有環境管理設定值。
  4. 更新設定值。

作為此案例的範例,客戶可能想要開啟 儲存體共用存取簽章 (SAS) IP 限制 和 SAS 呼叫的記錄。

步驟 1: 使用 Power Platform API 驗證

在開始之前,請為 Power Platform API 建立一個應用程式註冊,該註冊具有委派的 EnvironmentManagement.Settings.Read 與 EnvironmentManagement.Settings.ReadWrite 權限,並授予管理員同意。 如需更多資訊,請參閱 「驗證 與 權限參考」。

使用下列 PowerShell 指令碼透過 Power Platform API 進行驗證。 請以應用程式註冊時的應用程式(用戶端)ID 以及<tenant-id>目錄(租戶)ID 取代<client-id>。

Import-Module "MSAL.PS"
$AuthResult = Get-MsalToken -ClientId '<client-id>' -TenantId '<tenant-id>' -Scopes 'https://api.powerplatform.com/.default'
$Headers = @{Authorization = "Bearer $($AuthResult.AccessToken)"}

步驟 2: 創建新的設置值

使用下列 PowerShell 腳本來建立儲存體共用存取簽章 (SAS) IP 限制的新設定值,以及相關的稽核記錄功能。 這兩個設定已關閉,但我們稍後會更新它們以開啟它們。

#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=2024-10-01" -Headers $Headers -Body $body -ContentType "application/json"

    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
}

有關 API Power Platform 參考的更多資訊,請參閱 環境管理設置 - 創建環境管理設置。

步驟 3: 列出環境的所有管理設置

使用以下 PowerShell 腳本列出以前為此環境創建的所有設置。 腳本會以 backtick (`) 跳脫$select查詢參數,以免 PowerShell 將其視為變數。

#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=2024-10-01&`$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
}

有關 API 參考的更多資訊 Power Platform ,請參閱 環境管理設置 - 環境管理設置清單。

步驟 4. 更新設置值

使用以下 PowerShell 腳本更新以前定義的設置值。 在此步驟中,您將啟用存儲共用訪問簽名 (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=2024-10-01" -Headers $Headers -Body $body -ContentType "application/json"

    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
}

有關 API 參考的更多資訊 Power Platform ,請參閱 環境管理設置 - 更新環境管理設置。

環境管理設定