Chia sẻ qua


pac power-fx

(Xem trước) Các lệnh để làm việc với Power Fx

Sử dụng các lệnh này để tương tác với dữ liệu trong Dataverse bằng cách sử dụng dòng lệnh hoặc script. Giảm thời gian và độ phức tạp của các thao tác Dataverse bằng cách sử dụng Power Fx.

Bạn có thể sử dụng các công thức được liệt kê trong Tham chiếu công thức - Power Platform CLI.

Lệnh

Lệnh Mô tả
pac power-fx repl (Xem trước) Khởi chạy Vòng lặp Đọc Power FxEval-Print tương tác
pac power-fx run (Xem trước) Chạy một tệp hướng dẫn Power Fx

pac power-fx repl

(Xem trước) Khởi chạy Vòng lặp Đọc Power FxEval-Print tương tác

Lưu ý

Nếu bạn đã cài đặt POWER Platform CLI bằng Cách sử dụng Windows MSI, pac power-fx repl đã xảy ra sự cố đã biết trong đó shell có thể ngừng phản hồi khi sử dụng lệnh này.

Tham số Tùy chọn cho power-fx repl

--environment -env

Xác định Dataverse đích. Giá trị có thể là GUID hoặc URL https tuyệt đối. Khi không được chỉ định, tổ chức hiện hoạt được chọn cho hồ sơ xác thực hiện tại sẽ được sử dụng.

Ghi chú

Lệnh này cung cấp một shell để dùng các công thức được liệt kê trong Tham chiếu công thức - Power Platform CLI.

Bắt đầu repl Power Fx bằng cách chạy lệnh sau:

pac power-fx repl

Sau đó, bạn có thể chạy các lệnh Power Fx.

Ví dụ

Các ví dụ sau đây cho thấy cách sử dụng pac power-fx repl lệnh.

Thêm hàng
Collect(Contacts, { firstname: "Patti", lastname: "Fernandez" })

Repl Power Fx phản hồi với những điều sau:

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

Chạy lệnh sau đây để nhận trợ giúp về tất cả các lệnh sẵn dùng trong Power Fx repl:

Help()

Repl Power Fx phản hồi với đầu ra sau đây:

  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
>>

Lưu ý

Các lệnh này sẽ thay đổi theo thời gian để bao gồm nhiều lệnh hơn. Nó nên được đồng bộ hóa với các lệnh hiện có sẵn tài liệu.

Exit

Dùng Exit() để thoát khỏi repl lệnh.

pac power-fx run

(Xem trước) Chạy một tệp hướng dẫn Power Fx

Ví dụ

Các ví dụ sau đây cho thấy cách sử dụng pac power-fx run lệnh.

Lưu ý

Trong mỗi ví dụ này, bạn cần:

  1. Tạo một tệp có tên test-pfx.txt với các biểu thức Power Fx trong một thư mục trên máy tính của bạn, chẳng hạn như c:\test.

  2. Mở cửa sổ Windows PowerShell, dẫn testrun hướng đến thư mục và sử dụng lệnh xác định tệp có chứa các lệnh: pac power-fx run --file test-pfx.txt.

Để sử dụng bảng Dataverse, bạn phải tạo và chọn một hồ sơ xác thực. Xem pac tạo xác thực vàpac chọn xác thực.

Nếu bạn gặp lỗi, hãy kiểm tra Khắc phục sự cố.

Tạo bản ghi Dataverse

  1. Thêm văn bản sau đây test-pfx.txt vào và lưu tệp.

    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. Chạy lệnh này:

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

    Bạn có thể mong đợi đầu ra như sau:

    {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", ...}
    

Truy vấn bảng Dataverse

  1. Thêm văn bản này test-pfx.txt vào và lưu tệp.

    FirstN(Contacts, 5)
    

    Lệnh này truy xuất năm liên hệ đầu tiên từ Dataverse.

  2. Chạy lệnh này:

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

    Bạn có thể mong đợi đầu ra như sau:

    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>
    

Sử dụng các hàm PowerFx

  1. Thêm văn bản sau đây test-pfx.txt vào và lưu tệp.

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

    Ví dụ này sử dụng các hàm Mid, Ifvà Set Power Fx.

  2. Chạy lệnh sau:

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

    Bạn có thể mong đợi đầu ra như sau:

    PS C:\test> pac power-fx run --file test-pfx.txt
    x: 1
    true
    Result: "X"
    PS C:\test>
    
  3. Thêm tham số --echo và bạn có thể mong đợi kết quả này:

    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>
    

Tham số Bắt buộc cho power-fx run

--file -f

Đường dẫn của tệp để thực thi

Tham số Tùy chọn cho power-fx run

--echo -e

Đúng nếu lệnh nhập sẽ lặp lại về đầu ra

Tham số này không yêu cầu giá trị. Đó là công tắc.

--environment -env

Xác định Dataverse đích. Giá trị có thể là GUID hoặc URL https tuyệt đối. Khi không được chỉ định, tổ chức hiện hoạt được chọn cho hồ sơ xác thực hiện tại sẽ được sử dụng.

Khắc phục sự cố

Sau đây là các bước khắc phục các lỗi bạn có thể gặp phải khi sử pac power-fx dụng lệnh.

Lỗi khi không tồn tại cấu hình xác thực Dataverse

Bạn có thể gặp lỗi như thế này khi không có hồ sơ xác thực chung được liên kết với môi trường 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.

Để giải quyết các lỗi như thế này, hãy tạo hồ sơ xác thực phổ dụng liên kết với môi trường Dataverse.

  1. Chạy danh sách xácpac thực lệnh.

    Bạn có thể thấy kết quả như sau trong đó UNIVERSAL bao gồm hồ sơ xác thực không có Url cho biết url được liên kết với môi trường Dataverse.

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

    Điều này là không đủ vì không có môi trường Dataverse nào được xác định.

  2. Sử dụngpac xác thực tạo để tạo hồ sơ UNIVERSAL xác thực được kết nối với môi trường Dataverse.

    Lưu ý

    Sử dụng tham --environment số sử dụng giá trị environmentid. Tìm ID môi trường của bạn.

    Không sử dụng tham số vì --url thao tác này sẽ tạo hồ sơ DATAVERSE chứ không phải hồ UNIVERSAL sơ.

    Ví dụ:

    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. Chạy pac lại danh sách xác thực. Bạn sẽ thấy kết quả với hồ sơ UNIVERSAL kết nối với URL cho môi trường Dataverse của bạn.

    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. Chọn môi trường UNIVERSAL để đặt nó làm cấu hình mặc định bằng cách sửpac dụng lệnh chọn xác thực.

    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>
    

Xem thêm

Microsoft Power Platform CLI Command Groups
Tổng quan về MICROSOFT Power Platform CLI