forked from anshulagrawal07/sfsnowsightextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopy-SFObjects.ps1
563 lines (500 loc) · 29.5 KB
/
Copy-SFObjects.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
function Copy-SFObjects ()
{
[CmdletBinding()]
param (
[Parameter(Mandatory)] [String]$SFObjectTypes,
[Parameter(Mandatory)] [String]$SourceAccountLocatorOrFilepath,
[Parameter()] [String]$TargetAccounts,
[Parameter()] [String]$SFRole='ACCOUNTADMIN',
[Parameter()] [String]$SFWarehouse='COMPUTE_WH',
[Parameter()] [String]$SFDatabase,
[Parameter()] [String]$SFSchema,
[Parameter()] [String]$OutputDirectory,
[Parameter()] [ValidateSet("CreateNew", "Skip")] [String]$ActionIfExistsDashboard='Skip',
[Parameter()] [ValidateSet("Skip", "Overwrite")] [String]$ActionIfExistsFilter='Skip',
[Parameter()] [ValidateSet("CreateNew", "Skip", "Overwrite")] [String]$ActionIfExistsWorksheet='Skip',
[Parameter()] [bool]$SSO=$false,
[Parameter()] [bool]$CleanFiles=$false,
[Parameter()] [bool]$InteractiveMode=$true
)
# Build a hashtable of values for iterating through in target account uploads
$SourceReplacementValues = @{
Role = $SFRole
Warehouse = $SFWarehouse
Database = $SFDatabase
Schema = $SFSchema
}
# Retrieve Source Account Objects
if (-Not (Test-Path $SourceAccountLocatorOrFilepath)) {
Write-Host "`r`nNo file path detected, attempting to connect to account locator $($SourceAccountLocatorOrFilepath).`r`n" -ForegroundColor Red
# If InteractiveMode, prompt for SSO Input per every source and target account
if ($InteractiveMode){
Write-Host "`r`nRunning in InteractiveMode. This program will prompt for inputs.`r`n" -ForegroundColor Red
if (SSOPrompt($SourceAccountLocatorOrFilepath)){
$SourceContext = Connect-SFApp -Account $SourceAccountLocatorOrFilepath -SSO
}
else {
$SourceContext = Connect-SFApp -Account $SourceAccountLocatorOrFilepath
}
}
else {
if ($SSO){
$SourceContext = Connect-SFApp -Account $SourceAccountLocatorOrFilepath -SSO
}
else {
$SourceContext = Connect-SFApp -Account $SourceAccountLocatorOrFilepath
}
}
# OutputDirectory used to create an account specific OutPath aka MyOutputDirectory/MyAcctLocator123/[filters,dashboards,worksheets]
if ($OutputDirectory) {
$OutPath = "$OutputDirectory/$SourceAccountLocatorOrFilepath"
}
# Import Each Object Type
$SFObjectTypes -Split ',' | ForEach-Object {
$obj = $_.Trim().ToLower()
Write-Host "`r`nRetreiving $obj from source account.`r`n" -ForegroundColor Cyan
# Pull down objects + update object wh, role, db, schema
if ($obj -eq "all") {
$SourceFilters = Get-SFFilters -AuthContext $SourceContext
$SourceFilters = $SourceFilters | Where-Object { $_.Scope -ne 'global'} # Ensure the global snowflake filters are not retrieved at all
$SourceDashboards = Get-SFDashboards -AuthContext $SourceContext
$SourceWorksheets = Get-SFWorksheets -AuthContext $SourceContext
# If an OutputDirectory is provided, write to it
if ($OutPath){
# Save the source objects as files
$SourceFilters | ForEach-Object {$_.SaveToFolder("$OutPath/filters")}
$SourceDashboards | ForEach-Object {$_.SaveToFolder("$OutPath/dashboards")}
$SourceWorksheets | ForEach-Object {$_.SaveToFolder("$OutPath/worksheets")}
# if CleanFiles, rename and clean the source account file.
if ($CleanFiles){
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $SFRole -SFWarehouse $SFWarehouse -WorksheetsPath "$OutPath/worksheets" -DashboardsPath "$OutPath/dashboards" -FiltersPath "$OutPath/filters" -SFDatabase $SFDatabase -SFSchema $SFSchema
}
}
}
}
# Individual object handling
elseif($obj -eq "filters") {
$SourceFilters = Get-SFFilters -AuthContext $SourceContext
$SourceFilters = $SourceFilters | Where-Object { $_.Scope -ne 'global'}
if ($OutPath){
$SourceFilters | ForEach-Object {$_.SaveToFolder("$OutPath/filters")}
if ($CleanFiles) {
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $SFRole -SFWarehouse $SFWarehouse -DashboardsPath "$OutPath/dashboards" -FiltersPath "$OutPath/filters" -SFDatabase $SFDatabase -SFSchema $SFSchema
}
}
}
}
elseif ($obj -eq "dashboards") {
$SourceDashboards = Get-SFDashboards -AuthContext $SourceContext
if ($OutPath){
$SourceDashboards | ForEach-Object {$_.SaveToFolder("$OutPath/dashboards")}
if ($CleanFiles) {
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $SFRole -SFWarehouse $SFWarehouse -DashboardsPath "$OutPath/dashboards" -SFDatabase $SFDatabase -SFSchema $SFSchema
}
}
}
}
elseif ($obj -eq "worksheets") {
$SourceWorksheets = Get-SFWorksheets -AuthContext $SourceContext
if ($OutPath){
$SourceWorksheets | ForEach-Object {$_.SaveToFolder("$OutPath/worksheets")}
if ($CleanFiles) {
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $SFRole -SFWarehouse $SFWarehouse -WorksheetsPath "$OutPath/worksheets" -SFDatabase $SFDatabase -SFSchema $SFSchema
}
}
}
}
else {
Write-Host "$obj not a valid object. Use 'Filters, Dashboards, Worksheets, a combination of the three in a comma seperated list, or All.'`r`n"
}
}
if ($TargetAccounts) {
# Begin the upload to target account process
$TargetAccounts -Split ',' | ForEach-Object {
$TargetAccountLocator = $_
Write-Host -ForegroundColor Cyan "`r`nBegin upload to Account: $TargetAccountLocator"
$TargetPath = "$OutPath/$TargetAccountLocator"
if ($InteractiveMode) {
# Prompt for replacement values and SSO if InteractiveMode
Write-Host -ForegroundColor Green "Account Context for $($TargetAccountLocator):"
Write-Output ($SourceReplacementValues | Out-String)
$yn_str = "Would you like to change any of the context values (Role, Warehouse, Database, or Schema) for the objects to be uploaded to account $TargetAccountLocator?"
if (confirmPrompt $yn_str) {
$TargetReplacementValues = ChangeValues($SourceReplacementValues)
}
else {$TargetReplacementValues = $SourceReplacementValues}
if (SSOPrompt($TargetAccountLocator)){
$TargetContext = Connect-SFApp -Account $TargetAccountLocator -SSO
}
else {
$TargetContext = Connect-SFApp -Account $TargetAccountLocator
}
}
else {
$TargetReplacementValues = $SourceReplacementValues
if ($SSO){
$TargetContext = Connect-SFApp -Account $TargetAccountLocator -SSO
}
else {
$TargetContext = Connect-SFApp -Account $TargetAccountLocator
}
}
# Parse through SFObjects for upload
$SFObjectTypes -Split ',' | ForEach-Object {
$obj = $_.Trim().ToLower()
if ($obj -eq "all") {
foreach ($f in $SourceFilters){
Update-Filter-Object($f, $TargetReplacementValues)
New-SFFilter -AuthContext $TargetContext -Filter $f -ActionIfExists $ActionIfExistsFilter
}
foreach ($f in $SourceDashboards){
Update-Dashboard-Object($f, $TargetReplacementValues)
New-SFDashboard -AuthContext $TargetContext -Dashboard $f -ActionIfExists $ActionIfExistsDashboard
}
foreach ($f in $SourceWorksheets){
Update-Worksheet-Object($f, $TargetReplacementValues)
New-SFWorksheet -AuthContext $TargetContext -Worksheet $f -ActionIfExists $ActionIfExistsWorksheet
}
}
elseif ($obj -eq "filters") {
foreach ($f in $SourceFilters){
Update-Filter-Object($f, $TargetReplacementValues)
New-SFFilter -AuthContext $TargetContext -Filter $f -ActionIfExists $ActionIfExistsFilter
}
}
elseif ($obj -eq "dashboards") {
foreach ($f in $SourceDashboards){
Update-Dashboard-Object($f, $TargetReplacementValues)
New-SFDashboard -AuthContext $TargetContext -Dashboard $f -ActionIfExists $ActionIfExistsDashboard
}
}
elseif ($obj -eq "worksheets") {
foreach ($f in $SourceWorksheets){
Update-Dashboard-Object($f, $TargetReplacementValues)
New-SFWorksheet -AuthContext $TargetContext -Worksheet $f -ActionIfExists $ActionIfExistsWorksheet
}
}
else {
Write-Host "$obj not a valid object. Use 'Filters, Dashboards, Worksheets, a combination of the three in a comma seperated list, or All.'`r`n"
}
}
}
}
# No target account provided
else { Write-Host "`r`nNo target accounts supplied, ending program run successfully." -ForegroundColor Cyan }
}
# Begin Filepath Logic
else {
Write-Host "`r`nFound matching file path, reading files from directory at $($SourceAccountLocatorOrFilepath)/`r`n" -ForegroundColor Red
if ($OutputDirectory) {
$OutPath = $OutputDirectory
}
else {
$OutPath = $SourceAccountLocatorOrFilepath
}
if ($TargetAccounts){
$TargetAccounts -Split ',' | ForEach-Object {
$TargetAccountLocator = $_
Write-Host "Begin upload to Account: $TargetAccountLocator" -ForegroundColor Cyan
$TargetPath = "$OutPath/$TargetAccountLocator"
if ($InteractiveMode) {
# Prompt for replacement values and SSO if InteractiveMode
Write-Host -ForegroundColor Green "Account Context for $($TargetAccountLocator):"
Write-Output ($SourceReplacementValues | Out-String)
$yn_str = "Would you like to change any of the context values (Role, Warehouse, Database, or Schema) for the objects to be uploaded to account $($TargetAccountLocator)?"
if (confirmPrompt $yn_str) {
$TargetReplacementValues = ChangeValues($SourceReplacementValues)
}
else {$TargetReplacementValues = $SourceReplacementValues}
if (SSOPrompt($TargetAccountLocator)){
$TargetContext = Connect-SFApp -Account $TargetAccountLocator -SSO
}
else {
$TargetContext = Connect-SFApp -Account $TargetAccountLocator
}
}
else {
$TargetReplacementValues = $SourceReplacementValues
if ($SSO){
$TargetContext = Connect-SFApp -Account $TargetAccountLocator -SSO
}
else {
$TargetContext = Connect-SFApp -Account $TargetAccountLocator
}
}
$SFObjectTypes -Split ',' | ForEach-Object {
$obj = $_.Trim().ToLower()
if ($obj -eq "all") {
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $TargetReplacementValues.Role -SFWarehouse $TargetReplacementValues.Warehouse -WorksheetsPath "$SourceAccountLocatorOrFilepath/worksheets" -DashboardsPath "$SourceAccountLocatorOrFilepath/dashboards" -FiltersPath "$SourceAccountLocatorOrFilepath/filters" -SFDatabase $TargetReplacementValues.Database -SFSchema $TargetReplacementValues.Schema -OutputDirectory $TargetPath
}
$TargetFilters = Get-ChildItem "$TargetPath/filters" -recurse -exclude '*.daterange.*','*.datebucket.*','*.timezone.*'
$TargetDashboards = Get-ChildItem "$TargetPath/dashboards"
$TargetWorksheets = Get-ChildItem "$TargetPath/worksheets"
foreach ($f in $TargetFilters){
New-SFFilter -AuthContext $TargetContext -FilterFile $f.FullName -ActionIfExists $ActionIfExistsFilter
}
foreach ($f in $TargetDashboards){
New-SFDashboard -AuthContext $TargetContext -DashboardFile $f.FullName -ActionIfExists $ActionIfExistsDashboard
}
foreach ($f in $TargetWorksheets){
New-SFWorksheet -AuthContext $TargetContext -WorksheetFile $f.FullName -ActionIfExists $ActionIfExistsWorksheet
}
}
elseif ($obj -eq "filters") {
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $TargetReplacementValues.Role -SFWarehouse $TargetReplacementValues.Warehouse -FiltersPath "$SourceAccountLocatorOrFilepath/filters" -SFDatabase $TargetReplacementValues.Database -SFSchema $TargetReplacementValues.Schema -OutputDirectory $TargetPath
}
$TargetFilters = Get-ChildItem "$TargetPath/filters" -recurse -exclude '*.daterange.*','*.datebucket.*','*.timezone.*'
foreach ($f in $TargetFilters){
New-SFFilter -AuthContext $TargetContext -FilterFile $f.FullName -ActionIfExists $ActionIfExistsFilter
}
}
elseif ($obj -eq "dashboards") {
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $TargetReplacementValues.Role -SFWarehouse $TargetReplacementValues.Warehouse -DashboardsPath "$SourceAccountLocatorOrFilepath/dashboards" -SFDatabase $TargetReplacementValues.Database -SFSchema $TargetReplacementValues.Schema -OutputDirectory $TargetPath
}
$TargetDashboards = Get-ChildItem "$TargetPath/dashboards"
foreach ($f in $TargetDashboards){
New-SFDashboard -AuthContext $TargetContext -DashboardFile $f.FullName -ActionIfExists $ActionIfExistsDashboard
}
}
elseif ($obj -eq "worksheets") {
Invoke-Command -ScriptBlock { Update-SFDocuments -SFObjectTypes $SFObjectTypes -SFRole $TargetReplacementValues.Role -SFWarehouse $TargetReplacementValues.Warehouse -WorksheetsPath "$SourceAccountLocatorOrFilepath/worksheets" -SFDatabase $TargetReplacementValues.Database -SFSchema $TargetReplacementValues.Schema -OutputDirectory $TargetPath
}
$TargetWorksheets = Get-ChildItem "$TargetPath/worksheets"
foreach ($f in $TargetWorksheets){
New-SFWorksheet -AuthContext $TargetContext -WorksheetFile $f.FullName -ActionIfExists $ActionIfExistsWorksheet
}
}
else {
Write-Host "$obj not a valid object. Use 'Filters, Dashboards, Worksheets, a combination of the three in a comma seperated list, or All.'`r`n"
}
# If no output directory was provided, delete the target account dir
if (-Not($OutputDirectory)) {
Remove-Item $TargetPath -Recurse
}
}
}
# No TargetAccounts Supplied
}
else { Write-Host "`r`nNo target accounts supplied, ending program run successfully." -ForegroundColor Cyan }
}
<#
.SYNOPSIS
Copy-SFObjects is a wrapper module which invokes several other modules in sequence including:
Import-Dashboards
Update-SFDocuments.ps1
taks a single account locator or filepath to existing files pulled from an account and uploads the specified objects to one or many accounts.
You can also run it without any target accounts to simply pull down files from the source account.
.DESCRIPTION
Use this function for taking snowsight objects between accounts in a one line command.
.PARAMETER SFObjectTypes
Required [string]: SFObjectTypes specifies the object type being updated. Filters, Dashboards, Worksheets, or or a combination of the three (do not use all with the others or duplicates will be created) can be entered.
Choose All to update Filters, Dashboards, and Worksheets at the same time. Casing, ordering, and spaces do not matter for this argument do not matter. 'filter,dashboard,worksheet' = 'Worksheet, Dashboard, Filter'."
.Parameter SourceAccountLocatorOrFilepath
Required [string]: Contains account locator or file path of the source objects you want to transfer to target accounts. If supplied with a value that is not a file path, this will evaluate as an account locator and attempt to connect. See the -Connect-SFApp method for more details on how connections will be made.
When supplied with a value that is a valid path on your filesystem, this paremeter will evaluate this path and use it to read in files from the following subdirectories:
/filters, /dashboards, /worksheets.
.Parameter TargetAccounts
Optional [string]: A comma separated string with the account locators you wish to upload snowflake objects from SourceAccountLocatorOrFilepath to.
Ex: 'xy12345.us-east-1,MyGCPAcct.us-central1,xy12345.west-us-2.azure,MyPrivateLinkAcct.privatelink.snowflakecomputing.com'
.PARAMETER SFRole
Optional [string]: The name of the Snowflake Role required to run the specific object in target accounts or cleaned source files. Default is ACCOUNTADMIN. This value can be changed within the program with InteractiveMode set to true.
.PARAMETER SFWarehouse
Optional [string]: The name of the Snowflake virtual warehouse required to run the specific object in target accounts or cleaned source files for context. Default is COMPUTE_WH. This value can be changed within the program with InteractiveMode set to true.
.PARAMETER SFDatabase
Optional [string]: The name of the database to use in target accounts or cleaned source files for context. This value can be changed within the program with InteractiveMode set to true.
.PARAMETER SFSchema
Optional [string]: The name of the schema to use in target accounts or cleaned source files for for context. This value can be changed within the program with InteractiveMode set to true.
.PARAMETER OutputDirectory
Optional [string]: A parent level dir determining where source and target accounts should create sub dirs. Subdirs will be whichever objects are provided in ObjectTypes (worksheets/,dashboards/,filters/)
.PARAMETER ActionIfExistsDashboard
Optional [bool] {CreateNew, Skip}: The desired behavior when a Dashboard with the same name is found. Default is Skip.
.PARAMETER ActionIfExistsFilter
Optional [bool] {Overwrite, Skip}: The desired behavior when a Filter with the same name is found. Default is Skip.
.PARAMETER ActionIfExistsWorksheet
Optional [bool] {Overwrite, CreateNew, Skip}: The desired behavior when a Worksheet with the same name is found. Default is Skip.
.PARAMETER CleanFiles
Optional [bool]: Determines whether files are 'cleaned' of identifiable information including account locator, urls, role, warehouse, database, and schema. Default is false.
.PARAMETER SSO
Optional [bool]: Required when not in InteractiveMode, else the user will be prompted on an account by account basis for target accounts. default is False.
.PARAMETER InteractiveMode
Optional [bool]: InteractiveMode specifies that the user can provide inputs during runtime for values such as the context values and SSOs.
.INPUTS
The program will take input. You cannot pipe objects to Copy-Objects.
.OUTPUTS
Directories with files for the relevant SFObjectTypes in the OutputDirectory or PWD
.EXAMPLE
---------------- All (AWS Account Locator) ----------------
Copy-Objects -SFObjectTypes 'all' -SourceAccountLocatorOrFilepath 'MyAWSSourceAcct123.us-east-1' -TargetAccounts 'xy12345.us-east-1,MyGCPAcct.us-central1,xy12345.west-us-2.azure,MyAWSPrivateLinkAcct.us-east-1.privatelink.snowflakecomputing.com' -SFRole TEST_ROLE -SFWarehouse TEST_WH -SFDatabase TEST_DB -SFSchema TEST_SCHEMA -OutputDirectory 'path/to/my/output/dir'
.EXAMPLE
---------------- Filters (Filepath) ----------------
Copy-Objects -SFObjectTypes 'filters' -TargetAccounts 'xy12345.us-east-2.aws,MyGCPAcct.us-central1,xy12345.west-us-2.azure,MyAWSPrivateLinkAcct.us-east-1.privatelink.snowflakecomputing.com' -SFRole TEST_ROLE -SFWarehouse TEST_WH -SFDatabase TEST_DB -SFSchema TEST_SCHEMA -OutputDirectory 'path/to/my/output/dir'
.EXAMPLE
---------------- Dashboard (Azure & Privatelink AccountLocator) ----------------
Copy-Objects -SFObjectTypes 'dashboards' -SourceAccountLocatorOrFilepath 'MyAzureSourceAcct123.MyAWSPrivateLinkAcct.west-us-2.azure.privatelink.snowflakecomputing.com' -TargetAccounts 'xy12345.us-east-1,MyGCPAcct.us-central1.gcp,xy12345.west-us-2.azure,MyAWSPrivateLinkAcct.us-east-1.privatelink.snowflakecomputing.com' -SFRole TEST_ROLE -SFWarehouse TEST_WH -SFDatabase TEST_DB -SFSchema TEST_SCHEMA -OutputDirectory 'path/to/my/output/dir'
.EXAMPLE
---------------- Worksheet (Filepath) ----------------
Copy-Objects -SFObjectTypes 'worksheets' -SourceAccountLocatorOrFilepath 'path/to/my/acct/object/dir' -TargetAccounts 'xy12345.us-east-1,MyGCPAcct.us-central1.gcp,xy12345.west-us-2.azure,MyAWSPrivateLinkAcct.us-east-1.privatelink.snowflakecomputing.com' -SFRole TEST_ROLE -SFRole TEST_ROLE -SFWarehouse TEST_WH -SFDatabase TEST_DB -SFSchema TEST_SCHEMA -OutputDirectory 'path/to/my/output/dir'
.EXAMPLE
---------------- Only Pull Down and Update Files, No TargetAccount Argument ----------------
Copy-Objects -SFObjectTypes 'filters, dashboards, worksheets' -SourceAccountLocatorOrFilepath 'path/to/my/acct/object/dir' -SFRole TEST_ROLE -SFWarehouse TEST_WH -SFDatabase TEST_DB -SFSchema TEST_SCHEMA
.EXAMPLE
---------------- Mix of assets, 'all', Filepath ----------------
Copy-Objects -SFObjectTypes 'filters, dashboards, worksheets' -SourceAccountLocatorOrFilepath 'path/to/my/acct/object/dir' -TargetAccounts 'xy12345.us-east-1.gcp,MyGCPAcct.us-central1,xy12345.west-us-2.azure,MyAWSPrivateLinkAcct.us-east-1.privatelink.snowflakecomputing.com' -SFRole TEST_ROLE -SFWarehouse TEST_WH -SFDatabase TEST_DB -SFSchema TEST_SCHEMA
#>
}
function Update-Filter-Object ($fparam, $values)
{
$fparam.update | ForEach-Object { #Manual Filter Update
if($fparam.Type -eq 'manual'){
$fparam.Role = $values.Role
$fparam.Warehouse = $values.Warehouse
$fparam.Configuration.context.role = $values.Role
$fparam.Configuration.context.warehouse = $values.Warehouse
$fparam.Database = $values.Database
$fparam.Schema = $values.Schema
$fparam.Configuration.context.database = $values.Database
$fparam.Configuration.context.schema = $values.Schema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
}
#Query Filter Update
elseif($fparam.Type -eq 'query') {
$fparam.Worksheet.OwnerUserID = ""
$fparam.Worksheet.OwnerUserName = ""
$fparam.Worksheet.Role = $values.Role
$fparam.Worksheet.Warehouse = $values.Warehouse
# $fparam.Worksheet.FileSystemSafeName = ""
$fparam.Worksheet.AccountName = ""
$fparam.Worksheet.AccountFullName = ""
$fparam.Worksheet.AccountUrl = ""
$fparam.Worksheet.OrganizationID = ""
$fparam.Worksheet.Region = ""
$fparam.Worksheet.Database = $values.Database
$fparam.Worksheet.Schema = $values.Schema
$fparam.Role = $values.Role
$fparam.Warehouse = $values.Warehouse
$fparam.Configuration.context.role = $values.Role
$fparam.Configuration.context.warehouse = $values.Warehouse
$fparam.Database = $values.Database
$fparam.Schema = $values.Schema
$fparam.Configuration.context.database = $values.Database
$fparam.Configuration.context.schema = $values.Schema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
}
}
}
function Update-Dashboard-Object ($fparam, $values)
{
$fparam.update | ForEach-Object {
$fparam.OwnerUserID = ""
$fparam.OwnerUserName = ""
$fparam.Role = $values.Role
$fparam.Warehouse = $values.Warehouse
$fparam.Database = $values.Database
$fparam.Schema = $values.Schema
foreach ($worksheet in $fparam.Worksheets) {
$worksheet.OwnerUserID = ""
$worksheet.OwnerUserName = ""
$worksheet.Role = $values.Role
$worksheet.Warehouse = $values.Warehouse
$worksheet.Database = $values.Database
$worksheet.Schema = $values.Schema
$worksheet.FileSystemSafeName = ""
$worksheet.AccountName = ""
$worksheet.AccountFullName = ""
$worksheet.AccountUrl = ""
$worksheet.OrganizationID = ""
$worksheet.Region = ""
}
$fparam.Database = $values.Database
$fparam.Schema = $values.Schema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
$fparam.Contents.context.role = $values.Role
$fparam.Contents.context.warehouse = $values.Warehouse
$fparam.Contents.context.database = $values.Database
$fparam.Contents.context.schema = $values.Schema
}
}
function Update-Worksheet-Object ($fparam, $values)
{
$fparam.update | ForEach-Object {
$fparam.OwnerUserID = ""
$fparam.OwnerUserName = ""
$fparam.Role = $values.Role
$fparam.Warehouse = $values.Warehouse
$fparam.Database = $values.Database
$fparam.Schema = $values.Schema
$fparam.FileSystemSafeName = ""
$fparam.AccountName = ""
$fparam.AccountFullName = ""
$fparam.AccountUrl = ""
$fparam.OrganizationID = ""
$fparam.Region = ""
}
}
function SSOPrompt([string]$inputString){
return confirmPrompt "Does the account at $inputString use SSO?"
}
# function RenameTargetObjectsPrompt($values, $account){
# $acctString = "Account Context: {0}" -f $account | Out-String
# Write-Host -ForegroundColor Red $acctString
# Write-Output ($values | Out-String)
# $yn_str = "Would you like to change any of the context values (Role, Warehouse, Database, or Schema) for the objects to be uploaded to account {0}?" -f $account
# if (confirmPrompt $yn_str) {
# $ret = ChangeValues($values)
# return $ret
# }
# else {return $values}
# }
function take_input() {
param
(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[string]$msg,
[string]$ForegroundColor = "Cyan"
)
Write-Host -ForegroundColor $ForegroundColor -NoNewline $msg;
return Read-Host
}
function confirmPrompt([string]$inputString){
do {
$UserInput = take_input "`r`n$($inputString) Use y/n: "
} while (
'y','yes','n','no' -notcontains $UserInput
)
if ('y','yes' -contains $UserInput){
return $true
}
else{
return $false
}
}
function ChangeValues($values){
$OutHash = @{}
foreach ($h in $values.GetEnumerator()) {
$k = $h.Name
$v = $h.Value
$inString = "Please provide a value for {0}. Current value is {1}, hit enter to use this value: " -f $k, $v
$UserInput = take_input $inString
if ($UserInput) {
$OutHash.$k = $UserInput
}
else {$OutHash.$k = $v
}
}
return $OutHash
}