dubravcik / pbixrefresher-python

Refresh Power BI reports programmatically for free
MIT License
125 stars 55 forks source link

How to set up the automatic running job #31

Open PaulWu-China opened 4 years ago

PaulWu-China commented 4 years ago

Hello, i have some PBIX files need to be refreshed every day, so i put them to the task scheduler of a remote server, but i can only run it successfully when i logon the server and keep the logon window, when i disconnect the server, it failed with the below error. would you please help? Thanks a lot.

pbixerror

dawiepoolman commented 4 years ago

I have found this .bat file script on another site that worked for me.. It runs as an admin user and closes the RDP session without killing the background process ID so that the screen stays visible for the GUI automation:

------------------------------- bat file start---------------------------------------------------

:: BatchGotAdmin :------------------------------------- REM --> Check for permissions IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (

nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system" ) ELSE ( nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" )

REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges... goto UACPrompt ) else ( goto gotAdmin )

:UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" set params= %* echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:gotAdmin pushd "%CD%" CD /D "%~dp0"

echo "Starting.." > RefreshPowerBIReports.log rem Close RDP session echo "Logging off RDP" > RefreshPowerBIReports.log C:\Shortcut_Disconnect_RDP_for_GUI_automation.lnk

rem call your pbixrefresher.py here echo "Refreshing PowerBIReport" >> RefreshPowerBIReports.log py pbixrefresher.py "C:\MyPowerBIReport.pbix" --no-publish --refresh-timeout 1200 --init-wait 45 >> RefreshPowerBIReports.log timeout 2 echo "----------------------" >> RefreshPowerBIReports.log

------------------------------- bat file end ---------------------------------------------------

Notice that the .bat file calls another bat file via a shortcut link I have added to the taskbar. You need to always close the RDP session manually using the shortcut as well instead of closing it with the cross.

The .bat file that closes the RDP session:

------------------------------- bat file start--------------------------------------------------- @echo off

:: BatchGotAdmin :------------------------------------- REM --> Check for permissions IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (

nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system" ) ELSE ( nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system" )

REM --> If error flag set, we do not have admin. if '%errorlevel%' NEQ '0' ( echo Requesting administrative privileges... goto UACPrompt ) else ( goto gotAdmin )

:UACPrompt echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs" set params= %* echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"

"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B

:gotAdmin pushd "%CD%" CD /D "%~dp0" :--------------------------------------
@echo on

for /f "skip=1 tokens=3" %%s in ('query user %YourWindowsUsername%') do ( %windir%\System32\tscon.exe %%s /dest:console ) exit

------------------------------- bat file end ---------------------------------------------------

In the pbixrefresher.py I have also changed the code to use hotkeys instead for the refresh. It works more stable in the latest version of PowerBI desktop (at least for me so far)

------------------------------- pbixrefresher.py file start---------------------------------------------- import pyautogui

...all the code to get to the Refresh up here..

pyautogui.hotkey('alt', 'win')
pyautogui.press('h')
pyautogui.press('r')

------------------------------- pbixrefresher.py file end------------------------------------------------

obviously you would need to install pyautogui via cmd line: pip install pyautogui

Hope this helps

dawiepoolman commented 4 years ago

^ Sorry, I struggled with the markup formatting