我正在尝试使用 API 重命名 Qlik Sense Sheets。我知道我们应该只使用属性来重命名。我在 Powershell 中尝试了以下代码,但它不起作用。请帮我纠正代码。 我在检索工作表信息时遇到错误
$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 $headers -ContentType 'application/json' -Body $body
}
$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 'Data' -and $app.name -eq 'Operational Test Rename') {
# Retrieve list of sheets
$sheets = Invoke-RestMethod -Uri "https://$($FQDN):4242/qrs/sheet/full" -Method Get -Headers $headers
# 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: (403) Forbidden.
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
看起来您的
Update-SheetName
函数正在使用 $headers
变量,而在脚本开头您创建了 $hdrs
。您可能还需要使用 -Certificate $cert
部分以及 xrfkey=examplexrfkey123
查询参数。