删除 PowerShell JSON 对象的子成员的更好方法是什么

Bor*_*bel 3 powershell json

我在 PowerShell 中有一个工作循环,它将从 JSON 对象中删除子成员(如果存在)。但是,我相信有一种更干净的方法可以做到这一点。你知不知道怎么?

for ($i = 0; $i -lt $destinationReleaseDefinitionJSON.environments.Count; $i++) {
    $destinationReleaseDefinitionJSON.environments[$i] = $destinationReleaseDefinitionJSON.environments[$i] | Select-Object * -ExcludeProperty queueId
    for ($ii = 0; $ii -lt $destinationReleaseDefinitionJSON.environments[$i].deployPhases.Count; $ii++) {
        $destinationReleaseDefinitionJSON.environments[$i].deployPhases[$ii].deploymentInput = $destinationReleaseDefinitionJSON.environments[$i].deployPhases[$ii].deploymentInput | Select-Object * -ExcludeProperty queueId
    }
}
Run Code Online (Sandbox Code Playgroud)

wOx*_*xOm 5

假设您只想删除一个属性,请直接通过以下方式执行.PSObject.Properties.Remove

foreach ($env in $destinationReleaseDefinitionJSON.environments) {
    $env.PSObject.Properties.Remove('queueId')
    foreach ($phase in $env.deployPhases) {
        $phase.deploymentInput.PSObject.Properties.Remove('queueId')
    }
}
Run Code Online (Sandbox Code Playgroud)