Bulk updates safely¶
Scenario: a team was renamed and you need to reassign its open incidents, and you want to be certain nothing unexpected changes.
The safe pattern¶
- Query exactly the records you intend to change.
- Back them up.
- Preview with
-WhatIf. - Apply.
- Verify.
Connect-Snowplow -Profile test -ClientCredentials
# 1. Select
$targets = Get-SnowRecord -Table incident `
-Query 'active=true^assignment_group.name=Helpdesk' `
-Fields sys_id,number,assignment_group -All
"$($targets.Count) incidents selected"
# Resolve the target group and stop unless the name is unambiguous
$serviceDeskGroups = @(Get-SnowRecord -Table sys_user_group -Query 'name=Service Desk' -Fields sys_id -Limit 2)
if ($serviceDeskGroups.Count -ne 1) {
throw "Expected exactly one Service Desk group, found $($serviceDeskGroups.Count)."
}
$assignmentGroupId = $serviceDeskGroups[0].sys_id
# 2. Back up
$targets | Export-SnowRecord -Format Json -Path ".\backup-$(Get-Date -Format yyyyMMdd-HHmm).json"
# 3. Preview
$targets | ForEach-Object {
Set-SnowRecord -Table incident -SysId $_.sys_id -Values @{ assignment_group = $assignmentGroupId } -WhatIf
}
# 4. Apply
$targets | ForEach-Object {
Set-SnowRecord -Table incident -SysId $_.sys_id -Values @{ assignment_group = $assignmentGroupId }
}
# 5. Verify
Get-SnowRecord -Table incident -Query 'active=true^assignment_group.name=Helpdesk' -Limit 1
Production guard rails¶
A profile with -Environment Prod refuses writes until you reconnect with -AllowWrite, and then
prompts for each one. Rehearse the same script against a Test profile first.
Add-SnowplowProfile -Name prod -Instance acme -ClientCredentialSecretName SnowplowProdOAuth -Environment Prod
Connect-Snowplow -Profile prod -ClientCredentials -AllowWrite
Clean up test data¶
Get-SnowRecord -Table sys_user -Query 'user_nameSTARTSWITHtest.' -Fields sys_id,user_name |
ForEach-Object { Remove-SnowRecord -Table sys_user -SysId $_.sys_id -WhatIf }
Related: Set-SnowRecord, New-SnowRecord, Remove-SnowRecord.