简体   繁体   中英

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

  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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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