使用API重命名Qlik Sense Sheet遇404错误,求修正PowerShell代码
Qlik Sense Sheet重命名API调用404错误修正
问题场景
尝试通过PowerShell调用Qlik Sense API重命名Sheet,已知需通过properties接口操作,但执行代码时在获取Sheet信息步骤返回404错误。
原代码
$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 } } }
错误信息
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
错误原因
Qlik Sense的Sheet API资源必须关联具体App,直接请求/qrs/sheet/full未指定所属App,接口无法定位到有效资源,因此返回404。
修正后的代码
$hdrs = @{} $hdrs.Add("X-Qlik-Xrfkey","examplexrfkey123") $hdrs.Add("X-Qlik-User", "UserDirectory=INTERNAL; UserId=sa_api") # 确保正确获取Qlik客户端证书 $cert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Where-Object {$_.Subject -match 'QlikClient'} $Data = Get-Content C:\ProgramData\Qlik\Sense\Host.cfg $FQDN = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($Data)) # 使用更安全的TLS协议版本 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]'Tls12,Tls13' # 函数:更新Sheet名称 function Update-SheetName { param ( [string]$sheetId, [string]$newName ) $body = @{ name = $newName } | ConvertTo-Json -Compress try { Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/$sheetId/properties?xrfkey=examplexrfkey123" -Method Put -Headers $hdrs -ContentType 'application/json' -Body $body -Certificate $cert -ErrorAction Stop Write-Host "Sheet $sheetId 名称已更新为: $newName" } catch { Write-Error "更新Sheet $sheetId 失败: $_" } } # 获取目标App列表 $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') { Write-Host "开始处理App: $($app.name) (ID: $($app.id))" # 通过App ID过滤获取所属Sheet,这是解决404的关键 $sheetsUri = "https://$($FQDN):4242/qrs/sheet/full?filter=app.id eq '$($app.id)'&xrfkey=examplexrfkey123" try { $sheets = Invoke-RestMethod -Uri $sheetsUri -Method Get -Headers $hdrs -ContentType 'application/json' -Certificate $cert -ErrorAction Stop foreach ($sheet in $sheets) { $ownerName = $sheet.owner.name $sheetType = if ($sheet.community) { "Community" } else { "MySheet" } $newName = "$ownerName - $sheetType - $($sheet.name)" Update-SheetName -sheetId $sheet.id -newName $newName } } catch { Write-Error "获取App $($app.id) 的Sheet列表失败: $_" } } }
关键修正点
- Sheet接口添加App过滤:通过
filter=app.id eq '$($app.id)'参数指定Sheet所属的App,确保接口能定位到有效资源 - 协议版本优化:移除不安全的Ssl3、Tls,仅保留Tls12、Tls13
- 错误处理增强:添加
try/catch块捕获异常,便于排查问题 - 证书匹配优化:使用
-match替代-like,提升证书匹配准确性 - 更新接口补充xrfkey:在
properties接口URL中添加xrfkey参数,符合Qlik API安全规范
内容的提问来源于stack exchange,提问作者Sai Krishna
相关产品推荐
相关产品推荐

