pac power-fx

(预览版)用于使用 Power Fx 的命令

使用这些命令通过命令行或脚本与 Dataverse 中的数据交互。 使用 Power Fx 减少 Dataverse作的时间和复杂性。

可以使用 公式参考 - Power Platform CLI 中列出的公式。

指令

Command Description
pac power-fx repl (预览版)启动交互式 Power Fx 读取 -Eval-Print 循环
pac power-fx 运行 (预览版)运行 Power Fx 指令的文件

pac power-fx repl

(预览版)启动交互式 Power Fx 读取 -Eval-Print 循环

注释

如果使用 Windows MSI 安装了 Power Platform CLI,则 shell 在使用此命令时可能会停止响应的 已知问题 pac power-fx repl

power-fx repl 的可选参数

--environment -env

指定目标 Dataverse。 该值可以是 Guid 或绝对 https URL。 如果未指定,将使用为当前身份验证配置文件选择的活动组织。

注解

此命令提供 shell,以使用 公式引用中列出的公式 - Power Platform CLI

运行以下命令启动 Power Fx repl:

pac power-fx repl

之后,可以运行 Power Fx 命令。

例子

以下示例演示了命令的使用 pac power-fx repl

添加行
Collect(Contacts, { firstname: "Patti", lastname: "Fernandez" })

Power Fx repl 响应以下内容:

{contactid:GUID("118d7b47-8f80-ee11-8179-0022482a40c7"), fullname:"Patti Fernandez", ...}
Help

运行以下命令以获取有关 Power Fx repl 中提供的所有命令的帮助:

Help()

Power Fx repl 使用以下输出进行响应:

  Abs             Acos            Acot            AddColumns      And
  Asin            AsType          Atan            Atan2           Average
  Blank           Boolean         Char            Clear           ClearCollect
  Coalesce        Collect         ColorFade       ColorValue      Concat
  Concatenate     Cos             Cot             Count           CountA
  CountIf         CountRows       Date            DateAdd         DateDiff
  DateTime        DateTimeValue   DateValue       Day             Dec2Hex
  Decimal         Degrees         Distinct        DropColumns     EDate
  EncodeUrl       EndsWith        EOMonth         Error           Exp
  Filter          Find            First           FirstN          Float
  ForAll          GUID            Help            Hex2Dec         Hour
  If              IfError         Index           Int             IsBlank
  IsBlankOrError  IsEmpty         IsError         IsMatch         IsNumeric
  IsToday         Language        Last            LastN           Left
  Len             Ln              Log             LookUp          Lower
  Match           MatchAll        Max             Mid             Min
  Minute          Mod             Month           Not             Notify
  Now             OptionSetInfo   Or              ParseJSON       Patch
  Pi              PlainText       Power           Proper          Radians
  Rand            RandBetween     Refresh         Remove          Replace
  RGBA            Right           Round           RoundDown       RoundUp
  Second          Sequence        Set             Shuffle         Sin
  Sort            Split           Sqrt            StartsWith      StdevP
  Substitute      Sum             Switch          Table           Tan
  Text            Time            TimeValue       TimeZoneOffset  Today
  Trim            TrimEnds        Trunc           Upper           Value
  VarP            Weekday         With            Year
>>

注释

这些命令将随着时间的推移而更改,以包含更多命令。 它应与记录 的当前可用命令同步。

Exit

用于 Exit() 退出 repl 命令。

pac power-fx 运行

(预览版)运行 Power Fx 指令的文件

例子

以下示例演示了命令的使用 pac power-fx run

注释

在上述每个示例中,需要:

  1. 在计算机上的文件夹中创建一 test-pfx.txt 个名为 Power Fx 表达式的文件,例如 c:\test

  2. 打开 Windows PowerShell 窗口,导航到test文件夹并使用run指定包含命令的文件的命令: pac power-fx run --file test-pfx.txt

若要使用 Dataverse 表,必须创建并选择身份验证配置文件。 请参阅 pac 身份验证创建pac 身份验证选择

如果收到错误检查 故障排除

创建 Dataverse 记录

  1. 将以下文本添加到 test-pfx.txt 并保存文件。

    Collect(Contacts, { firstname: "Megan", lastname: "Bowen" })
    Collect(Contacts, { firstname: "Garth", lastname: "Forth" })
    Collect(Contacts, { firstname: "Adele", lastname: "Vance" })
    Collect(Contacts, { firstname: "Patti", lastname: "Fernandez" })
    
  2. 运行以下命令:

    PS C:\test> pac power-fx run --file test-pfx.txt
    

    可以期待如下所示的输出:

    {contactid:GUID("80eb0c02-9180-ee11-8179-0022482a40c7"), fullname:"Megan Bowen", ...}
    {contactid:GUID("81eb0c02-9180-ee11-8179-0022482a40c7"), fullname:"Garth Forth", ...}
    {contactid:GUID("82eb0c02-9180-ee11-8179-0022482a40c7"), fullname:"Adele Vance", ...}
    {contactid:GUID("83eb0c02-9180-ee11-8179-0022482a40c7"), fullname:"Patti Fernandez", ...}
    

查询 Dataverse 表

  1. 将此文本添加到 test-pfx.txt 文件并保存。

    FirstN(Contacts, 5)
    

    此命令从 Dataverse 检索前五个联系人。

  2. 运行以下命令:

    PS C:\test> pac power-fx run --file test-pfx.txt
    

    可以期待如下所示的输出:

    Connected to... Your Organization
    Connected as you@yourorg.onmicrosoft.com
    
    contactid                                      fullname           ...
    ============================================== ================== =====
    GUID("eeb27495-d127-ed11-9db1-00224804f8e2")   Yvonne McKay (sample)
    GUID("cba0ed9e-d127-ed11-9db1-00224804f8e2")   Susanna Stubberod (sample)
    GUID("cea0ed9e-d127-ed11-9db1-00224804f8e2")   Nancy Anderson (sample)
    GUID("6cc7483c-7a77-ee11-8179-00224809f375")   Maria Campbell (sample)
    GUID("49b0be2e-d01c-ed11-b83e-000d3a572421")   Sidney Higa (sample)
    PS C:\test>
    

使用 PowerFx 函数

  1. 将以下文本添加到 test-pfx.txt 并保存文件。

    Set(x, 1)
    Result = If( Mid( "asdf",x,1 ) = "a", "X", "Y" )
    

    此示例使用 MidIfSet Power Fx 函数。

  2. 运行下面的命令:

    PS C:\test> pac power-fx run --file test-pfx.txt
    

    可以期待如下所示的输出:

    PS C:\test> pac power-fx run --file test-pfx.txt
    x: 1
    true
    Result: "X"
    PS C:\test>
    
  3. --echo添加参数,可以预期此输出:

    PS C:\test> pac power-fx run --file test-pfx.txt --echo
    Set(x, 1)
    
    x: 1
    true
    Result = If( Mid( "asdf",x,1 ) = "a", "X", "Y" )
    
    Result: "X"
    PS C:\test>
    

power-fx 运行所需的参数

--file -f

要执行的文件的路径

power-fx 运行的可选参数

--echo -e

如此 如果输入命令应回显回输出

此参数不需要任何值。 这是一个开关。

--environment -env

指定目标 Dataverse。 该值可以是 Guid 或绝对 https URL。 如果未指定,将使用为当前身份验证配置文件选择的活动组织。

Troubleshooting

以下是排查使用 pac power-fx 命令时可能会遇到的错误的步骤。

不存在 Dataverse 身份验证配置文件时出错

如果没有与 Dataverse 环境关联的通用身份验证配置文件,可能会收到如下所示的错误。

PS C:\test> pac power-fx run --file test-pfx.txt
Error 8-16: Name isn't valid. 'Contacts' isn't recognized.
Error 18-59: The specified column 'firstname' does not exist.
Error 0-60: The function 'Collect' has some invalid arguments.
Error 8-16: Name isn't valid. 'Contacts' isn't recognized.
Error 18-59: The specified column 'firstname' does not exist.
Error 0-60: The function 'Collect' has some invalid arguments.
Error 8-16: Name isn't valid. 'Contacts' isn't recognized.
Error 18-59: The specified column 'firstname' does not exist.
Error 0-60: The function 'Collect' has some invalid arguments.
Error 8-16: Name isn't valid. 'Contacts' isn't recognized.
Error 18-63: The specified column 'firstname' does not exist.
Error 0-64: The function 'Collect' has some invalid arguments.

若要解决此类错误,请创建与 Dataverse 环境关联的通用身份验证配置文件。

  1. 运行命令 pac 身份验证列表

    你可能会看到如下所示的结果,其中包含一个 UNIVERSAL 身份验证配置文件,其中没有指示它与 Dataverse 环境关联的 URL。

    Index Active Kind      Name Friendly Name Url User                Cloud  Type
    [1]   *      UNIVERSAL                        you@yourcompany.com Public OperatingSystem
    

    这是不够的,因为未指定 Dataverse 环境。

  2. 使用 pac auth create 创建 UNIVERSAL 连接到 Dataverse 环境的身份验证配置文件。

    注释

    --environment使用 environmentid 值的参数。 查找环境 ID

    请勿使用参数, --url 因为这将创建配置文件 DATAVERSE 而不是 UNIVERSAL 配置文件。

    例如:

    PS C:\test> pac auth create --environment f2cef599-b112-4803-bb5d-090077017685 --name ConnectionName
    'you@yourcompany.onmicrosoft.com' authenticated successfully.
    Validating connection...
    Default organization: YourOrganization
    Connected to... YourOrganization
    Connected as you@yourcompany.onmicrosoft.com
    Authentication profile created
       * UNIVERSAL ConnectionName                 https://yourcompany.crm.dynamics.com/          : you@yourcompany.onmicrosoft.com              Public
    
    PS C:\test>
    
  3. 再次运行 pac 身份验证列表 。 应会看到具有 UNIVERSAL Dataverse 环境的 URL 的连接配置文件的结果。

    PS C:\test> pac auth list
    Index Active Kind      Name           Friendly Name        Url                                   User                  Cloud  Type
    [1]   *      UNIVERSAL                                                                           you@yourcompany.com   Public OperatingSystem
    [2]   *      UNIVERSAL ConnectionName YourOrganization     https://yourcompany.crm.dynamics.com/ you@yourcompany.com   Public User
    
    PS C:\test>
    
  4. 选择 UNIVERSAL 环境以使用 pac auth select 命令使其成为默认配置文件。

    PS C:\test> pac auth select --index 2
    New default profile:
       * UNIVERSAL ConnectionName                 https://yourcompany.crm.dynamics.com/          : you@yourcompany.com              Public
    
    PS C:\test>
    

另请参阅

Microsoft Power Platform CLI 命令组
Microsoft Power Platform CLI 概述