Skip to content

Instantly share code, notes, and snippets.

@Nillth
Created October 30, 2020 14:53
Show Gist options
  • Save Nillth/a678ffa19c19f7fea68d130fab60ff24 to your computer and use it in GitHub Desktop.
Save Nillth/a678ffa19c19f7fea68d130fab60ff24 to your computer and use it in GitHub Desktop.
Copy Qlik Sense Data Connections from one environment to another
<#
.NOTES
===========================================================================
Created on: 2020-10-31 1:44 AM
Created by: Marc Collins (Marc.Collins@Qlik.com)
Organization: Qlik Professional Services
Filename: QlikSense-Copy-DataConnections.ps1
===========================================================================
.DESCRIPTION
Copy Data Connections from one environment to another.
#>
$OutputFolder = "C:\Temp"
$DataConnectionFile = "$($OutputFolder)\DataConnections_$($env:COMPUTERNAME)_$(Get-date -Format yyyyMMdd).json"
$SourceServerName = "Localhost"
$DestinationServerName = "Sense01"
#Set the name of any to be excluded. e.g the builtin ones you dont want updated in Destination
$ExcludeConnections = "ServerLogFolder", "QLogs", "ArchivedLogsFolder", "AttachedFiles"
#region backup
#Must use a QlikClient Certificate in order to get UserNames & Password (User auth does not get this)
$SourcCert = Get-Item Cert:\CurrentUser\My\A39B2FA96278185F158046CF335BC5A0D337E58A
Connect-Qlik -TrustAllCerts -Certificate $SourcCert -Username "Internal\sa_api" -Computername $SourceServerName
#Get source server connections
$Dataconnections = Get-QlikDataConnection -full -raw
#Export to File
$Dataconnections | ConvertTo-Json -Depth 10 -Compress | out-file -Encoding utf8 -FilePath $DataConnectionFile
#endregion backup
#for Air Gapped environments copy the json file to the next environment and modify the next line to import the file
#$Dataconnections = Get-Content "C:\temp\DataConnections_sourceserver_date.json" |ConvertFrom-Json
#Connect to next environment (User auth is find for creating the connections)
Connect-Qlik -TrustAllCerts -Computername $DestinationServerName
#region Import missing
#Get the existing connections for comparison
$DataconnectionsEnvX = Get-QlikDataConnection -full -raw
#Create any connections that dont exist
$Dataconnections | Where-Object{
!($_.name -in $DataconnectionsEnvX.name)
} | ForEach-Object{
#foreach of the dataconnections from source that do not exist, convert the object to Json and post to the pai
Invoke-QlikPost "/qrs/dataconnection" -body $($_ | ConvertTo-Json -Depth 10 -Compress)
}
#endregion Import missing
#region Update Existing
#update any existing connections.
$Date = (Get-Date).AddDays(1).ToString("yyyy-MM-ddTHH:mm:ss.000Z")
$Dataconnections | Where-Object{
!($_.name -like "monitor_apps_*" -or $_.name -in $ExcludeConnections) -and ($_.name -in $DataconnectionsEnvX.name)
} | ForEach-Object{
$DC = $_;
$EDC = $DataconnectionsEnvX | Where-Object{
$_.name -eq $DC.name
};
$EDC.modifiedDate = $Date
$EDC.connectionstring = $DC.connectionstring;
$EDC.type = $DC.type;
$EDC.username = $DC.username;
$EDC.password = $DC.password
$EDC.logOn = $DC.logOn
$EDC.architecture = $DC.architecture
#Find the owner as the Object ID will probably be different
$EDC.owner = Get-QlikUser -filter "UserID eq '$($DC.owner.userId)' and userDirectory eq '$($DC.owner.userDirectory)'"
#These are a little more complext as they need to exist in the envrionemnt before they can be applied 99% this is why it will fail.
#$EDC.tags = $DC.tags
#$EDC.customProperties = $DC.customProperties;
Invoke-QlikPut "/qrs/dataconnection/$($EDC.id)" -body $($EDC | ConvertTo-Json -Depth 10 -Compress)
}
#endregion Update Existing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment