{"id":1148,"date":"2024-08-15T08:00:48","date_gmt":"2024-08-15T15:00:48","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/powerplatform\/?p=1148"},"modified":"2024-08-15T08:53:24","modified_gmt":"2024-08-15T15:53:24","slug":"create-scripts-with-power-platform-cli","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powerplatform\/create-scripts-with-power-platform-cli\/","title":{"rendered":"Create scripts with Power Platform CLI"},"content":{"rendered":"<p>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&#8217;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.<\/p>\n<p>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!<\/p>\n<p>That means I will have to use the following Power Platform CLI commands:<\/p>\n<ul>\n<li>pac admin create (to create the environment)<\/li>\n<li>pac env select (to switch to the environment that got created before)<\/li>\n<li>pac env update-settings (to enable the PCF setting)<\/li>\n<li>pac admin assign-group (to add the group to the environment)<\/li>\n<li>pac solution import (to import solutions)<\/li>\n<\/ul>\n<h2>Basics<\/h2>\n<p>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:<\/p>\n<ul>\n<li><a href=\"https:\/\/code.visualstudio.com\/Download\">Visual Studio Code<\/a><\/li>\n<li><a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=microsoft-IsvExpTools.PowerPlatformTools\">Power Platform Tools extension<\/a> <\/li>\n<\/ul>\n<p>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 <a href=\"https:\/\/devblogs.microsoft.com\/powerplatform\/getting-started-with-the-power-platform-cli\/\">this blog<\/a>. Also &#8211; make sure to open a folder in Visual Studio Code where you can add the script file. Create a file in that folder called <code>CLI-script.ps1<\/code><\/p>\n<h2>Create the environment<\/h2>\n<p>Let&#8217;s start by the creation of the environment. This can be done by running the <code>pac admin create<\/code> command. I always use the help command to learn more about the command. If your Visual Studio Code Terminal isn&#8217;t open yet, use Ctrl + ` to open it. Next, run the following in the Visual Studio Code terminal to learn more about the command:<\/p>\n<pre><code class=\"powershell\">pac admin create help\n<\/code><\/pre>\n<p>You will see all the parameters of the command here. Some parameters in here don&#8217;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&#8217;ll keep it simple and use only a couple of parameters:<\/p>\n<pre><code class=\"powershell\">pac admin create --name \"Developer Environment\" --type \"Developer\" --user \"user@lowcoderevolution.onmicrosoft.com\"\n<\/code><\/pre>\n<p>This command will create the environment and after it&#8217;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?<\/p>\n<h2>Use the JSON parameter<\/h2>\n<p>Some commands have the option for the <code>--json<\/code> parameter. This is only available in the documentation and not via the help commands. When I visit <a href=\"https:\/\/aka.ms\/pac\/admin\/#pac-admin-create\">the documentation<\/a> of the <code>pac admin create<\/code> command, I can see there is a <code>--json<\/code> parameter available. The description is: Returns the output of the command as a JSON formatted string.<\/p>\n<p>This is exactly what you want, because then you can turn it into a PowerShell object.<\/p>\n<p>The following commands support the <code>--json<\/code> parameter:<\/p>\n<ul>\n<li><code>pac admin copy<\/code><\/li>\n<li><code>pac admin create<\/code><\/li>\n<li><code>pac admin list<\/code><\/li>\n<li><code>pac admin reset<\/code><\/li>\n<li><code>pac admin restore<\/code><\/li>\n<li><code>pac catalog list<\/code><\/li>\n<li><code>pac connector list<\/code><\/li>\n<li><code>pac env who<\/code><\/li>\n<li><code>pac solution list<\/code><\/li>\n<\/ul>\n<h2>Convert from JSON<\/h2>\n<p>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 <code>--json<\/code> parameter to it, it would love something like this:<\/p>\n<pre><code class=\"powershell\">pac admin create --name \"Developer Environment\" --type \"Developer\" --user \"user@lowcoderevolution.onmicrosoft.com\" --json | ConvertFrom-Json\n<\/code><\/pre>\n<p>To make it easier for ourselves, lets also add this to a variable:<\/p>\n<pre><code class=\"powershell\">$devEnvironment = pac admin create --name \"Developer Environment\" --type \"Developer\" --user \"user@lowcoderevolution.onmicrosoft.com\" --json | ConvertFrom-Json\n<\/code><\/pre>\n<p>Add this line to the script file (<code>CLI-script.ps1<\/code>) you created earlier. Run the command after adding it to your script file. After running the above command, the <code>$devEnvironment<\/code> variable will contain the output of the <code>pac admin create<\/code> command as a PowerShell object. Values like environment ID and environment URL will be available for you. Run <code>$devEnvironment<\/code> after the <code>pac admin create<\/code> command is done to see what&#8217;s in there.<\/p>\n<h2>Select the newly created developer environment<\/h2>\n<p>The next step is to switch to the newly created environment. Because you used the <code>$devEnvironment<\/code> variable, you can now use the environment ID. To switch environments, you need to use the <code>pac env select<\/code> command:<\/p>\n<pre><code class=\"powershell\">pac env select --environment $devEnvironment.EnvironmentId\n<\/code><\/pre>\n<p>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 <code>pac env select<\/code> command you just added to the script. This will select the new environment you created.<\/p>\n<h2>Update the setting to use PCF inside of canvas apps<\/h2>\n<p>Now you have selected the new environment, the next steps will be easier, since you don&#8217;t need to add the <code>--environment<\/code> parameter to every command from now on.<\/p>\n<p>Run the following command in the Visual Studio Code terminal to see all the settings in the environment:<\/p>\n<pre><code class=\"powershell\">pac env list-settings\n<\/code><\/pre>\n<p>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 <code>--filter<\/code> parameter to filter the list. Let&#8217;s try that out by running the following command:<\/p>\n<pre><code class=\"powershell\">pac env list-settings --filter iscustomcontrolsincanvasappsenabled\n<\/code><\/pre>\n<p>The command will filter the settings to only one setting, the <code>iscustomcontrolsincanvasappsenabled<\/code> setting. This setting shows if PCF in Canvas Apps is enabled or not. In your case, the value should be <code>No<\/code> because this is the default. To change this, run the following command:<\/p>\n<pre><code class=\"powershell\">pac env update-settings --name \"iscustomcontrolsincanvasappsenabled\" --value true\n<\/code><\/pre>\n<p>Note that the value is <code>true<\/code> and not <code>Yes<\/code>. This has to do with the <code>pac env list-settings<\/code> command, which lists the values in <code>Yes\/No<\/code> instead of <code>true\/false<\/code>. When updating the settings always use <code>true<\/code> or <code>false<\/code> 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 <code>pac env list-settings<\/code> command again to see if the value changed from <code>No<\/code> to <code>Yes<\/code>.<\/p>\n<h2>Add a security group to the environment for the administrators<\/h2>\n<p>The next step is to create a Dataverse group for the administrators. I have created a security group in Microsoft Entra ID called <code>Contoso Administrators<\/code>. Adding this group to the Dataverse environment is pretty easy. Run <code>pac admin assign-group help<\/code> in the Visual Studio Code terminal to see which parameters are available. You don&#8217;t need all of them! In this case, you can limit it to the following command:<\/p>\n<pre><code class=\"powershell\">pac admin assign-group --group-name \"Contoso Administrators\" --group \"4627d989-e3c3-45f9-9977-814cad8fe9a0\" --team-type \"AadSecurityGroup\" --membership-type \"Members\" --role \"System Administrator\"\n<\/code><\/pre>\n<p>Add this to the script file and make sure to replace the value for <code>--group-name<\/code> and <code>--group<\/code> to use your own values. Also, feel free to change the role if needed.<\/p>\n<h2>Import a solution<\/h2>\n<p>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&#8217;re going to import. The <a href=\"https:\/\/aka.ms\/creatorkit\">Creator Kit<\/a> 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 <a href=\"https:\/\/aka.ms\/creatorkitdownload\">download it from the website<\/a>. When you have downloaded the solution, add it to the same folder as your script file.<\/p>\n<p>To import a solution to an environment, you can use the <code>pac solution import<\/code> command. If you want to learn more about the parameters for that command, run <code>pac solution import help<\/code>. In this case, you only need to use the <code>--path<\/code> parameter, but in other scenarios, you might need other parameters as well. Add the following command to the script file:<\/p>\n<pre><code class=\"powershell\">pac solution import --path .\\CreatorKitCore_1.0.20240529.1_managed.zip\n<\/code><\/pre>\n<p>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!<\/p>\n<h2>Wrap up<\/h2>\n<p>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!<\/p>\n<p>The full script is available below:<\/p>\n<pre><code class=\"powershell\">$devEnvironment = pac admin create --name \"Developer Environment\" --type \"Developer\" --user \"user@lowcoderevolution.onmicrosoft.com\" --json | ConvertFrom-Json\npac env select --environment $devEnvironment.EnvironmentId\npac env update-settings --name \"iscustomcontrolsincanvasappsenabled\" --value true\npac admin assign-group --group-name \"Contoso Administrators\" --group \"4627d989-e3c3-45f9-9977-814cad8fe9a0\" --team-type \"AadSecurityGroup\" --membership-type \"Members\" --role \"System Administrator\"\npac solution import --path .\\CreatorKitCore_1.0.20240529.1_managed.zip\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;re developing a Power Platform [&hellip;]<\/p>\n","protected":false},"author":115431,"featured_media":1167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[11],"class_list":["post-1148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powerplatform","tag-cli"],"acf":[],"blog_post_summary":"<p>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&#8217;re developing a Power Platform [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/posts\/1148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/users\/115431"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/comments?post=1148"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/posts\/1148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/media\/1167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/media?parent=1148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/categories?post=1148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powerplatform\/wp-json\/wp\/v2\/tags?post=1148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}