简体   繁体   English

带有 Powershell 的 Power BI 推送数据集; 如何在单个 HTTP 调用中推送多个值/对象?

[英]Power BI Push Dataset w/ Powershell; How to push multiple values/objects in a single HTTP call?

I have a powershell script that queries a database to retrieve a single row with multiple different columns.我有一个 powershell 脚本,它查询数据库以检索具有多个不同列的单行。 I need to run this query on multiple servers adding to the array each time and ultimately building a json object where each object in the json contains the 4 columns:我需要在每次添加到数组的多个服务器上运行此查询,并最终构建一个 json object 其中每个 object 包含 Z466DEEC7146ECDF5FCA6ZD38

  1. Date日期
  2. Time时间
  3. TotalBlockTime总阻塞时间
  4. ServerName服务器名称

I can successfully get this to work when invoking the rest method for a single server in each loop, but I need to get all the data for each server first and THEN push the array of JSON object by calling Invoke-RestMethod.在每个循环中为单个服务器调用 rest 方法时,我可以成功地让它工作,但我需要先获取每个服务器的所有数据,然后通过调用 Invoke-C 推送 JSON ZA8CFDE6331BD59EB666F891Z 的数组

$endpoint = "https://api.powerbi.com/MyEndpoint"

$ServerList = ('localhost', 'dev')

while($true)
{
    foreach ($Server in $ServerList)
    {
    $QueryResults = Invoke-DbaQuery -SqlInstance $Server -Database "Master" -Query "
    SELECT 
    SUM(wt.wait_duration_ms) as TotalBlockTime, @@SERVERNAME as ServerName
    FROM sys.dm_tran_locks AS tm
    INNER JOIN sys.dm_os_waiting_tasks as wt ON tm.lock_owner_address = wt.resource_address
    "
    Write-Host 'Query Executed' 
  
    $TotalBlockTime = $QueryResults.TotalBlockTime

    $ServerName = $QueryResults.ServerName

    $Date = Get-Date -DisplayHint Date -Format MM/dd/yyyy

    $Time = Get-Date -DisplayHint Time -Format HH:mm:ss
    }
    Write-Host 'Building Payload'
    $payload = @{
    "Date" = $Date
    "Time" = $Time
    "TotalBlockTime" = $TotalBlockTime
    "ServerName" = $ServerName
    }

    Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json @($payload))

    write-host $payload.TotalBlockTime $payload.ServerName

    Write-Host "Date: " $Date " Time: " $Time " TotalBlockTime: " $TotalBlockTime " ServerName: " $ServerName
    sleep 2
}

You must accumulate the query results from each server in something (eg an array), then construct a JSON as specified in the documentation , with a property rows , which is an array of values:您必须将每个服务器的查询结果累积在某个东西(例如数组)中,然后按照文档中的指定构造一个 JSON ,其属性为rows ,它是一个值数组:

{
  "rows": [
    {
      "ProductID": 1,
      "Name": "Adjustable Race",
      "Category": "Components",
      "IsCompete": true,
      "ManufacturedOn": "07/30/2014"
    },
    {
      "ProductID": 2,
      "Name": "LL Crankarm",
      "Category": "Components",
      "IsCompete": true,
      "ManufacturedOn": "07/30/2014"
    },
    {
      "ProductID": 3,
      "Name": "HL Mountain Frame - Silver",
      "Category": "Bikes",
      "IsCompete": true,
      "ManufacturedOn": "07/30/2014"
    }
  ]
}

If I amend your code, it could be something like this:如果我修改你的代码,它可能是这样的:

$endpoint = "https://api.powerbi.com/MyEndpoint"

$ServerList = ('localhost', 'dev')

while($true)
{
    $ServerResults = @() # Results from each of the servers will be stored here

    foreach ($Server in $ServerList)
    {
        $QueryResults = Invoke-DbaQuery -SqlInstance $Server -Database "Master" -Query "
        SELECT 
        SUM(wt.wait_duration_ms) as TotalBlockTime, @@SERVERNAME as ServerName
        FROM sys.dm_tran_locks AS tm
        INNER JOIN sys.dm_os_waiting_tasks as wt ON tm.lock_owner_address = wt.resource_address
        "
        Write-Host 'Query Executed' 

        $TotalBlockTime = $QueryResults.TotalBlockTime

        $ServerName = $QueryResults.ServerName

        $Date = Get-Date -DisplayHint Date -Format MM/dd/yyyy

        $Time = Get-Date -DisplayHint Time -Format HH:mm:ss

        Write-Host 'Building Payload'
        $row = @{
            "Date" = $Date
            "Time" = $Time
            "TotalBlockTime" = $TotalBlockTime
            "ServerName" = $ServerName
        }

        Write-Host $row.TotalBlockTime $row.ServerName

        Write-Host "Date: " $Date " Time: " $Time " TotalBlockTime: " $TotalBlockTime " ServerName: " $ServerName
        $ServerResults += $row
    }

    $payload = @{ rows = $ServerResults }

    Invoke-RestMethod -Method Post -Uri "$endpoint" -Body (ConvertTo-Json $payload)

    Write-Host "Date: " $Date " Time: " $Time " TotalBlockTime: " $TotalBlockTime " ServerName: " $ServerName
    sleep 2
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM