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

    )

$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"
}

Start-Process $PBIXFilePath #Open the PBIX file / Power BI report
Start-Sleep -Seconds 30

$CPU = -1 #Declaring a variable to capture the previou instance of CPU utilization

#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........"
Start-Sleep -Seconds 15

}
#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 Authentication Window"
Stop-Process -Name "Microsoft.AAD.BrokerPlugin"
}
else
{
}
write-host "Starting with conversion.."
Start-Sleep -Seconds 5
[System.Windows.Forms.SendKeys]::SendWait('%')
Start-Sleep -Seconds 1
[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 30
Stop-Process -Name "PBIDesktop" # Close the Power BI Report



} 

Export-ModuleMember -Function PBIXtoPBIP_PBITConversion