Skip to content

Instantly share code, notes, and snippets.

@Hugoberry
Last active March 20, 2017 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hugoberry/7d012a4603b2955a308d19e752cf6a44 to your computer and use it in GitHub Desktop.
Save Hugoberry/7d012a4603b2955a308d19e752cf6a44 to your computer and use it in GitHub Desktop.
Extracting Power Query queries
#Finding the portnumber on which the $Embedded$ tabular model is running on
$embedded = "$env:LOCALAPPDATA\Microsoft\Power BI Desktop\AnalysisServicesWorkspaces"
$ports = Get-ChildItem $embedded -rec | where {$_.Name -eq "msmdsrv.port.txt"}
$port = Get-Content $ports.FullName -Encoding Unicode
#Getting the data sources from the $Embedded$ tabular model
[xml] $db = Invoke-ASCmd -Server:localhost:$port -Query:"SELECT * from `$SYSTEM.TMSCHEMA_DATA_SOURCES"
$db.return.root.row
$cs = $db.return.root.row.ConnectionString
#Cleaning up the connection string
$b64 = $cs.Split(";")[-1].Trim("Mashup=").Trim("""")
$bytes = [Convert]::FromBase64String($b64)
$temp = "c:\temp"
#Output to a binary ZIP file
[IO.File]::WriteAllBytes("$temp\a.zip", $bytes)
#Unzip
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::ExtractToDirectory("$temp\a.zip","$temp\a")
#TADA!
($m = Get-Content $temp\a\Formulas\Section1.m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment