Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
Anonymous
Not applicable

Create streaming dataset from powershell script

Hi All,

I am trying to build a streaming dataset using power shell script. 

On powershell doc there is not this option:
Add-PowerBIDataset (MicrosoftPowerBIMgmt.Data) | Microsoft Docs

On the other hand looks like that it is possible from rest api:
Push Datasets - Datasets PostDataset - REST API (Power BI Power BI REST APIs) | Microsoft Docs

 

Not sure if a solution could be to call the rest api from powershell. 
Is there a way to do it using the powershell cmdlet authentication?

Are there Any other sdk that allows it?


For instance:

# User credential

$User = "admin@CRM738334.onmicrosoft.com"
$Pword = ConvertTo-SecureString –String 'bL3JZOyTDU' –AsPlainText -Force
$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pword

# Connect to service
Connect-PowerBIServiceAccount -Credential $Credential

1 ACCEPTED SOLUTION

Hi @Anonymous,

Your code looks fine except for the way you pass the JSON value. You can use:

 

# User credential
$User = "admin@CRM738334.onmicrosoft.com"
$Pword = ConvertTo-SecureString –String 'bL3JZOyTDU' –AsPlainText -Force
$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pword

# Connect to AAD
$AzureProfile = Connect-AzAccount -Credential $Credential

# Get an AccessToken to the Power BI service
$AccessTokenDetails = Get-AzAccessToken -ResourceUrl 'https://analysis.windows.net/powerbi/api' -DefaultProfile $AzureProfile

$Headers = @{
    'Authorization' = "Bearer $($AccessTokenDetails.Token)"
}

$Url = "https://api.powerbi.com/v1.0/myorg/datasets?defaultRetentionPolicy=basicFIFO"
$Body = '{
  "name": "SalesMarketing",
  "defaultMode": "Push",
  "tables": [
    {
      "name": "Product",
      "columns": [
        {
          "name": "ProductID",
          "dataType": "Int64"
        }
      ]
    }
  ]
}'

Invoke-RestMethod -Method 'Post' -Headers $Headers -Uri $Url -Body $body -ContentType 'application/json'

 


This worked for me.

SpartaBI_0-1652351846014.png




2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

View solution in original post

5 REPLIES 5
SpartaBI
Community Champion
Community Champion

Hi @Anonymous,

You can call the Post Dataset API like any other API using PowerShell. It definitely works.

If you want to authenticate using a PowerShell SDK, you can use the module Az.Accounts
Building on your example:

 

# User credential
$User = "admin@CRM738334.onmicrosoft.com"
$Pword = ConvertTo-SecureString –String 'bL3JZOyTDU' –AsPlainText -Force
$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pword

# Connect to AAD
$AzureProfile = Connect-AzAccount -Credential $Credential

# Get an AccessToken to the Power BI service
$AccessTokenDetails = Get-AzAccessToken -ResourceUrl 'https://analysis.windows.net/powerbi/api' -DefaultProfile $AzureProfile

$Headers = @{
    'Authorization' = "Bearer $($AccessTokenDetails.Token)"
}

# Call an API
Invoke-RestMethod -Headers $Headers # Complete the call...

 



2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

Anonymous
Not applicable

Hi SpartaBi,

using your suggested procedure and appending the rest request, as below, I am getting 403:

 

# User credential
$User = "admin@CRM738334.onmicrosoft.com"
$Pword = ConvertTo-SecureString –String 'bL3JZOyTDU' –AsPlainText -Force
$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pword

# Connect to AAD
$AzureProfile = Connect-AzAccount -Credential $Credential

# Get an AccessToken to the Power BI service
$AccessTokenDetails = Get-AzAccessToken -ResourceUrl 'https://analysis.windows.net/powerbi/api' -DefaultProfile $AzureProfile

$Headers = @{
    'Authorization' = "Bearer $($AccessTokenDetails.Token)"
}

$Url = "https://api.powerbi.com/v1.0/myorg/datasets?defaultRetentionPolicy=basicFIFO"
$Body = '{
  "name": "SalesMarketing",
  "defaultMode": "Push",
  "tables": [
    {
      "name": "Product",
      "columns": [
        {
          "name": "ProductID",
          "dataType": "Int64"
        }
      ]
    }
  ]
}'
$Body = $Body | ConvertFrom-Json
Invoke-RestMethod -Method 'Post' -Headers $Headers -Uri $Url -Body $body 
Invoke-RestMethod : The remote server returned an error: (403) Forbidden.
At line:1 char:1
+ Invoke-RestMethod -Method 'Post' -Headers $Headers -Uri $Url -Body $b ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

 

 

Hi @Anonymous,

Your code looks fine except for the way you pass the JSON value. You can use:

 

# User credential
$User = "admin@CRM738334.onmicrosoft.com"
$Pword = ConvertTo-SecureString –String 'bL3JZOyTDU' –AsPlainText -Force
$Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $User, $Pword

# Connect to AAD
$AzureProfile = Connect-AzAccount -Credential $Credential

# Get an AccessToken to the Power BI service
$AccessTokenDetails = Get-AzAccessToken -ResourceUrl 'https://analysis.windows.net/powerbi/api' -DefaultProfile $AzureProfile

$Headers = @{
    'Authorization' = "Bearer $($AccessTokenDetails.Token)"
}

$Url = "https://api.powerbi.com/v1.0/myorg/datasets?defaultRetentionPolicy=basicFIFO"
$Body = '{
  "name": "SalesMarketing",
  "defaultMode": "Push",
  "tables": [
    {
      "name": "Product",
      "columns": [
        {
          "name": "ProductID",
          "dataType": "Int64"
        }
      ]
    }
  ]
}'

Invoke-RestMethod -Method 'Post' -Headers $Headers -Uri $Url -Body $body -ContentType 'application/json'

 


This worked for me.

SpartaBI_0-1652351846014.png




2022-05-09 22_36_04-Power BI Workspace Datasets License Permissions - Microsoft Power BI Community.png

Showcase Report – Contoso By SpartaBI


SpartaBI_3-1652115470761.png  SpartaBI_1-1652115142093.png   SpartaBI_2-1652115154505.png

Full-Logo11.png

Anonymous
Not applicable

@SpartaBI I really thank you so much!!! Really!

@Anonymous my pleasure

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.