-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathPowerBI_GenerateDtapWorkspaces.ps1
103 lines (86 loc) · 4.5 KB
/
PowerBI_GenerateDtapWorkspaces.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# =================================================================================================================================================
# Check if Power BI Module is available, if not install
# =================================================================================================================================================
$moduleName = Get-Module -ListAvailable -Verbose:$false | Where-Object { $_.Name -eq "MicrosoftPowerBIMgmt" } | Select-Object -ExpandProperty Name;
if ([string]::IsNullOrEmpty($moduleName)) {
Write-Host -ForegroundColor White "==============================================================================";
Write-Host -ForegroundColor White "Install module MicrosoftPowerBIMgmt...";
Install-Module MicrosoftPowerBIMgmt -SkipPublisherCheck -AllowClobber -Force
Write-Host -ForegroundColor White "==============================================================================";
}
# =================================================================================================================================================
# Generic Tasks
# =================================================================================================================================================
# Variables
$workspaceName = "sample workspace"
$capacityId = "5ABEFB55-8DFB-42D0-B2A2-A6720D45A8A8"
$tenantId = "{InsertTenantId}" #Only applicable with SPN authentication
# DTAP stages setup
$dev = "$workspaceName" + "-dev"
$tst = "$workspaceName" + "-tst"
$prd = "$workspaceName"
$stages = @($dev, $tst, $prd)
# Create empty json array
$wsCreated = @()
# =================================================================================================================================================
# Authentication
# =================================================================================================================================================
#Connect to Power BI service Account
Write-Host -ForegroundColor White "Connect to PowerBI service"
Connect-PowerBIServiceAccount #Connect with service account
#Connect using SPN
Connect-PowerBIServiceAccount -ServicePrincipal -Credential (Get-Credential) -Tenant $tenantId
$ClientId = $SpInlog.ClientId
# Note: This SPN should have Tenant.ReadWriteAll permissions to execute this script
# =================================================================================================================================================
# Workspace actions
# =================================================================================================================================================
Function SetupWorkspace {
[cmdletbinding()]
param (
[parameter(Mandatory = $true)][string]$wsName
)
# Create workspace
$bodyCreate =
@"
{
"name": "$wsName"
}
"@
$createWs = Invoke-PowerBIRestMethod -Method POST -Url "https://api.powerbi.com/v1.0/myorg/groups" -Body $bodyCreate | ConvertFrom-Json
$wsId = $createWs.id
Write-Host "Workspace '$workspaceName' created with id $wsId" -ForegroundColor Green
# Assign workspace to capacity
$bodyAssign =
@"
{
"capacityId": "$capacityId"
}
"@
$assign = "https://api.powerbi.com/v1.0/myorg/groups/" + $createWs.id + "/AssignToCapacity"
$assignToCapacity = Invoke-PowerBIRestMethod -Method POST -Url $assign -Body $bodyAssign | ConvertFrom-Json
Write-Host "$wsId assigned to capacity $capacityId" -ForegroundColor Green
# Set storage format to large
$bodyStorageFormat =
@"
{
"defaultDatasetStorageFormat": "Large"
}
"@
$storageformat = "https://api.powerbi.com/v1.0/myorg/groups/" + $createWs.id
$changeStorageFormat = Invoke-PowerBIRestMethod -Method PATCH -Url $storageformat -Body $bodyStorageFormat | ConvertFrom-Json
Write-Host "$wsId storage format set to Large" -ForegroundColor Green
}
# =================================================================================================================================================
# Generate workspaces
# =================================================================================================================================================
# Generate Workspaces
foreach($wsName in $stages) {
Write-Host "Identified stage workspace: $wsName"
$CreateWorkspaces = SetupWorkspace -WsName $wsName
foreach($wsCreated in $CreateWorkspaces) {
Add-Member -InputObject $wsId -NotePropertyName 'WorkspaceId' -NotePropertyValue $wsId
$wsCreated += $wsId
}
}
# filling above array with all generated workspaces is not yet working properly, array contains same value all the time