Forum Discussion
powershell command to get datasource name is not working
Below is script to try and get the datasource names for all the datasources for every report in my power bi service. It is the workspace and dataset correctly but it is not getting the datasource name correctly. for datasource name it just returns a blank like so, "Datasource: ". But what it does do correctly is return the number of Datasources. So, for example if a report has 3 datasources it will return "Datasource: " 3 times. Any help would be very appreciated.
Install-Module -Name MicrosoftPowerBIMgmt.Data
Login-PowerBI
# Fetch all workspaces
$workspaces = Get-PowerBIWorkspace
foreach ($workspace in $workspaces) {
Write-Output "Workspace: $($workspace.Name)"
# Get datasets for each workspace
$datasets = Get-PowerBIDataset -WorkspaceId $workspace.Id
foreach ($dataset in $datasets) {
Write-Output " Dataset: $($dataset.Name)"
# Get datasources for each dataset in the current workspace
$datasources = Get-PowerBIDatasource -DatasetId $dataset.Id -WorkspaceId $workspace.Id
foreach ($datasource in $datasources) {
Write-Output " DataSource: $($datasource.Name)"
}
}
}
1 Reply
- LeonPavesicSilver Contributor
Hi momo_man,
To retrieve the datasource names correctly, you can use the DataSources property of the dataset. Here's an updated version of your script that should correctly display the datasource names (I am not able to test the script at the moment):Install-Module -Name MicrosoftPowerBIMgmt.Data Login-PowerBI # Fetch all workspaces $workspaces = Get-PowerBIWorkspace foreach ($workspace in $workspaces) { Write-Output "Workspace: $($workspace.Name)" # Get datasets for each workspace $datasets = Get-PowerBIDataset -WorkspaceId $workspace.Id foreach ($dataset in $datasets) { Write-Output " Dataset: $($dataset.Name)" # Get the dataset details, including datasources $datasetDetails = Get-PowerBIDataset -WorkspaceId $workspace.Id -Id $dataset.Id -Detail foreach ($datasource in $datasetDetails.DataSources) { Write-Output " DataSource: $($datasource.Name)" } } }
My idea is to use the -Detail parameter with Get-PowerBIDataset to get all the dataset details, including datasources, and then looping through the DataSources property to get and display the datasource names.Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic