PBIXtoPBIP_PBITConversion.psm1

<#
     .SYNOPSIS
         This command would convert the PBIX file format into either PBIP or PBIT based on the run time parameter passed.
    
     
    
     .DESCRIPTION
         This command would convert the PBIX file format into either PBIP or PBIT based on the run time parameter passed.
    
     
    
     .PARAMETER PBIXFilePath
     Accepts the Path where the PBIX file is present.
    
     
    
     .PARAMETER ConversionFileType
     The file type the PBIX file needs to be converted to (Either PBIP or PBIT).
    
     
    
     .EXAMPLE
     PBIXtoPBIP_PBITConversion -PBIXFilePath "<<PBIXFilePath>>" -ConversionFileType "<<ConversionFileType>>"
      
#>

Function PBIXtoPBIP_PBITConversion
{

 

    Param 
    (

    [Parameter(Mandatory=$true)]
    [string] $PBIXFilePath,
    [Parameter(Mandatory=$true)]
    [string] $ConversionFileType

    )

#Validate whether the conversion file type is either PBIP or PBIT
$ConversionFileTypeUpper =$ConversionFileType.ToUpper()
if (($ConversionFileTypeUpper -eq "PBIP") -or ($ConversionFileTypeUpper -eq "PBIT"))
{
}
else
{
Write-Host "Incorrect parameter value passed. ConversionFileType should only have value as either PBIP or PBIT"
}
 
#Function to get the Main Window Process Name
function Get-ProcessMainWindowHandle {
    param (
        [Parameter(Mandatory=$true)]
        [string]$ProcessName
    )
    $process = Get-Process -Name $ProcessName -ErrorAction SilentlyContinue
    if ($process) {
        return $process.MainWindowHandle
    } else {
        Write-Host "Process $ProcessName not found."
        return $null
    }
}
 
 
function Wait-LetPBIDesktopCPUUtiliztaionStabilize {
$CPU=-1
#Wait till the time the Power BI report is open and the previous iteration of CPU utilization of report is same as the current one
while ( $CPU -ne (Get-Process | Where-Object{$_.ProcessName -eq "PBIDesktop"}).CPU)
{
$CPU = (Get-Process | Where-Object{$_.ProcessName -eq "PBIDesktop"}).CPU
Write-host "Report is still Opening........" $CPU "CPU(S)"
Start-Sleep -Seconds 15
}
}
 
function CloseSignInWindow {
Write-Host "Terminating Sign in Window"
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -Seconds 30
 
}

Start-Process $PBIXFilePath #Open the PBIX file / Power BI report
Start-Sleep -Seconds 30
 
Wait-LetPBIDesktopCPUUtiliztaionStabilize # Let the PBIDesktop CPU Utilization Stabilize
#Close the window in case if sign in/authentication window pops up
$process = Get-Process | Where-Object{$_.ProcessName -eq "AccountsControlHost"}
if ($process -ne $null)
{
Write-Host "Terminating AccountsControlHost Window"
Stop-Process -Name "AccountsControlHost"
}
else
{
}
 
Wait-LetPBIDesktopCPUUtiliztaionStabilize # Let the PBIDesktop CPU Utilization Stabilize
$process = Get-Process | Where-Object{$_.ProcessName -eq "PBIDesktop"}
if ($process.cpu -lt 15)
{
CloseSignInWindow #Close the sign In window
}
else
{
}
 
#Close the window in case if sign in/authentication window pops up
$process = Get-Process | Where-Object{$_.ProcessName -eq "Microsoft.AAD.BrokerPlugin"}
if ($process -ne $null)
{
Write-Host "Terminating AAD Authentication Window"
Stop-Process -Name "Microsoft.AAD.BrokerPlugin"
}
else
{
}
Wait-LetPBIDesktopCPUUtiliztaionStabilize # Let the PBIDesktop CPU Utilization Stabilize
 
if ("SetProcess" -as [type]) {} else {
# Function to set the foreground window using user32.dll
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class SetProcess {
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
    }
"@

}
# The name of the process you want to switch to (without .exe)
$processName = "PBIDesktop"
# Get the main window handle of the desired process
$mainWindowHandle = Get-ProcessMainWindowHandle -ProcessName $processName
if ($mainWindowHandle) {
    # Switch to the application window
    [SetProcess]::SetForegroundWindow($mainWindowHandle)
} else {
    Write-Host "Unable to find or switch to the window of $processName."
}

 
CloseSignInWindow <# The SIgn in window is appearing at random basis and there is no way to
identify the process. Hence this function would either terminate the window in case if it is present or would minimze the
filter tab in report ensuring report is not impacted to overcome the limitation that we are unable to idenify the process
#>

 
write-host "Starting with conversion.."
Start-Sleep -Seconds 5
[System.Windows.Forms.SendKeys]::SendWait('%')
Start-Sleep -Seconds 5
[System.Windows.Forms.SendKeys]::SendWait('F')
Start-Sleep -Seconds 1
#Click File on Power BI desktop
[System.Windows.Forms.SendKeys]::SendWait('{ENTER}')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
#Click Save AS
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
if ($ConversionFileTypeUpper -eq "PBIP")
{
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
}
elseif ($ConversionFileTypeUpper -eq "PBIT")
{
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait('{DOWN}')
}
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -Seconds 1
if ($ConversionFileTypeUpper -eq "PBIT")
{
[System.Windows.Forms.SendKeys]::SendWait("{TAB}")
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
}
else
{
}
Start-Sleep -Seconds 120
Write-Host "PBIX to " $ConversionFileType " Conversion completed"
Write-Host "Closing the Report"
Stop-Process -Name "PBIDesktop" # Close the Power BI Report


} 

Export-ModuleMember -Function PBIXtoPBIP_PBITConversion