Crear un código basado en el contenido para identificar rápidamente si un registro específico sufrió modificaciones de un mes a otro.
Create a content-based code to quickly identify whether a specific record changed from one month to another.
Este ejercicio es solo para aprendizaje y pruebas. No ejecutes scripts contra datos reales, ambientes de producción, carpetas compartidas, sistemas críticos o repositorios empresariales sin autorización, respaldos, pruebas previas, permisos mínimos, control de cambios y cumplimiento de los protocolos de ciberseguridad de tu organización.
This exercise is for learning and testing only. Do not run scripts against real data, production environments, shared folders, critical systems, or enterprise repositories without authorization, backups, prior testing, least-privilege permissions, change control, and compliance with your organization's cybersecurity protocols.
Agregar una columna de checksum que represente el estado de una fila. Si algún campo clave cambia, el hash cambia también, facilitando auditorías, reconciliación mensual y control de cambios.
Add a checksum column that represents the state of a row. If any key field changes, the hash also changes, making audits, monthly reconciliation, and change control easier.
| ID | Status | Monto |
|---|---|---|
| 1001 | Active | 2500 |
| 1002 | Active | 1800 |
| 1003 | Inactive | 0 |
| 1004 | Active | 3250 |
$cadena = "$($_.ID)-$($_.Status)-$($_.Monto)" $hash = Get-FileHash -InputStream ` ([IO.MemoryStream]::new([byte[]][char[]]$cadena)) ` -Algorithm MD5
| ID | Status | Monto | Row_Checksum |
|---|---|---|---|
| 1001 | Active | 2500 | 9F3A...D21B |
| 1002 | Active | 1800 | 71BC...90AF |
| 1003 | Inactive | 0 | 54DE...118C |
| 1004 | Active | 3250 | 8AA2...EF31 |
# ==========================================================
# PowerShell Advanced Topic 14
# Generate a Unique Row Hash / Checksum
# ==========================================================
$inputFile = "C:\Data\Monthly_Records.csv"
$outputFile = "C:\Data\Monthly_Records_With_Checksum.csv"
$datos = Import-Csv $inputFile
$resultado = foreach ($row in $datos) {
# Build a consistent string using the fields that define the record
$cadena = "$($row.ID)-$($row.Status)-$($row.Monto)"
# Convert the string into a memory stream
$bytes = [System.Text.Encoding]::UTF8.GetBytes($cadena)
$stream = [System.IO.MemoryStream]::new($bytes)
# Generate hash
$hash = Get-FileHash -InputStream $stream -Algorithm MD5
# Add checksum as a new property
$row | Add-Member -NotePropertyName "Row_Checksum" -NotePropertyValue $hash.Hash -Force
$row
}
$resultado | Export-Csv $outputFile -NoTypeInformation -Encoding UTF8
Write-Host "Process completed."
Write-Host "Output file: $outputFile"Este patrón convierte una fila completa en una firma técnica. Es muy útil para reconciliación, auditoría, control de cambios y procesos ETL donde necesitamos saber qué registros cambiaron realmente.
This pattern turns a full row into a technical signature. It is highly useful for reconciliation, auditing, change control, and ETL processes where we need to know which records actually changed.