Home > Enterprise >  PowerShell & Power BI Rest API
PowerShell & Power BI Rest API

Time:10-05

Essentially what I'm after is the results of rest API Gateways - Get Datasource Users but retaining the ID (in this example $Line.id from my imported CSV file).

The end result should be a CSV with the following fields - ID, emailAddress, datasourceAccessRight, displayName, identifier, principalType

I'm new to PowerShell and surprised I got this far but can't figure out this final bit.

Cheers

$webclient=New-Object System.Net.WebClient
$webclient.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

$Dir = "C:\pbi_pro_user_logs\"

Login-PowerBI

$GateWayFile = Import-CSV -Path "C:\pbi_pro_user_logs\Gateway_Detail.csv"
$Output = @()
foreach ($Line in $GateWayFile){
    $Item = $Line.id
    $url =  "https://api.powerbi.com/v1.0/myorg/gateways/HIDDEN/datasources/" $Item "/users"
    $Output  = (Invoke-PowerBIRestMethod -Url $url -Method Get | ConvertFrom-Json)
}

$Result = $Output.value

$Result | Export-Csv $Dir"GateWay_users.csv" -NoTypeInformation

CodePudding user response:

Try this, using a calculated property from Select-Object:

$GateWayFile = Import-CSV -Path "C:\pbi_pro_user_logs\Gateway_Detail.csv"
$Output = Foreach ($Line in $GateWayFile){
    $url =  "https://api.powerbi.com/v1.0/myorg/gateways/HIDDEN/datasources/" $Line.id "/users"
    $Item = (Invoke-PowerBIRestMethod -Url $url -Method Get | ConvertFrom-Json)

    # output all properties of the item, plus the ID:
    $ItemWithID = $Item | Select *,@{l='Id';e={$line.id}}
    Write-Output $ItemWithID
}

# This depends on how you want your csv structured, but for example:
$Result = $Output | Select Id,Value

Or, if Value is a whole object that ID should be assigned inside of, then change the selection lines:

  $ItemWithID = $Item.Value | Select *,@{l='Id';e={$line.id}}
$Result = $Output
  • Related