我正在编写一个脚本,通过API修改现有对象的安全性.我可以很容易地抓取对象,以及分配给它们的用户/组列表.但是,我需要解析这些数据,并将任何不匹配的用户/组添加到一个新的JSON主体中,以便在POST请求中发送回go .我为此创建了两个函数.一个用于创建JSON主体,另一个用于添加到JSON主体的删除部分.然而,它并没有像我期望的那样将id/type添加到remove部分,因为JSON的remove部分在每次迭代之后都保持为空.
我错过了什么吗?
function Build-JsonBody {
param (
[string]$defaultSecurity,
[array]$include,
[array]$remove
)
$jsonBody = @{
"default_security" = $defaultSecurity
"include" = $include
"remove" = $remove
}
return $jsonBody
}
function Add-ToRemoveSection {
param (
[ref]$removeSection,
[string]$id,
[string]$type
)
$itemToRemove = @{
"id" = $id
"type" = $type
}
$removeSection.Value += $itemToRemove
}
Write-Host "Finding all workspaces with Matter #$matter_num..."
$ws_search_url = $baseurl + "/work/api/v2/customers/$customer_id/libraries/ABC/workspaces?custom2=$matter_num"
$ws_resp = Invoke-RestMethod -Uri $ws_search_url -Headers $header -Method Get -ContentType "application/json"
$ws_results = $ws_resp.data.results
# For each workspace returned, look at the list of users, groups, and default security
foreach($ws in $ws_results){
$id = $ws.id
$sec_search_url = $baseurl + "/work/api/v2/customers/$customer_id/libraries/ABC/workspaces/ABC!164/security"
$sec_resp = Invoke-RestMethod -Uri $sec_search_url -Headers $header -Method Get -ContentType "application/json"
$sec_results = $sec_resp.data.results
$jsonBody = Build-JsonBody -defaultSecurity "private" -include @(@{ "id" = "ADMIN_USERS"; "access_level" = "full_access"; "type" = "group" }) -remove @()
# Iterate through the source data
foreach ($item in $sec_results) {
if ($item.id -ne "ADMIN_USERS") {
# For "user" type, add to the "remove" section
Add-ToRemoveSection -removeSection ([ref]$jsonBody.remove) -id $item.id -type $item.type
}
}
}
#Convert body to JSON
$jsonBodyJson = $jsonBody | ConvertTo-Json -Depth 10
Write-Host $jsonBodyJson