-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCopy-GoalInScoreCard.ps1
208 lines (169 loc) · 6.6 KB
/
Copy-GoalInScoreCard.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<#
.SYNOPSIS
Duplicate a goal in a Power BI scorecard
.DESCRIPTION
Duplicate a goal in a Power BI scorecard to the same scorecard, specifying the number of duplicates to create.
.EXAMPLE
.\Copy-GoalInScoreCard.ps1
.NOTES
Warning: This script uses an internal API that is not officially supported by Microsoft. It may break at any time. Use at your own risk.
TODO: Add parameters to skip the prompt
.LINK
[Source code](https://github.com/JamesDBartlett3/ps-for-pbi/blob/master/Copy-GoalInScoreCard.ps1)
.LINK
[The author's blog](https://datameerkat.com/)
.LINK
[Follow the author on LinkedIn](https://www.linkedin.com/in/stepan-resl/)
.LINK
[Follow the author on Mastodon](https://techhub.social/deck/@StepanResl)
.LINK
[Follow the author on BlueSky](https://bsky.app/profile/stepanresl.bsky.social)
#>
# PowerShell dependencies
#Requires -Modules MicrosoftPowerBIMgmt
$ErrorActionPreference = 'Stop'
$publicEndpoint = 'https://api.powerbi.com'
if (!(Get-Module -ListAvailable -Name MicrosoftPowerBIMgmt)) {
try {
Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser -AllowClobber -Confirm:$False -Force
}
catch [Exception] {
$_.message
exit
}
}
$headers = [System.Collections.Generic.Dictionary[[String], [String]]]::New()
try {
$headers = Get-PowerBIAccessToken
}
catch {
Write-Host '🔒 Power BI Access Token required. Launching Azure Active Directory authentication dialog...'
Start-Sleep -s 1
Connect-PowerBI -WarningAction SilentlyContinue | Out-Null
$headers = Get-PowerBIAccessToken
}
if ($headers) {
Write-Host '🔑 Power BI Access Token acquired. Proceeding...'
}
else {
Write-Host '❌ Power BI Access Token not acquired. Exiting...'
exit
}
$token = $headers['Authorization']
function GetApiUrl() {
$response = Invoke-WebRequest -Uri "$publicEndpoint/metadata/cluster" -Headers @{ 'Authorization' = $token }
$cluster = $response.Content | ConvertFrom-Json
return $cluster.backendUrl
}
$api = GetApiUrl
function GetScorecard($scorecardId) {
Write-Host 'Retrieving scorecard...' -NoNewline
$response = Invoke-WebRequest `
-Uri "$api/v1.0/myOrg/internalScorecards($scorecardId)?`$expand=goals" `
-Headers @{ 'Authorization' = $token }
Write-Host -ForegroundColor Green OK
$scorecard = $response.Content | ConvertFrom-Json
return $scorecard
}
function GetGoal($scorecardId, $goalId) {
Write-Host 'Retrieving scorecard...' -NoNewline
$response = Invoke-WebRequest `
-Uri "$api/v1.0/myOrg/internalScorecards($scorecardId)/goals($goalId)" `
-Headers @{ 'Authorization' = $token }
Write-Host -ForegroundColor Green OK
$goal = $response.Content | ConvertFrom-Json
return $goal
}
function CopyGoal($sourceGoal, $destinationScorecardId, $parentGoalId, $destinationWsId) {
Write-Host "Copying ""$($sourceGoal.name)""... " -NoNewline
try {
$newGoalRequest = $sourceGoal | Select-Object -Property name, startDate, completionDate, unit, owner, additionalOwners, valuesFormatString, datesFormatString
$newGoalRequest.name = 'PLACEHOLDER'
$response = Invoke-WebRequest `
-Method Post `
-Uri "$api/v1.0/myOrg/groups/$destinationWsId/internalScorecards($destinationScorecardId)/goals" `
-Headers @{ 'Authorization' = $token } `
-Body ($newGoalRequest | ConvertTo-Json) `
-ContentType 'application/json'
$goal = $response.Content | ConvertFrom-Json
Write-Host -ForegroundColor Green OK
if ($sourceGoal.statusRules) {
CopyStatusRules -goal $sourceGoal -destinationScorecardId $destinationScorecardId -destinationGoalId $goal.id
}
return $goal
}
catch {
Write-Host -ForegroundColor Red 'Could not copy goal'
throw
}
}
function CopyStatusRules($goal, $destinationScorecardId, $destinationGoalId) {
Write-Host ' - Copying status rules... ' -NoNewline
$response = Invoke-WebRequest `
-Uri "$api/v1.0/myOrg/internalScorecards($($goal.scorecardId))/goals($($goal.id))/statusRules" `
-Headers @{ 'Authorization' = $token }
$rules = $response.Content | ConvertFrom-Json
$response = Invoke-WebRequest `
-Method Post `
-Uri "$api/v1.0/myOrg/internalScorecards($destinationScorecardId)/goals($destinationGoalId)/statusRules" `
-Body ($rules | ConvertTo-Json -Depth 100) `
-ContentType 'application/json' `
-Headers @{ 'Authorization' = $token }
Write-Host -ForegroundColor Green OK
}
function DuplicateGoal($idScorecard, $sourceScorecard, $duplicates, $sourceGoal, $destinationWSId) {
$goalsByParentId = @{}
$topLevelGoals = @()
if ($sourceGoal.parentId) {
if (!$goalsByParentId[$sourceGoal.parentId]) {
$goalsByParentId[$sourceGoal.parentId] = @()
}
$goalsByParentId[$sourceGoal.parentId] += $sourceGoal
}
else {
$topLevelGoals += $sourceGoal
}
Write-Host "Duplicating $($originGoalId.goals.Length+1) goal to scorecard $sourceScorecard..."
for (($refreshes = 0); $refreshes -lt $duplicates; $refreshes++) {
CopyGoal -sourceGoal $sourceGoal -destinationScorecardId $idScorecard -parentGoalId $goalsByParentId -destinationWsId $destinationWSId
Write-Host "Count of done duplicates: $($refreshes+1) "
}
Write-Host 'Done'
}
function ShowPrompt() {
while ($true) {
Write-Host -ForegroundColor Yellow 'Goal cloning utility'
Write-Host 'Choose action:'
Write-Host ' [d] - Duplicate goal in scorecard'
Write-Host ' [q] - Quit'
$action = Read-Host -Prompt 'Choose action or press enter to duplicate a scorecard'
break
}
if ($action -and ($action -ne 'd')) {
if ($action -ne 'q') {
Write-Host -ForegroundColor red 'Invalid action'
return $false
}
}
$scorecardId = Read-Host -Prompt 'Enter source scorecard id'
if (!$scorecardId) {
Write-Error 'Invalid scorecard id'
}
$sourceScorecard = GetScorecard -scorecardId $scorecardId
Write-Host -ForegroundColor Green "Scorecard: $($sourceScorecard.name). Workspace: $($sourceScorecard.groupId)"
$destinationWorkspaceId = $sourceScorecard.groupId
$goalId = Read-Host -Prompt 'Enter source goal id'
if (!$scorecardId) {
Write-Error 'Invalid scorecard id'
}
$sourceGoal = GetGoal -scorecardId $scorecardId -goalId $goalId
Write-Host -ForegroundColor Green "Goal: $($sourceGoal.name)"
$numberOfDuplicates = Read-Host -Prompt 'Enter number of duplicates to create'
if (!$numberOfDuplicates) {
Write-Error 'Invalid number of duplicates'
}
if ($action -eq 'd') {
DuplicateGoal -idScorecard $scorecardId -sourceScorecard $sourceScorecard -duplicates $numberOfDuplicates -sourceGoal $sourceGoal -destinationWSId $destinationWorkspaceId
}
}
ShowPrompt