最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

powershell - Script to call the MS Fabric -Git update From Git API - Stack Overflow

programmeradmin1浏览0评论

I have below script for updating the MS Fabric workspace in ADO release pipeline. This is not working. Also the authentication is done on Service Principal for the token.

$accessToken = "$(accessToken)"  # Use the token from pipeline variables

$headers = @{
    Authorization = "Bearer $accessToken"
    "Content-Type" = "application/json"
}

$apiUrl = "=<workspace-id>&itemId=<item-id>"
$body = @{
    gitBranch = "feature"
} | ConvertTo-Json -Depth 10

$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body

Write-Output $response

Error:

2025-03-05T17:37:34.2927854Z Invoke-RestMethod : 
2025-03-05T17:37:34.2928743Z Not Found
2025-03-05T17:37:34.2930059Z Not Found
2025-03-05T17:37:34.2931039Z HTTP Error 404. The requested resource is not found.
2025-03-05T17:37:34.2932857Z At D:\a\_temp\.ps1:16 char:13
2025-03-05T17:37:34.2933909Z + $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $hea ...
2025-03-05T17:37:34.2934961Z +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2025-03-05T17:37:34.2936330Z     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
2025-03-05T17:37:34.2937301Z    eption

I have below script for updating the MS Fabric workspace in ADO release pipeline. This is not working. Also the authentication is done on Service Principal for the token.

$accessToken = "$(accessToken)"  # Use the token from pipeline variables

$headers = @{
    Authorization = "Bearer $accessToken"
    "Content-Type" = "application/json"
}

$apiUrl = "https://api.fabric.microsoft/core/git/update-from-git?workspaceId=<workspace-id>&itemId=<item-id>"
$body = @{
    gitBranch = "feature"
} | ConvertTo-Json -Depth 10

$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $headers -Body $body

Write-Output $response

Error:

2025-03-05T17:37:34.2927854Z Invoke-RestMethod : 
2025-03-05T17:37:34.2928743Z Not Found
2025-03-05T17:37:34.2930059Z Not Found
2025-03-05T17:37:34.2931039Z HTTP Error 404. The requested resource is not found.
2025-03-05T17:37:34.2932857Z At D:\a\_temp\.ps1:16 char:13
2025-03-05T17:37:34.2933909Z + $response = Invoke-RestMethod -Uri $apiUrl -Method Post -Headers $hea ...
2025-03-05T17:37:34.2934961Z +             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2025-03-05T17:37:34.2936330Z     + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc 
2025-03-05T17:37:34.2937301Z    eption
Share asked Mar 5 at 17:41 SalmanSalman 1,9556 gold badges15 silver badges28 bronze badges 1
  • Judging from the Fabric API documentation it looks like you want to use the URI format https://api.fabric.microsoft/v1/workspaces/<workspace-id>/git/updateFromGit instead and pass a commit hash rather than a branch name – Mathias R. Jessen Commented Mar 5 at 18:22
Add a comment  | 

1 Answer 1

Reset to default 0

I could reproduce the 404 error in local PowerShell prompt with your script, which didn't seem to call a valid API URL.

You may seek to call this API instead to Update From Git. However, please note that it doesn't support identities of service principal or managed identities.

According to the document on Identity Support,

Each API's reference page lists whether the API supports service principals and managed identities. When using APIs, consider whether the API call relies on other APIs or items that don't support the calling identity. In such cases, your call will fail.

I think this is also why my call to List Workspaces succeeded, while the call to Get Git Status failed to get the git head to be updated from. I also tested to call that Get Git Status api authenticating with my user's bearer token and it worked.

Adding my script to help you understand my tests.

# Generate AAD token for service principal to access Microsoft Fabric
$scope = "https://api.fabric.microsoft/.default"
$grantType = "client_credentials"

$body = @{
    client_id = $clientId
    scope = $scope
    client_secret = $clientSecret
    grant_type = $grantType
}

$response = Invoke-WebRequest -Uri "https://login.microsoftonline/$tenantId/oauth2/v2.0/token" -Method POST -ContentType "application/x-www-form-urlencoded" -Body $body | ConvertFrom-json
$accessToken = $response.access_token 

$headers = @{
    Authorization = "Bearer $accessToken"
    "Content-Type" = "application/json"
}

# Succeeded to list workspaces
$workspacesUrl = "https://api.fabric.microsoft/v1/workspaces"
$workspaces = Invoke-RestMethod -Uri $workspacesUrl -Method Get -Headers $headers
$devWorkspaceId = ($workspaces.value | Where-Object { $_.displayName -eq "Dev" }).id

# Failed with service principal but worked when authenticating against the bearer token of my user account
$gitStatusUrl = "https://api.fabric.microsoft/v1/workspaces/$devWorkspaceId/git/status"
$gitStatus= Invoke-RestMethod -Uri $gitStatusUrl -Method Get -Headers $headers
$gitStatus

We are not sure whether you found that API https://api.fabric.microsoft/core/git/update-from-git?workspaceId=<workspace-id>&itemId=<item-id> from Microsoft Fabric REST API references - Microsoft Fabric REST APIs | Microsoft Learn. If yes, please edit your original post and share the API link. Furthermore, let us know if the script works outside pipelines to help narrow down the root cause.

Azure Pipelines are simply an automation tool. You need to make sure your script works locally before integrating it in pipelines. Hope the information clarifies the actual cause of the issue.

发布评论

评论列表(0)

  1. 暂无评论