Skip to content

Commit 7cdeff3

Browse files
authored
Create CreateFolder.ps1
1 parent a8a2b85 commit 7cdeff3

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

CreateFolder.ps1

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
param(
2+
[string]$TenantId = "<YourTenantID>",
3+
[string]$ClientId = "<ServicePrincipalID>",
4+
[string]$ClientSecret = "<ServicePrincipalSecret>",
5+
[string]$WorkspaceId = "<YourWorkspaceID>",
6+
[string]$FolderPath = "TestFolder/TestFolder/a" # from manifest
7+
)
8+
9+
# ===== Helper: Get Access Token =====
10+
function Get-AccessToken {
11+
param($TenantId, $ClientId, $ClientSecret)
12+
13+
$body = @{
14+
grant_type = "client_credentials"
15+
client_id = $ClientId
16+
client_secret = $ClientSecret
17+
scope = "https://api.fabric.microsoft.com/.default"
18+
}
19+
20+
$response = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" -Body $body
21+
return $response.access_token
22+
}
23+
24+
# ===== Helper: Get Folder by Name =====
25+
function Get-FolderByName {
26+
param($WorkspaceId, $ParentFolderId, $Name, $Headers)
27+
28+
$uri = "https://api.fabric.microsoft.com/core/folders/v1/workspaces/$WorkspaceId/folders"
29+
if ($ParentFolderId -ne "") {
30+
$uri += "?parentFolderId=$ParentFolderId"
31+
}
32+
33+
$folders = Invoke-RestMethod -Method Get -Uri $uri -Headers $Headers
34+
return $folders.value | Where-Object { $_.name -eq $Name }
35+
}
36+
37+
# ===== Helper: Create Folder =====
38+
function New-Folder {
39+
param($WorkspaceId, $Name, $ParentFolderId, $Headers)
40+
41+
$body = @{ name = $Name }
42+
if ($ParentFolderId -ne "") { $body["parentFolderId"] = $ParentFolderId }
43+
44+
$bodyJson = $body | ConvertTo-Json -Depth 5
45+
$uri = "https://api.fabric.microsoft.com/core/folders/v1/workspaces/$WorkspaceId/folders"
46+
return Invoke-RestMethod -Method Post -Uri $uri -Headers $Headers -Body $bodyJson
47+
}
48+
49+
# ===== Main Script =====
50+
$token = Get-AccessToken -TenantId $TenantId -ClientId $ClientId -ClientSecret $ClientSecret
51+
$headers = @{ "Authorization" = "Bearer $token"; "Content-Type" = "application/json" }
52+
53+
$parts = $FolderPath -split "/"
54+
$parentId = ""
55+
$createdFolders = @()
56+
57+
foreach ($part in $parts) {
58+
$existing = Get-FolderByName -WorkspaceId $WorkspaceId -ParentFolderId $parentId -Name $part -Headers $headers
59+
60+
if ($null -eq $existing) {
61+
Write-Output "Creating folder: $part (under parent: $parentId)"
62+
$folder = New-Folder -WorkspaceId $WorkspaceId -Name $part -ParentFolderId $parentId -Headers $headers
63+
} else {
64+
Write-Output "Folder already exists: $part"
65+
$folder = $existing
66+
}
67+
68+
$parentId = $folder.id
69+
$createdFolders += $folder
70+
}
71+
72+
Write-Output "Final folder ID (deepest level): $parentId"

0 commit comments

Comments
 (0)