I am trying to rename Qlik Sense Sheets using APIs. I got to know we should rename only using properties. I tried below code in Powershell but its not working. Please help me correct the code. I am getting error while retrieving sheets information
$hdrs = @{}
$hdrs.Add("X-Qlik-Xrfkey","examplexrfkey123")
$hdrs.Add("X-Qlik-User", "UserDirectory=INTERNAL; UserId=sa_api")
$cert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Where {$_.Subject -like '*QlikClient*'}
$body = '{}'
$Data = Get-Content C:\ProgramData\Qlik\Sense\Host.cfg
$FQDN = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($($Data)))
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
$guid = [guid]::NewGuid()
# Function to update sheet name
function Update-SheetName {
param (
[string]$sheetId,
[string]$newName
)
$body = @{
name = $newName
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/$sheetId/properties" -Method Put -Headers $hdrs -ContentType 'application/json' -Body $body -Certificate $cert
}
$apps = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/app/full?xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert
Foreach ($app in $apps) {
if ($app.published -and $app.stream.name -eq 'SCPM' -and $app.name -eq 'MAERSK Operational Test Rename') {
# Retrieve list of sheets
$sheets = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/full?xrfkey=examplexrfkey123" -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert
# Iterate through each sheet
foreach ($sheet in $sheets) {
# Construct new sheet name with owner name and sheet type
$ownerName = $sheet.owner.name
$sheetType = if ($sheet.community) { "Community" } else { "MySheet" }
$newName = "$ownerName - $sheetType - $($sheet.name)"
# Update sheet name
Update-SheetName -sheetId $sheet.id -newName $newName
}
}
}
ERROR:
Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At C:\Test\RenameSheetName-2.ps1:37 char:13
+ ... $sheets = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/f ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Looks like your
Update-SheetNamefunction is using a$headersvariable while at the beginning of your script you created a$hdrs. You may need to use the-Certificate $certpart too, as well as thexrfkey=examplexrfkey123query parameter.