Hi all,
Is it possible to script a new connection? As in this action:
![daive_0-1738734677758.png daive_0-1738734677758.png]()
This is my script, its failing with a 404 Not Found
$tenantId = "<tenant-id>"
$clientId = "<client-id>"
$clientSecret = "<client-secret>"
$gatewayId = "<your-gateway-id>"
$resource = "https://analysis.windows.net/powerbi/api"
$loginUrl = "https://login.microsoftonline.com/$tenantId/oauth2/token"
# Get Access Token for Power BI API
$body = @{
grant_type = "client_credentials"
client_id = $clientId
client_secret = $clientSecret
resource = $resource
}
$tokenResponse = Invoke-RestMethod -Method Post -Uri $loginUrl -Body $body
$accessToken = $tokenResponse.access_token
# Define API Endpoint
$uri = "https://api.powerbi.com/v1.0/myorg/gateways/$gatewayId/datasources"
# Define Connection Details
$body = @{
"dataSourceType" = "Sql Server"
"connectionDetails" = @{
"server" = "your-sql-server-name"
"database" = "your-database-name"
}
"credentialDetails" = @{
"credentialType" = "ServicePrincipal" # Using Service Principal
"credentials" = @{
"applicationId" = "$clientId"
"tenantId" = "$tenantId"
"clientSecret" = "$clientSecret"
} | ConvertTo-Json -Compress
"encryptedConnection" = "Encrypted"
"privacyLevel" = "Organizational"
}
} | ConvertTo-Json -Depth 10
# API Headers
$headers = @{
Authorization = "Bearer $accessToken"
"Content-Type" = "application/json"
}
# Make API Call to Create the Data Source
Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body
Hopefully this is possible 🙂
Thanks,
David