บทช่วยสอน: สร้างรายงานความจุรายวัน

API ของ Power Platform สามารถใช้เพื่อดึงรายละเอียดต่างๆ และข้อมูลเมตาจากสภาพแวดล้อม Microsoft Power Platform ของคุณที่มีหรือไม่มีฐานข้อมูล Microsoft Dataverse API ถูกใช้ภายในโดยไคลเอ็นต์ต่างๆ ที่มีอยู่ในปัจจุบัน เช่น PowerShell

ในบทช่วยสอนนี้ คุณจะเรียนรู้วิธีการ:

  • สร้าง Power Automate หรือเวิร์กโฟลว์ Logic Apps (Azure) หรือสคริปต์ PowerShell ที่รับรองความถูกต้องด้วย Power Platform API
  • เรียกจุดสิ้นสุดสภาพแวดล้อมรายการเพื่อดึงรายละเอียดสภาพแวดล้อม Microsoft Power Platform ของคุณ
  • วนซ้ำผ่านออบเจ็กต์ความจุเพื่อดึงการบริโภคจริง
  • บันทึกข้อมูลการใช้ลงในตารางเพื่อแสดง

จากตัวอย่างของสถานการณ์นี้ ลูกค้ากำลังมองหาวิธีจัดการกับการใช้ความจุ เพื่อให้พวกเขาเข้าใจการจัดสรรความจุของผู้เช่าทั้งหมดตามแผนกได้ดีขึ้น ซึ่งจะช่วยให้ลูกค้าสามารถดำเนินการบัญชีต้นทุนภายในและเรียกเก็บเงินคืนตามปริมาณที่แต่ละแผนกใช้กำลังการผลิตที่มีอยู่ทั้งหมด ลูกค้ารายนี้กำลังใช้คำอธิบายของสภาพแวดล้อมเพื่อเรียกแผนกที่เป็นเจ้าของสภาพแวดล้อมแต่ละแห่ง

เชื่อมต่อและตั้งค่าตัวแปร

ใช้รายละเอียดต่อไปนี้ในการเชื่อมต่อกับโปรแกรม Power Platform คุณสามารถเลือกได้ระหว่างประสบการณ์ Azure หรือสคริปต์ PowerShell

สร้างเวิร์กโฟลว์และตั้งค่าตัวแปร

ในการเริ่มต้น ในบทช่วยสอนนี้ เราจะใช้เวิร์กโฟลว์ Logic Apps โฟลว์ Power Automate ยังเป็นที่ยอมรับ และกลไกกระบวนการปฏิบัติการอื่นๆ ที่บริษัทของคุณต้องการใช้สำหรับการทำงานอัตโนมัติ การเรียกทั้งหมดเพื่อดึงข้อมูลใช้ RESTful API ดังนั้นเครื่องมือใดๆ ที่รองรับ REST สามารถทำงานร่วมกับบทช่วยสอนนี้ได้

เยี่ยมชมพอร์ทัล Azure และจากนั้น สร้าง Logic App ใหม่และตั้งชื่อ:

สร้าง Logic App

หลังจากเสร็จสิ้นการจัดเตรียม ให้แก้ไขเวิร์กโฟลว์โดยใช้ตัวออกแบบและตั้งค่าทริกเกอร์การเกิดขึ้นประจำเพื่อให้ทำงานทุกวัน:

ตั้งค่าทริกเกอร์การเกิดขึ้นประจำ

ต่อไปเราจะต้องกำหนดค่าเริ่มต้นของตัวแปรห้าตัว:

  • SPN-Id – นี่คือ ClientID หลักบริการของคุณ จะใช้ในภายหลังเพื่อดำเนินการพิสูจน์ตัวตนในบริบทหลักการบริการ ถ้าคุณกำลังใช้บริบทชื่อผู้ใช้/รหัสผ่าน คุณสามารถข้ามตัวแปรนี้
  • DBCapacity – นี่คือตัวแปร Float สำหรับความจุฐานข้อมูลที่ใช้เป็นเมกะไบต์
  • FileCapacity – นี่คือตัวแปร Float สำหรับความจุไฟล์ที่ใช้เป็นเมกะไบต์
  • LogCapacity – นี่คือตัวแปร Float สำหรับความจุบันทึกที่ใช้ไปในหน่วยเมกะไบต์
  • SimplifiedEnvironmentArray-Init – นี่คือตัวแปรอาร์เรย์ที่เราเพิ่มคุณสมบัติสภาพแวดล้อมบางอย่างลงไป สิ่งนี้ช่วยลดความซับซ้อนของผลลัพธ์รายงานตาราง HTML สุดท้ายได้อย่างมาก

สร้างตัวแปรห้าตัว

ขั้นตอนต่อไปคือการตรวจสอบสิทธิ์ด้วย Microsoft Entra และดึงโทเค็นสำหรับการเรียกใช้ Power Platform API หากคุณยังติดตั้ง Microsoft Entra ไม่เสร็จสิ้น ดูที่ การรับรองความถูกต้อง - ดั้งเดิม

ในบทช่วยสอนนี้ เราใช้ key vault เพื่อเก็บค่าความลับของบริการหลัก ด้วยวิธีนี้ ผู้ดูแลระบบ IT สามารถทำให้ค่านี้พร้อมใช้งานอย่างปลอดภัยสำหรับเวิร์กโฟลว์ของคุณ จากนั้นข้อมูลนี้จะถูกเติมไว้ในการเรียก POST เพื่อดึงโทเค็น Microsoft Entra

จากนั้นเราจะแยกวิเคราะห์โทเค็นการตอบสนอง Microsoft Entra ในออบเจ็กต์ที่กำหนดชนิดโดยใช้สคีมา JSON นี้ ในการดำเนินการ 'Parse JSON':

{
    "properties": {
        "access_token": {
            "type": "string"
        },
        "expires_in": {
            "type": "integer"
        },
        "ext_expires_in": {
            "type": "integer"
        },
        "token_type": {
            "type": "string"
        }
    },
    "type": "object"
}

แยกวิเคราะห์โทเค็น การตอบ เป็นอ็อบเจ็กต์ที่มีชนิด Microsoft Entra

เรียกข้อมูลสภาพแวดล้อม

ในส่วนนี้ เราจะดึงรายการสภาพแวดล้อมที่คุณดูแล ซึ่งสามารถทำได้ผ่าน API และ PowerShell

เรียกจุดสิ้นสุดสภาพแวดล้อมรายการ

ตอนนี้เป็นเวลาเรียกใช้ API ของ Power Platform ใช้รายการสภาพแวดล้อม ตำแหน่งข้อมูล เพื่อดึงข้อมูลสภาพแวดล้อมทั้งหมดของเราและข้อมูลเมตา โดยเฉพาะอย่างยิ่งด้วยพารามิเตอร์ $expand สำหรับความจุ นอกจากนี้ ยังใช้ส่วนหัวของข้อมูลรับรองความถูกต้องพร้อมกับโทเค็นแบบแบเรอร์ที่เราได้รับในส่วนก่อนหน้าจาก Microsoft Entra ID หากคุณใช้บริบทชื่อผู้ใช้/รหัสผ่าน คุณสามารถป้อนโทเค็นผู้ถือในขั้นตอนนี้ได้เช่นกัน

ใช้จุดสิ้นสุดแสดงรายการสภาพแวดล้อมเพื่อดึงข้อมูลสภาพแวดล้อมและข้อมูลเมตาทั้งหมด

จากนั้นเราจะวิเคราะห์ Power Platform API การตอบ เป็นอ็อบเจ็กต์ที่มีชนิดโดยใช้รูปแบบ JSON นี้ด้วยการดำเนินการ 'แยกวิเคราะห์ JSON':

{
    "properties": {
        "value": {
            "items": {
                "properties": {
                    "description": {
                        "type": "string"
                    },
                    "id": {
                        "type": "string"
                    },
                    "location": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string"
                    },
                    "properties": {
                        "properties": {
                            "addons": {
                                "type": "array"
                            },
                            "azureRegion": {
                                "type": "string"
                            },
                            "capacity": {
                                "items": {
                                    "properties": {
                                        "actualConsumption": {
                                            "type": "number"
                                        },
                                        "capacityType": {
                                            "type": "string"
                                        },
                                        "capacityUnit": {
                                            "type": "string"
                                        },
                                        "ratedConsumption": {
                                            "type": "number"
                                        },
                                        "updatedOn": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "capacityType",
                                        "actualConsumption",
                                        "ratedConsumption",
                                        "capacityUnit",
                                        "updatedOn"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            },
                            "clientUris": {
                                "properties": {
                                    "admin": {
                                        "type": "string"
                                    },
                                    "maker": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "cluster": {
                                "properties": {
                                    "category": {
                                        "type": "string"
                                    },
                                    "number": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "connectedGroups": {
                                "type": "array"
                            },
                            "createdBy": {
                                "properties": {
                                    "displayName": {
                                        "type": "string"
                                    },
                                    "id": {
                                        "type": "string"
                                    },
                                    "type": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "createdTime": {
                                "type": "string"
                            },
                            "creationType": {
                                "type": "string"
                            },
                            "databaseType": {
                                "type": "string"
                            },
                            "displayName": {
                                "type": "string"
                            },
                            "environmentSku": {
                                "type": "string"
                            },
                            "isDefault": {
                                "type": "boolean"
                            },
                            "linkedEnvironmentMetadata": {
                                "properties": {
                                    "backgroundOperationsState": {
                                        "type": "string"
                                    },
                                    "baseLanguage": {
                                        "type": "number"
                                    },
                                    "createdTime": {
                                        "type": "string"
                                    },
                                    "domainName": {
                                        "type": "string"
                                    },
                                    "friendlyName": {
                                        "type": "string"
                                    },
                                    "instanceApiUrl": {
                                        "type": "string"
                                    },
                                    "instanceState": {
                                        "type": "string"
                                    },
                                    "instanceUrl": {
                                        "type": "string"
                                    },
                                    "platformSku": {
                                        "type": "string"
                                    },
                                    "resourceId": {
                                        "type": "string"
                                    },
                                    "scaleGroup": {
                                        "type": "string"
                                    },
                                    "uniqueName": {
                                        "type": "string"
                                    },
                                    "version": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "protectionStatus": {
                                "properties": {
                                    "keyManagedBy": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "provisioningState": {
                                "type": "string"
                            },
                            "retentionDetails": {
                                "properties": {
                                    "backupsAvailableFromDateTime": {
                                        "type": "string"
                                    },
                                    "retentionPeriod": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "retentionPeriod": {
                                "type": "string"
                            },
                            "runtimeEndpoints": {
                                "properties": {
                                    "microsoft.ApiManagement": {
                                        "type": "string"
                                    },
                                    "microsoft.BusinessAppPlatform": {
                                        "type": "string"
                                    },
                                    "microsoft.CommonDataModel": {
                                        "type": "string"
                                    },
                                    "microsoft.Flow": {
                                        "type": "string"
                                    },
                                    "microsoft.PowerApps": {
                                        "type": "string"
                                    },
                                    "microsoft.PowerAppsAdvisor": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            },
                            "states": {
                                "properties": {
                                    "management": {
                                        "properties": {
                                            "id": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    },
                                    "runtime": {
                                        "properties": {
                                            "id": {
                                                "type": "string"
                                            }
                                        },
                                        "type": "object"
                                    }
                                },
                                "type": "object"
                            },
                            "updateCadence": {
                                "properties": {
                                    "id": {
                                        "type": "string"
                                    }
                                },
                                "type": "object"
                            }
                        },
                        "type": "object"
                    },
                    "type": {
                        "type": "string"
                    }
                },
                "required": [
                    "id",
                    "type",
                    "location",
                    "name",
                    "properties"
                ],
                "type": "object"
            },
            "type": "array"
        }
    },
    "type": "object"
}

แยกวิเคราะห์  Power Platform API การตอบ เป็นอ็อบเจ็กต์ที่มีชนิด

วนซ้ำผ่านออบเจ็กต์ความจุ

นี่เป็นส่วนที่ซับซ้อนที่สุดของบทช่วยสอน ที่นี่เราใช้ลูปภายในลูปเพื่อวนซ้ำแต่ละสภาพแวดล้อมในรายการสภาพแวดล้อม การตอบ และแต่ละสภาพแวดล้อมมีอาร์เรย์ของรายละเอียดความจุที่เราจะวนซ้ำด้วยเช่นกัน วิธีนี้ช่วยให้เรารวบรวมข้อมูลที่จำเป็นสำหรับแถวสภาพแวดล้อมแต่ละแถวในตารางรายงานความจุของเรา

For-each และการแยกวิเคราะห์

ลองทำตามทีละขั้นตอน ขั้นแรก เราใช้การควบคุม For Each โดยใช้ 'ค่า' ของเอาต์พุต Parse-List-Response:

ใช้ตัวควบคุม For Each โดยใช้ค่าของผลลัพธ์ Parse-List-Response

จากนั้นเราจะแยกวิเคราะห์สภาพแวดล้อมเดียวนี้ลงในอ็อบเจ็กต์ที่มีชนิดโดยใช้รูปแบบ JSON นี้:

{
    "properties": {
        "description": {
            "type": "string"
        },
        "id": {
            "type": "string"
        },
        "location": {
            "type": "string"
        },
        "name": {
            "type": "string"
        },
        "properties": {
            "properties": {
                "addons": {
                    "type": "array"
                },
                "azureRegion": {
                    "type": "string"
                },
                "capacity": {
                    "items": {
                        "properties": {
                            "actualConsumption": {
                                "type": "number"
                            },
                            "capacityType": {
                                "type": "string"
                            },
                            "capacityUnit": {
                                "type": "string"
                            },
                            "ratedConsumption": {
                                "type": "number"
                            },
                            "updatedOn": {
                                "type": "string"
                            }
                        },
                        "required": [
                            "capacityType",
                            "actualConsumption",
                            "ratedConsumption",
                            "capacityUnit",
                            "updatedOn"
                        ],
                        "type": "object"
                    },
                    "type": "array"
                },
                "clientUris": {
                    "properties": {
                        "admin": {
                            "type": "string"
                        },
                        "maker": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "cluster": {
                    "properties": {
                        "number": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "connectedGroups": {
                    "type": "array"
                },
                "createdBy": {
                    "properties": {
                        "displayName": {
                            "type": "string"
                        },
                        "id": {
                            "type": "string"
                        },
                        "type": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "createdTime": {
                    "type": "string"
                },
                "creationType": {
                    "type": "string"
                },
                "databaseType": {
                    "type": "string"
                },
                "displayName": {
                    "type": "string"
                },
                "environmentSku": {
                    "type": "string"
                },
                "isDefault": {
                    "type": "boolean"
                },
                "linkedEnvironmentMetadata": {
                    "properties": {
                        "backgroundOperationsState": {
                            "type": "string"
                        },
                        "baseLanguage": {
                            "type": "integer"
                        },
                        "createdTime": {
                            "type": "string"
                        },
                        "domainName": {
                            "type": "string"
                        },
                        "friendlyName": {
                            "type": "string"
                        },
                        "instanceApiUrl": {
                            "type": "string"
                        },
                        "instanceState": {
                            "type": "string"
                        },
                        "instanceUrl": {
                            "type": "string"
                        },
                        "resourceId": {
                            "type": "string"
                        },
                        "scaleGroup": {
                            "type": "string"
                        },
                        "uniqueName": {
                            "type": "string"
                        },
                        "version": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "protectionStatus": {
                    "properties": {
                        "keyManagedBy": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "provisioningState": {
                    "type": "string"
                },
                "retentionDetails": {
                    "properties": {
                        "backupsAvailableFromDateTime": {
                            "type": "string"
                        },
                        "retentionPeriod": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "retentionPeriod": {
                    "type": "string"
                },
                "runtimeEndpoints": {
                    "properties": {
                        "microsoft.ApiManagement": {
                            "type": "string"
                        },
                        "microsoft.BusinessAppPlatform": {
                            "type": "string"
                        },
                        "microsoft.CommonDataModel": {
                            "type": "string"
                        },
                        "microsoft.Flow": {
                            "type": "string"
                        },
                        "microsoft.PowerApps": {
                            "type": "string"
                        },
                        "microsoft.PowerAppsAdvisor": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                },
                "states": {
                    "properties": {
                        "management": {
                            "properties": {
                                "id": {
                                    "type": "string"
                                }
                            },
                            "type": "object"
                        },
                        "runtime": {
                            "properties": {
                                "id": {
                                    "type": "string"
                                }
                            },
                            "type": "object"
                        }
                    },
                    "type": "object"
                },
                "updateCadence": {
                    "properties": {
                        "id": {
                            "type": "string"
                        }
                    },
                    "type": "object"
                }
            },
            "type": "object"
        },
        "type": {
            "type": "string"
        }
    },
    "type": "object"
}

ถัดไป เราใช้การควบคุม For Each อื่นโดยใช้ 'ความจุ' ของเอาต์พุต Parse-CurrentItem จากนั้นเราจะแยกวิเคราะห์สิ่งนี้เป็นวัตถุที่มีชนิดโดยใช้รูปแบบ JSON นี้:

ตัวควบคุม For Each โดยใช้ความจุของผลลัพธ์ Parse-CurrentItem

ตอนนี้เราสามารถใช้การควบคุม Switch บนคุณสมบัติ CapacityType จากผลลัพธ์ Parse-Capacity นี่คือค่าของ 'ฐานข้อมูล' 'ไฟล์' หรือ 'บันทึก' ภายใต้กรณีของสวิตช์แต่ละกรณี บันทึกคุณสมบัติ 'actualConsumption' ที่เกี่ยวข้องลงในตัวแปรที่เกี่ยวข้อง ในกรณีต่อไปนี้ คุณจะเห็นว่าเรากำลังจับความจุของฐานข้อมูล:

ใช้การควบคุม Switch บนคุณสมบัติ CapacityType จากผลลัพธ์ Parse-Capacity

ในขั้นตอนสุดท้ายในลูป 'สำหรับแต่ละสภาพแวดล้อม' ตอนนี้เราสามารถบันทึกรายละเอียดสภาพแวดล้อมสำหรับแถวนี้ในรายงานได้ การใช้การควบคุมผนวกเข้ากับตัวแปรอาร์เรย์ ให้ใช้ Schema ของ JSON ต่อไปนี้:

{
    "properties": {
        "actualConsumption": {
            "type": "number"
        },
        "capacityType": {
            "type": "string"
        },
        "capacityUnit": {
            "type": "string"
        },
        "ratedConsumption": {
            "type": "number"
        },
        "updatedOn": {
            "type": "string"
        }
    },
    "type": "object"
}

บันทึกรายละเอียดสภาพแวดล้อม

สร้างตารางรายงาน

ขอแสดงความยินดี ตอนนี้คุณมาถึงส่วนที่ง่ายแล้ว! ตอนนี้เรามีอาร์เรย์ความจุของสภาพแวดล้อมที่มีประชากรครบถ้วนและเรียบง่ายแล้ว เราสามารถแสดงในรูปแบบตารางได้

ใช้ตัวเชื่อมต่อ HTML ตาราง

ส่งค่าไปยังตัวเชื่อมต่อสร้างตาราง HTML

โดยการเรียกใช้ Logic App ตอนนี้เราสามารถเห็นผลลัพธ์ของรายงานตาราง HTML:

ผลลัพธ์ของรายงานตาราง HTML

รายงานอาจถูกส่งทางอีเมลไปยังผู้มีส่วนได้ส่วนเสียในตัวอย่างนี้เพื่อวัตถุประสงค์ในการบัญชีต้นทุน หรืออาจบันทึกข้อมูลลงในฐานข้อมูลเพื่อการวิเคราะห์เพิ่มเติมและแนวโน้มในอดีต