Last active
March 8, 2022 20:03
-
-
Save bitmetric-bv/50d66362c6f356a3ee45d201b79c14bf to your computer and use it in GitHub Desktop.
Export QVFs without data from Qlik Sense server, using PowerShell and qlik-cli
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
#requires -version 4 | |
<# | |
.SYNOPSIS | |
Export QVF files from Qlik Sense Enterprise server without data, grouped by user and stream. | |
.DESCRIPTION | |
Export QVF files from Qlik Sense Enterprise server without data, grouped by user and stream. | |
The time period for which to keep backups can be configured (standard is 30 days). | |
Files are stored in [username]\[stream name] format. If the QVF is not published, the stream | |
name 'Personal' is used. | |
.NOTES | |
Version: 1.2 | |
Author: Barry Harmsen / Bitmetric | |
Creation Date: 2019-04-11 | |
Change Date: 2019-04-12 | |
Purpose/Change: Initial script development | |
#> | |
Set-ExecutionPolicy Unrestricted -Scope CurrentUser | |
$start_time = (Get-Date) | |
$qlik_server = "localhost" # Qlik Server hostname | |
$backup_root_folder = "D:\QlikSense\Backup\Qvf_Export\Qvf" # Target directory for backups | |
$log_folder = "D:\QlikSense\Backup\Qvf_Export\Log" # Folder for log files | |
$file_prefix = (Get-Date).ToString("yyyyMMdd") + "_" # Prefix for backed up QVFs (and log file) | |
$file_suffix = "" # Postfix for backed up QVFs (and log file) | |
$days_to_keep = "30" # Number of days to keep backups | |
Start-Transcript -Path "$($log_folder)\$($file_prefix)backup_qvf_log$($file_suffix).txt" -Force | |
# Delete old backups | |
Write-Output "Removing backups older than $days_to_keep days" | |
$date_remove = (Get-Date).AddDays(-$days_to_keep) | |
Get-ChildItem -Path $backup_root_folder -Recurse | Where { ! $_.PSIsContainer } | Where-Object { $_.LastWriteTime -lt $date_remove } | Remove-Item | |
# Start new export/backup | |
Write-Output "Connecting to $qlik_server" | |
Connect-Qlik $qlik_server | |
# Loop through all the QVFs on the server | |
foreach($qvf in $(Get-QlikApp -full)) { | |
# Remove illegal characters from userId and QVF filename | |
# Extra replace for [ and ], which for some reason lead to an error (?) | |
$user_id = ($qvf.owner.userId).Split([IO.Path]::GetInvalidFileNameChars()) -join ' ' | |
$qvf_name = (($qvf.name).Split([IO.Path]::GetInvalidFileNameChars()) -join ' ').replace('[', '(').replace(']', ')') | |
# If the app is published, place it in a folder | |
# with the stream name, otherwise use Personal | |
if ($qvf.published) { | |
$stream_name = ($qvf.stream.name).Split([IO.Path]::GetInvalidFileNameChars()) -join ' ' | |
$backup_sub_folder = $user_id + "\" + $stream_name | |
} else { | |
$backup_sub_folder = $user_id + "\Personal" | |
} | |
# Check if the backup folder exists, if not | |
# then create it. | |
If(!(test-path "$backup_root_folder\$backup_sub_folder")) { | |
Write-Output "Creating folder $backup_root_folder\$backup_sub_folder" | |
New-Item -ItemType Directory -Force -Path "$backup_root_folder\$backup_sub_folder" | |
} | |
# Export the QVF without data | |
$export_filename = "$($file_prefix)$($qvf_name)$($file_suffix).qvf" | |
Export-QlikApp -id $qvf.id -skipdata -filename "$($backup_root_folder)\$($backup_sub_folder)\$($export_filename)" | |
Write-Output "******************************************" | |
Write-Output "App: $($qvf.name)" | |
Write-Output "Id: $($qvf.id)" | |
Write-Output "Owner name: $($qvf.owner.name)" | |
Write-Output "Owner id: $($qvf.owner.userId)" | |
Write-Output "Stream: $($qvf.stream.name)" | |
Write-Output " " | |
Write-Output "Exported to: $($backup_root_folder)\$($backup_sub_folder)\$($export_filename)" | |
Write-Output " " | |
} | |
$elapsed_time = (Get-Date) - $start_time | |
Write-Output "Script runtime" | |
Write-Output "--------------" | |
Write-Output $elapsed_time | |
Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment