Created
December 16, 2022 22:58
-
-
Save Curts0/6761f647a5f1a753625979bfb0b62065 to your computer and use it in GitHub Desktop.
Get connection string for localhost pbi file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def get_msmdsrv() -> list: | |
msmdsrv = subprocess.check_output( | |
[ | |
"powershell", | |
"""Get-CimInstance -ClassName Win32_Process -Property * -Filter "Name = 'msmdsrv.exe'" | Select-Object -Property ProcessId -ExpandProperty ProcessId""", | |
] | |
) | |
msmdsrv_id = msmdsrv.decode().strip().splitlines() | |
return msmdsrv_id | |
def get_port_number(msmdsrv: str) -> str: | |
port = subprocess.check_output( | |
[ | |
"powershell", | |
f"""Get-NetTCPConnection -State Listen -OwningProcess {msmdsrv} | Select-Object -Property LocalPort -First 1 -ExpandProperty LocalPort""", | |
] | |
) | |
port_number = port.decode().strip() | |
return port_number | |
def get_parent_id(msmdsrv: str) -> str: | |
parent = subprocess.check_output( | |
[ | |
"powershell", | |
f"""Get-CimInstance -ClassName Win32_Process -Property * -Filter "ProcessId = {msmdsrv}" | Select-Object -Property ParentProcessId -ExpandProperty ParentProcessId""", | |
] | |
) | |
parent_id = parent.decode().strip() | |
return parent_id | |
def get_parent_title(parent_id: str) -> str: | |
pbi_title_suffixes: list = [ | |
" \u002D Power BI Desktop", # Dash Punctuation - minus hyphen | |
" \u2212 Power BI Desktop", # Math Symbol - minus sign | |
" \u2011 Power BI Desktop", # Dash Punctuation - non-breaking hyphen | |
" \u2013 Power BI Desktop", # Dash Punctuation - en dash | |
" \u2014 Power BI Desktop", # Dash Punctuation - em dash | |
" \u2015 Power BI Desktop", # Dash Punctuation - horizontal bar | |
] | |
title = subprocess.check_output( | |
["powershell", f"""(Get-Process -Id {parent_id}).MainWindowTitle"""] | |
) | |
title_name = title.decode().strip() | |
for suffix in pbi_title_suffixes: | |
title_name = title_name.replace(suffix, "") | |
return title_name | |
def create_connection_str(port_number: str) -> str: | |
connection_str = f"Data Source=localhost:{port_number}" | |
return connection_str | |
def find_local_pbi_instances() -> list: | |
"""The real genius is from this [Dax Studio PowerBIHelper](https://github.com/DaxStudio/DaxStudio/blob/master/src/DaxStudio.UI/Utils/PowerBIHelper.cs). | |
I just wanted it in python not C#, so reverse engineered what DaxStudio did. | |
It will run some powershell scripts to pull the appropriate info. | |
Then will spit out a list with tuples inside. | |
You can use the connection string to connect to your model with pytabular. | |
Returns: | |
list: Example - `[('PBI File Name1','localhost:{port}'),('PBI File Name2','localhost:{port}')]` | |
""" | |
instances = get_msmdsrv() | |
pbi_instances = [] | |
for instance in instances: | |
port = get_port_number(instance) | |
parent = get_parent_id(instance) | |
title = get_parent_title(parent) | |
connect_str = create_connection_str(port) | |
pbi_instances += [(title, connect_str)] | |
return pbi_instances |
from pbi_helper import find_local_pbi_instances
instances = find_local_pbi_instances()
print(instances)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The real genius is from this Dax Studio PowerBIHelper.
I just wanted it in python not C#, so reverse engineered what DaxStudio did.
It will run some powershell scripts to pull the appropriate info.
Then will spit out a list with tuples inside.
You can use the connection string to connect to your model with pytabular.