The Power Platform CLI is a great tool to use when you are working with the Power Platform. You can create Power Apps Component Framework (PCF) components with it, you can do tons of administrative actions, and much more! You can run these one by one in the console when you’re developing a Power Platform solution, but sometimes, you want to use multiple commands together. In this blog, I will show you how you can create scripts with Power Platform CLI.
Imagine the following: I am an administrator who wants to create environments for developers. I will enable the use of PCF Controls in Canvas Apps, which is an environment setting. Of course, I also want to have good control over the developer environments, so I will also create a group in the environment for the administrators and assign system administrator rights to that group. On top of that, I also want to import solutions so that developers can have a flying start!
That means I will have to use the following Power Platform CLI commands:
- pac admin create (to create the environment)
- pac env select (to switch to the environment that got created before)
- pac env update-settings (to enable the PCF setting)
- pac admin assign-group (to add the group to the environment)
- pac solution import (to import solutions)
Basics
You can follow along with this blog, and I encourage you to do so. To follow along in the right way, make sure to have the following tools installed:
After installing these tools, make sure to connect to a tenant where you have an account with Power Platform Administrator rights. If you need a refresher on how to connect, read this blog. Also – make sure to open a folder in Visual Studio Code where you can add the script file. Create a file in that folder called CLI-script.ps1
Create the environment
Let’s start by the creation of the environment. This can be done by running the pac admin create
command. I always use the help command to learn more about the command. If your Visual Studio Code Terminal isn’t open yet, use Ctrl + ` to open it. Next, run the following in the Visual Studio Code terminal to learn more about the command:
pac admin create help
You will see all the parameters of the command here. Some parameters in here don’t have to be set if you want to use the defaults. Examples of that are region (default: unitedstates), currency (default: USD) and language (default: English). In this case, I’ll keep it simple and use only a couple of parameters:
pac admin create --name "Developer Environment" --type "Developer" --user "user@lowcoderevolution.onmicrosoft.com"
This command will create the environment and after it’s done, I will see the details of the environment. While this is nice, we of course want to be able to run other commands after this with those details. How do I do that?
Use the JSON parameter
Some commands have the option for the --json
parameter. This is only available in the documentation and not via the help commands. When I visit the documentation of the pac admin create
command, I can see there is a --json
parameter available. The description is: Returns the output of the command as a JSON formatted string.
This is exactly what you want, because then you can turn it into a PowerShell object.
The following commands support the --json
parameter:
pac admin copy
pac admin create
pac admin list
pac admin reset
pac admin restore
pac catalog list
pac connector list
pac env who
pac solution list
Convert from JSON
In PowerShell, you have the option to convert from JSON by using the ConvertFrom-Json command. If we combine that with the command above, and add the --json
parameter to it, it would love something like this:
pac admin create --name "Developer Environment" --type "Developer" --user "user@lowcoderevolution.onmicrosoft.com" --json | ConvertFrom-Json
To make it easier for ourselves, lets also add this to a variable:
$devEnvironment = pac admin create --name "Developer Environment" --type "Developer" --user "user@lowcoderevolution.onmicrosoft.com" --json | ConvertFrom-Json
Add this line to the script file (CLI-script.ps1
) you created earlier. Run the command after adding it to your script file. After running the above command, the $devEnvironment
variable will contain the output of the pac admin create
command as a PowerShell object. Values like environment ID and environment URL will be available for you. Run $devEnvironment
after the pac admin create
command is done to see what’s in there.
Select the newly created developer environment
The next step is to switch to the newly created environment. Because you used the $devEnvironment
variable, you can now use the environment ID. To switch environments, you need to use the pac env select
command:
pac env select --environment $devEnvironment.EnvironmentId
Add this to your script file, so that the script contains both the steps to create an environment and to switch to the new environment. Run the pac env select
command you just added to the script. This will select the new environment you created.
Update the setting to use PCF inside of canvas apps
Now you have selected the new environment, the next steps will be easier, since you don’t need to add the --environment
parameter to every command from now on.
Run the following command in the Visual Studio Code terminal to see all the settings in the environment:
pac env list-settings
This will show you a large list of settings. In this example you only need one of them, but I wanted to make sure you knew about this command and all the different options you have here. You can also use the --filter
parameter to filter the list. Let’s try that out by running the following command:
pac env list-settings --filter iscustomcontrolsincanvasappsenabled
The command will filter the settings to only one setting, the iscustomcontrolsincanvasappsenabled
setting. This setting shows if PCF in Canvas Apps is enabled or not. In your case, the value should be No
because this is the default. To change this, run the following command:
pac env update-settings --name "iscustomcontrolsincanvasappsenabled" --value true
Note that the value is true
and not Yes
. This has to do with the pac env list-settings
command, which lists the values in Yes/No
instead of true/false
. When updating the settings always use true
or false
in case of a Boolean. Add the above command for updating the setting to the script file. If you want to be extra sure, you could run the pac env list-settings
command again to see if the value changed from No
to Yes
.
Add a security group to the environment for the administrators
The next step is to create a Dataverse group for the administrators. I have created a security group in Microsoft Entra ID called Contoso Administrators
. Adding this group to the Dataverse environment is pretty easy. Run pac admin assign-group help
in the Visual Studio Code terminal to see which parameters are available. You don’t need all of them! In this case, you can limit it to the following command:
pac admin assign-group --group-name "Contoso Administrators" --group "4627d989-e3c3-45f9-9977-814cad8fe9a0" --team-type "AadSecurityGroup" --membership-type "Members" --role "System Administrator"
Add this to the script file and make sure to replace the value for --group-name
and --group
to use your own values. Also, feel free to change the role if needed.
Import a solution
Now you have enabled the PCF controls in Canvas Apps, you are ready to import a solution. Changing the setting is a prerequisite of the solution you’re going to import. The Creator Kit has a bunch of components available that I want to make available to the developer when they get the developer environment. To make that solution available, you first need to download it from the website. When you have downloaded the solution, add it to the same folder as your script file.
To import a solution to an environment, you can use the pac solution import
command. If you want to learn more about the parameters for that command, run pac solution import help
. In this case, you only need to use the --path
parameter, but in other scenarios, you might need other parameters as well. Add the following command to the script file:
pac solution import --path .\CreatorKitCore_1.0.20240529.1_managed.zip
Note that the value of the path can be different in your case since the version of the Creator Kit can change between the moment of writing this blog and when you try it out!
Wrap up
Now you should have a script available that does a bunch of things. Of course, you can build in a bunch more like error handling, try/catch, turn it into a function with more parameters, and more. I hope this gives you an idea on how you can work with Power Platform CLI in scripting scenarios!
The full script is available below:
$devEnvironment = pac admin create --name "Developer Environment" --type "Developer" --user "user@lowcoderevolution.onmicrosoft.com" --json | ConvertFrom-Json
pac env select --environment $devEnvironment.EnvironmentId
pac env update-settings --name "iscustomcontrolsincanvasappsenabled" --value true
pac admin assign-group --group-name "Contoso Administrators" --group "4627d989-e3c3-45f9-9977-814cad8fe9a0" --team-type "AadSecurityGroup" --membership-type "Members" --role "System Administrator"
pac solution import --path .\CreatorKitCore_1.0.20240529.1_managed.zip
0 comments