Skip to content

New cmdlets to manage Power BI Capacities #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ Modules/PowerBIETL/PublishToGallery.ps1

/.vs/slnx.sqlite
/.vs
ImportPBIStuff.ps1
Test*.ps1
2 changes: 2 additions & 0 deletions Modules/PowerBIPS/PowerBIPS.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ FunctionsToExport = @(
, "Get-PBIDatasources", "Invoke-PBIRequest", "Get-PBIModuleConfig", "Set-PBIModuleConfig"
, "Set-PBIReportContent", "Get-PBIAuthTokenHttp", "Remove-PBIDataSet"
, "Remove-PBIReport"
, "Get-PBICapacities", "Set-PBICapacity", "Clear-PBICapacity","Get-PBICapacityAssignmentStatus"
, "Get-PBICapacityWorkloads", "Set-PBICapacityWorkload"
)

# Cmdlets to export from this module
Expand Down
265 changes: 265 additions & 0 deletions Modules/PowerBIPS/PowerBIPS.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,271 @@ Function Get-PBIDatasources{
}
}

#region Capacities

Function Get-PBICapacities{
<#
.SYNOPSIS
Gets an array of avaliable capacities the user has access to
.DESCRIPTION
Gets an array of avaliable capacities the user has access to
.PARAMETER AuthToken
The authorization token required to communicate with the PowerBI APIs
Use 'Get-PBIAuthToken' to get the authorization token string
.EXAMPLE
Get-PBICapacities -authToken $authtoken
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string] $authToken
)

Write-Verbose "Getting Capacities"

$resource = "capacities"

$capacities = @(Invoke-PBIRequest -authToken $authToken -method Get -resource $resource -ignoreGroup)

Write-Verbose "Found $($capacities.count) capacities."

Write-Output $capacities
}

Function Set-PBICapacity{
<#
.SYNOPSIS
Assigns the specified workspace to the specified capacity
.DESCRIPTION
Assigns the specified workspace to the specified capacity.
Note: To perform this operation, the user must be admin on the specified workspace and have admin or assign permissions on the capacity.
.PARAMETER AuthToken
The authorization token required to communicate with the PowerBI APIs
Use 'Get-PBIAuthToken' to get the authorization token string
.PARAMETER Capacity
The Capacity object or the capacity ID (GUID)
.PARAMETER GroupId
Id (GUID) of the workspace to which the capacity will be assigned
.EXAMPLE
Set-PBICapacity -authToken $authtoken -capacity {your_capacity}
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string] $authToken,
[Parameter(Mandatory=$true, ValueFromPipeline = $true)] $capacity,
[Parameter(Mandatory=$false)] [string] $groupId
)
begin {}
process
{
$capacityId = ""
if ($capacity -is [string])
{
$capacityId = $capacity
}
else
{
$capacityId = $capacity.id
}

Write-Verbose "Assigning Capacity"

$resource = "AssignToCapacity"

$bodyObj = @{capacityId=$capacityId}

$capacities = @(Invoke-PBIRequest -authToken $authToken -method Post -resource $resource -Body ($bodyObj | ConvertTo-Json) -groupId $groupId)

Write-Verbose "Assigned Capacity $($capacity.displayName) $($capacity.id)."

Write-Output $capacities
}
}

Function Clear-PBICapacity{
<#
.SYNOPSIS
Unassign currently assigned capacity
.DESCRIPTION
Unassign currently assigned capacity
.PARAMETER AuthToken
The authorization token required to communicate with the PowerBI APIs
Use 'Get-PBIAuthToken' to get the authorization token string
.PARAMETER GroupId
Id (GUID) of the workspace to which the capacity will be unassigned
.EXAMPLE
Clear-PBICapacity -authToken $authtoken
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string] $authToken,
[Parameter(Mandatory=$false)] [string] $groupId
)
begin {}
process
{
Write-Verbose "Unassigning Capacity"

$capacityId = "00000000-0000-0000-0000-000000000000"

$resource = "AssignToCapacity"

$bodyObj = @{capacityId=$capacityId}

$capacities = @(Invoke-PBIRequest -authToken $authToken -method Post -resource $resource -Body ($bodyObj | ConvertTo-Json) -groupId $groupId)

Write-Verbose "Unassigned Capacity."

Write-Output $capacities
}
}

Function Get-PBICapacityAssignmentStatus{
<#
.SYNOPSIS
Gets the status of the assignment to capacity operation of the specified workspace
.DESCRIPTION
Gets the status of the assignment to capacity operation of the specified workspace.
Note: To perform this operation, the user must be admin on the specified workspace.
.PARAMETER AuthToken
The authorization token required to communicate with the PowerBI APIs
Use 'Get-PBIAuthToken' to get the authorization token string
.PARAMETER GroupId
Id (GUID) of the workspace to which the capacity will be unassigned
.EXAMPLE
Get-PBICapacityAssignmentStatus -authToken $authtoken
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string] $authToken,
[Parameter(Mandatory=$false)] [string] $groupId
)

Write-Verbose "Getting Capacity Assignment Status"

$resource = "CapacityAssignmentStatus"

$capacityStatus = @(Invoke-PBIRequest -authToken $authToken -method Get -resource $resource)

Write-Output $capacityStatus
}

Function Get-PBICapacityWorkloads{
<#
.SYNOPSIS
Returns the current state of the specified capacity workloads.
.DESCRIPTION
Returns the current state of the specified capacity workloads.
If a workload is enabled also returns the maximum memory percentage that the workload can consume.
.PARAMETER AuthToken
The authorization token required to communicate with the PowerBI APIs
Use 'Get-PBIAuthToken' to get the authorization token string
.PARAMETER Capacity
The Capacity object or the capacity ID (GUID)
.PARAMETER WorkloadName
The workload name
.EXAMPLE
Get-PBICapacityWorkloads -authToken $authtoken -capacity $capacity
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string] $authToken,
[Parameter(Mandatory=$true, ValueFromPipeline = $true)] $capacity,
[Parameter(Mandatory=$false)] [string] $workloadName
)

$capacityId = ""
if ($capacity -is [string])
{
$capacityId = $capacity
}
else
{
$capacityId = $capacity.id
}

Write-Verbose "Getting Capacity Workloads"

$resource = "capacities/$capacityId/Workloads"

if (![string]::IsNullOrEmpty($workloadName))
{
$resource += "/$workloadName"
}

$workloads = @(Invoke-PBIRequest -authToken $authToken -method Get -resource $resource -ignoreGroup)

Write-Verbose "Found $($workloads.count) workloads."

Write-Output $workloads
}

Function Set-PBICapacityWorkload{
<#
.SYNOPSIS
Changes the state and/or maximum memory percentage of the specified workload
.DESCRIPTION
Changes the state of a specific workload to Enabled or Disabled.
When enabling a workload the maximum memory percentage that the workload can consume must be set.
.PARAMETER AuthToken
The authorization token required to communicate with the PowerBI APIs
Use 'Get-PBIAuthToken' to get the authorization token string
.PARAMETER Capacity
The Capacity object or the capacity ID (GUID)
.PARAMETER WorkloadName
The workload name
.PARAMETER Disable
By default, the cmdlet will Enable the workload. Use -disable to Disable it.
.PARAMETER maxMemoryPercentageSetByUser
The memory percentage maximum limit set by the user. Mandatory if you are gonna Enable the workload.
.EXAMPLE
Set-PBICapacityWorkload -authToken $authtoken -capacity $capacity -workloadName "Dataflows" -maxMemoryPercentageSetByUser 20
.EXAMPLE
Set-PBICapacityWorkload -authToken $authtoken -capacity $capacity -workloadName "Dataflows" -disable
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)] [string] $authToken,
[Parameter(Mandatory=$true, ValueFromPipeline = $true)] $capacity,
[Parameter(Mandatory=$true)] [string] $workloadName,
[Parameter(Mandatory=$false)] [switch] $disable,
[Parameter(Mandatory=$false)] [int] $maxMemoryPercentageSetByUser,
[Parameter(Mandatory=$false)] [string] $groupId
)
begin {}
process
{
$capacityId = ""
if ($capacity -is [string])
{
$capacityId = $capacity
}
else
{
$capacityId = $capacity.id
}

if ($disable){
$bodyObj = @{
state = "Disabled"
}
}
else {
$bodyObj = @{
state = "Enabled"
maxMemoryPercentageSetByUser = "$maxMemoryPercentageSetByUser"
}
}

$resource = "capacities/$capacityId/Workloads/$workloadName"

Invoke-PBIRequest -authToken $authToken -method Patch -resource $resource -Body ($bodyObj | ConvertTo-Json) -ignoreGroup

Write-Verbose "$workloadName Workload changed"
}
}

#endregion

Function Invoke-PBIRequest{
<#
.SYNOPSIS
Expand Down
71 changes: 71 additions & 0 deletions Modules/PowerBIPS/doc/Clear-PBICapacity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
external help file: PowerBIPS-help.xml
Module Name: PowerBIPS
online version:
schema: 2.0.0
---

# Clear-PBICapacity

## SYNOPSIS
Unassign currently assigned capacity

## SYNTAX

```
Clear-PBICapacity [[-authToken] <String>] [[-groupId] <String>] [<CommonParameters>]
```

## DESCRIPTION
Unassign currently assigned capacity

## EXAMPLES

### EXAMPLE 1
```
Clear-PBICapacity -authToken $authtoken
```

## PARAMETERS

### -authToken
The authorization token required to communicate with the PowerBI APIs
Use 'Get-PBIAuthToken' to get the authorization token string

```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -groupId
Id (GUID) of the workspace to which the capacity will be unassigned

```yaml
Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).

## INPUTS

## OUTPUTS

## NOTES

## RELATED LINKS
Loading