Functions/Export-BsgPbiDatasetSchedule.ps1
<#
.SYNOPSIS Save Power BI dataset schedule to a JSON file. .DESCRIPTION The schedule definition of a dataset is saved to a JSON file. .PARAMETER Source_WorkspaceId The id of the workspace you would like to save the schedule from. You can find it in the Power BI workspace URL. .PARAMETER Source_DatasetId The id of the dataset you would like to save the schedule from. You can find it in the Power BI dataset URL. .PARAMETER Path_Datasets The path to the folder, where the temporary files will be saved. Subfolders for Schedules will be created automatically. .EXAMPLE # Export schedule Import Export-BsgPbiDatasetSchedule -Source_WorkspaceId 15ebfcd7-1aa9-4c3d-8963-46c3812a559a -Source_DatasetId 4837389a-a104-4701-8039-1bb4645644d7 -Path_Datasets "C:\temp\BSG PBI Administration\Backup\Workspaces\BSGroup DA - Test Workspace\Datasets" # Export schedule Import (with details) Export-BsgPbiDatasetSchedule -Source_WorkspaceId 15ebfcd7-1aa9-4c3d-8963-46c3812a559a -Source_DatasetId 4837389a-a104-4701-8039-1bb4645644d7 -Path_Datasets "C:\temp\BSG PBI Administration\Backup\Workspaces\BSGroup DA - Test Workspace\Datasets" -v # Export schedule DirectQuery Export-BsgPbiDatasetSchedule -Source_WorkspaceId 15ebfcd7-1aa9-4c3d-8963-46c3812a559a -Source_DatasetId b1494d98-127d-4d1d-b954-ef443d484cf1 -Path_Datasets "C:\temp\BSG PBI Administration\Backup\Workspaces\BSGroup DA - Test Workspace\Datasets" .INPUTS .OUTPUTS .NOTES This script uses the Power BI Management module for Windows PowerShell. If this module isn't installed, install it by using the command 'Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser'. #> function Export-BsgPbiDatasetSchedule{ param( [Parameter(Mandatory=$true)][string]$Source_WorkspaceId, [Parameter(Mandatory=$true)][guid]$Source_DatasetId, [Parameter(Mandatory=$true)][string]$Path_Datasets ) try{ # Define base URLs for API requests if ($Source_WorkspaceId -eq 'me'){ $Source_WorkspaceUrl = "https://api.powerbi.com/v1.0/myorg" } else{ $Source_WorkspaceUrl = "https://api.powerbi.com/v1.0/myorg/groups/" + $Source_WorkspaceId } $Source_DatasetUrl = $Source_WorkspaceUrl + "/datasets/" + $Source_DatasetId # === # Get dataset # = try{ if ($Source_WorkspaceId -eq 'me'){ $Source_Dataset = Get-PowerBIDataset -Id $Source_DatasetId -ErrorAction Stop } else{ $Source_Dataset = Get-PowerBIDataset -WorkspaceId $Source_WorkspaceId -Id $Source_DatasetId -ErrorAction Stop } $Source_DatasetName = $Source_Dataset.name } catch{ throw "Error trying to get Power BI dataset with dataset id $Source_DatasetId and workspace id $Source_WorkspaceId. `n$_" } # === # Check if dataset exists # = if (!$Source_Dataset.id){ throw "No dataset found with dataset id $Source_DatasetId and workspace id $Source_WorkspaceId." } # === # Get schedule depending on type of schedule # = if ($Source_Dataset.isRefreshable){ # === # Import schedule # = $RequestUrl = $Source_DatasetUrl + "/refreshSchedule" # API-call try{ $Source_Response = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get } catch{ throw "Error after calling request URL: $RequestUrl." } # Prepare final path $Path = Join-Path -Path $Path_Datasets -ChildPath "refreshSchedules" } else{ # === # DirectQuery schedule # = $RequestUrl = $Source_DatasetUrl + "/directQueryRefreshSchedule" # API-call try{ $Source_Response = Invoke-PowerBIRestMethod -Url $RequestUrl -Method Get } catch{ throw "Error after calling request URL: $RequestUrl." } # Prepare final path $Path = Join-Path -Path $Path_Datasets -ChildPath "directQueryRefreshSchedules" } # === # Save to JSON file # = if ($Source_Response){ # Create temporary directories if((Test-Path $Path) -eq $false){ $FileCreatedResponse = New-Item -Path $Path -ItemType Directory -ErrorAction SilentlyContinue # Write-PSFMessage -Level Verbose -Message "Temp directory created: $Path" } # Save file $Filename = $Source_DatasetName + "_" + $Source_DatasetId + ".json" $Path_Schedules = Join-Path -Path $Path -ChildPath $Filename $Source_Response | Out-File $Path_Schedules # Write-PSFMessage -Level Verbose -Message " Location: `"$Path_Schedules`"" } else{ Write-Host Write-Warning "No data received." Write-PSFHostColor -Level Host -DefaultColor gray -String "Schedule skipped." Write-Host } # Message if ($Source_Dataset.isRefreshable){ # Write-PSFMessage -Level Host -FunctionName "Export-BsgPbiDataset" -Message " Refreshable schedule exported." # Write-PSFMessage -Level Verbose -FunctionName "Export-BsgPbiDataset" -Message " Schedule exported." } else { # Write-PSFMessage -Level Host -FunctionName "Export-BsgPbiDataset" -Message " Not refreshable schedule exported." # Write-PSFMessage -Level Verbose -FunctionName "Export-BsgPbiDataset" -Message " Schedule exported." } }catch{ if ($Source_DatasetName){ Write-Host Stop-PSFFunction -Message "Could not export schedule for dataset $Source_DatasetName." -EnableException $False -Errorrecord $_ return } else{ Write-Host Stop-PSFFunction -Message "Could not export schedule." -EnableException $False -ErrorRecord $_ return } } } |