{"id":1695,"date":"2026-11-06T18:14:13","date_gmt":"2026-11-06T16:14:13","guid":{"rendered":"https:\/\/www.flip-design.de\/?p=1695"},"modified":"2026-11-06T18:14:13","modified_gmt":"2026-11-06T16:14:13","slug":"updatedatasources-updating-connections-in-a-semantic-model-using-powershell","status":"publish","type":"post","link":"https:\/\/www.flip-design.de\/?p=1695","title":{"rendered":"UpdateDatasources \u2013 Updating Connections in a Semantic Model Using PowerShell"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In my recent posts, I described how to build a continuous deployment (CD) pipeline step by step using PowerShell. Among other things, I demonstrated how to deploy projects and update parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.flip-design.de\/?p=1687\">https:\/\/www.flip-design.de\/?p=1687<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.flip-design.de\/?p=1588\">https:\/\/www.flip-design.de\/?p=1588<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.flip-design.de\/?p=1563\">https:\/\/www.flip-design.de\/?p=1563<\/a><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">When deploying to different environments, data source connections often need to be adjusted as well. For example, a report in the test environment should connect to the test database.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Native deployment pipelines support these adjustments through deployment rules. Alternatively, the data sources of a semantic model can be updated using a PowerShell script and a REST API call.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The screenshots below first show how to find a gateway connection that points to a previous server or a development server.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image.png\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"459\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image.png\" alt=\"\" class=\"wp-image-1696\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image.png 940w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image-300x146.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image-768x375.png 768w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After the script has been executed, the semantic model points to a different database that is also accessible through the gateway:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"940\" height=\"135\" src=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image-1.png\" alt=\"\" class=\"wp-image-1697\" srcset=\"https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image-1.png 940w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image-1-300x43.png 300w, https:\/\/www.flip-design.de\/wp-content\/uploads\/2026\/11\/image-1-766x110.png 766w\" sizes=\"auto, (max-width: 940px) 100vw, 940px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The script is fairly straightforward, so I\u2019m sharing it here:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Specify the current database to match and the replacement database to use.\nparam (\n    &#91;Parameter(Mandatory = $true)]\n    &#91;ValidateNotNullOrEmpty()]\n    &#91;string]$oldDatabase,\n\n    &#91;Parameter(Mandatory = $true)]\n    &#91;ValidateNotNullOrEmpty()]\n    &#91;string]$newDatabase\n)\n\n# Configure the service principal credentials used to authenticate with Power BI.\n$tenantId = \"dd31239f-1fba-49a4-8b6c-47df999883d6\"\n$clientId = \"b705d1ab-0a03-4c29-8c42-d6e5d9f5808f\"\n$clientSecret = ConvertTo-SecureString \"XXXXXXXXXX\" -AsPlainText -Force\n\n# Sign in using the service principal's client ID and client secret.\n$credential = &#91;System.Management.Automation.PSCredential]::new($clientId, $clientSecret)\nConnect-PowerBIServiceAccount -ServicePrincipal -Tenant $tenantId -Credential $credential -ErrorAction Stop | Out-Null\n\n# Identify the workspace and semantic model whose data source should be updated.\n$workspaceId = \"f2558832-a956-45b8-be5c-de8e884419ee\"\n$datasetId   = \"c920dfda-0516-40a5-8647-8a63ad660305\"\n\n# Keep the SQL Server unchanged while switching the database.\n$server      = \"G9PRM84\"\n\n# Build the request body: match the existing SQL connection and provide its replacement.\n$body = @{\n    updateDetails = @(\n        @{\n            datasourceSelector = @{\n                datasourceType = \"Sql\"\n                connectionDetails = @{\n                    server   = $server\n                    database = $oldDatabase\n                }\n            }\n            connectionDetails = @{\n                server   = $server\n                database = $newDatabase\n            }\n        }\n    )\n} | ConvertTo-Json -Depth 10\n\n# Transfer semantic model ownership to the signed-in service principal.\nInvoke-PowerBIRestMethod `\n    -Url \"groups\/$workspaceId\/datasets\/$datasetId\/Default.TakeOver\" `\n    -Method Post `\n    -ErrorAction Stop\n\n# Apply the new database connection details to the matching data source.\nInvoke-PowerBIRestMethod `\n    -Url \"groups\/$workspaceId\/datasets\/$datasetId\/Default.UpdateDatasources\" `\n    -Method Post `\n    -Body $body `\n    -ErrorAction Stop\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my recent posts, I described how to build a continuous deployment (CD) pipeline step by step using PowerShell. Among other things, I demonstrated how to deploy projects and update parameters: When deploying to different environments, data source connections often &hellip; <a href=\"https:\/\/www.flip-design.de\/?p=1695\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1695","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1695"}],"version-history":[{"count":1,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1695\/revisions"}],"predecessor-version":[{"id":1698,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=\/wp\/v2\/posts\/1695\/revisions\/1698"}],"wp:attachment":[{"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flip-design.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}