Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Apps - Microsoft Dataverse
Unanswered

Using a Python script to create new Environments via Rest API (PowerShell script worked)

(1) ShareShare
ReportReport
Posted on by 661
I had a crack at this with PowerShell and managed to get the creation to work but looks like most of the PowerShell commands either no longer exist, don't do what I want or are hallucinated into existence by AI attempt to help.

This is the PowerShell that at least creates the environment. I just couldn't get any additional config done after.

I managed to get most of the below converted, but I'm having issues just trying to authenticate, I tried a single user but ran into issues with parsing the token / it not liking something about it or something the user cannot supply.

Managed to get a template working that would auth using an Azure App_Registration, but as soon as I try the actual Environment Creation It fails, usually with invalid token, wrong audience..

Anyway as I write this I just found https://learn.microsoft.com/en-us/power-platform/admin/programmability-authentication-v2 which will probably get me through it.
 
I'll still post as it would be interesting to see if anyone else is using this type of method, or has any other handy scripts.
<# Replace placeholders like <FirstSecurityGroupId> and <SecondSecurityGroupId> 
with the actual security group IDs from Azure AD.
Use the correct EnvironmentName for operations that depend on the created environment.
The PowerShell modules (Microsoft.PowerPlatform.Admin.PowerShell and AzureAD) 
must be installed and imported.
Run the script in an elevated PowerShell session.#>

# Import required modules
Import-Module Microsoft.PowerApps.Administration.PowerShell
Import-Module AzureAD

# Install required modules if not already installed
# Install-Module -Name Microsoft.PowerPlatform.Admin.PowerShell
# Install-Module -Name AzureAD

# Sign in to Power Apps
Add-PowerAppsAccount -Username "Your Admin Account"
Connect-AzureAD

# Variables
$environmentName = "Shortname_PROD"
$environmentType = "Production"  # Set to "Production" or "Sandbox"
$securityGroupId1 = "Your Azure Group GUID"  
$securityGroupId2 = "Your Azure Group2 GUID"
$location = "Country"  # Adjust as needed
$url ="NewEnvironmentName" # Adjust as needed
$description = "This is a test environment made with a PowerShell Script"

if ($environmentType -eq "Production") { 
    $auditEnabled = $true
    $isProduction = $true
} else { 
    $auditEnabled = $false
    $isProduction = $false
}

# Create a new Power Apps environment
New-AdminPowerAppEnvironment -DisplayName $environmentName -Location $location -EnvironmentSku $environmentType -SecurityGroupId $securityGroupId1 -Description $description -Domainname $url -ProvisionDatabase -WaitUntilFinished
Write-Output "Environment $environmentName created successfully."

# Get the environment details
$environment = Get-AdminPowerAppEnvironment ("*" + $environmentName + "*") 

#TO DO 
#error checking above, set as managed environment##



# Works to this point but not any further....

# Create a Team and assign the System Administrator role
New-AdminPowerAppEnvironmentTeam -EnvironmentName $environmentName -DisplayName "$environmentName`_EnvironmentAdmins" -Description "System Administrator role" -MembershipType "Members" -AzureADGroupId $securityGroupId1
Set-AdminPowerAppEnvironmentRole -EnvironmentName $environmentName -TeamDisplayName "$environmentName`_EnvironmentAdmins" -RoleName "System Administrator"
Write-Output "System Administrator Team $environmentName`_EnvironmentAdmins created and assigned roles."

# Create a Team and assign the App Opener role
New-AdminPowerAppEnvironmentTeam -EnvironmentName $environmentName -DisplayName "$environmentName`_Users" -Description "App Opener Role" -MembershipType "Members" -AzureADGroupId $securityGroupId2
Set-AdminPowerAppEnvironmentRole -EnvironmentName $environmentName -TeamDisplayName "$environmentName`_Users" -RoleName "App Opener"
Write-Output "App Opener Team $environmentName`_Users created and assigned roles."

# Enable Auditing for Non-Production environments
if ($auditEnabled -and $environment.EnvironmentType -ne "Production") {
    Set-AdminPowerAppEnvironment -EnvironmentName $environmentName -IsAuditingEnabled $true
    Write-Output "Auditing enabled for the environment $environmentName."
}

# Update Environment Features
Set-AdminPowerAppEnvironment -EnvironmentName $environmentName -DisableAIBuilder $true -DisableAIPrompts $true -BlockUnmanagedCustomizations $true -DisableProcessCapacityOverage $true -DisableAutoClaimOfProcessCapacity $true
Write-Output "Environment security settings updated for $environmentName"
Write-Output "DisableAIBuilder, DisableAIPrompts, BlockUnmanagedCustomizations, DisableProcessCapacityOverage, DisableAutoClaimOfProcessCapacity"

# Enable IP address-based cookie binding for Production environments
if ($isProduction) {
    Set-AdminPowerAppEnvironment -EnvironmentName $environmentName -EnableIPBasedCookieBinding $true
    Write-Output "IP-based cookie binding enabled for production $environmentName."
}


 
Categories:
  • RandomDept Profile Picture
    661 on at
    Using a Python script to create new Environments via Rest API (PowerShell script worked)
    @Michael E. Gernaey

    Stilling banging my head against the wall as I continue working through it with AI
  • Michael E. Gernaey Profile Picture
    40,262 Super User 2025 Season 1 on at
    Using a Python script to create new Environments via Rest API (PowerShell script worked)
    Hi 
     
    Did you ever get this resolved? Or do you still need help?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,522 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,869 Most Valuable Professional

Leaderboard

Featured topics