|
| 1 | +<#PSScriptInfo |
| 2 | +
|
| 3 | +.Version |
| 4 | + 1.0 |
| 5 | +.Guid |
| 6 | + 96c125d3-1a20-4282-b569-0da33be2d8c3 |
| 7 | +.Author |
| 8 | + Thomas J. Malkewitz @dotps1 |
| 9 | +.Tags |
| 10 | + Csv |
| 11 | +.ProjectUri |
| 12 | + https://github.com/dotps1/PSFunctions |
| 13 | +.ReleaseNotes |
| 14 | + Initial Release. |
| 15 | +
|
| 16 | +#> |
| 17 | + |
| 18 | +<# |
| 19 | +
|
| 20 | +.Synopsis |
| 21 | + Sets a value in a row in a csv file. |
| 22 | +.Description |
| 23 | + Sets a value or multiple values in the same row in a comma separated value. |
| 24 | +.Inputs |
| 25 | + System.String |
| 26 | + System.Collections.Hashtable |
| 27 | +.Outputs |
| 28 | + None |
| 29 | +.Parameter Path |
| 30 | + System.String |
| 31 | + The path to the csv file to modify. |
| 32 | +.Parameter Key |
| 33 | + System.String |
| 34 | + The csv header value to search in. |
| 35 | +.Parameter Value |
| 36 | + System.String |
| 37 | + The value of the cell matching the Key. |
| 38 | +.Parameter Hashtable |
| 39 | + System.Collections.HashTable |
| 40 | + A Hashtable containing the Key (Header) Value pairs to set in the row. |
| 41 | +.Example |
| 42 | + PS C:\> Set-CsvValue -Path .\my.csv -Key "ComputerName" -Value "MyComputer" -Hashtable @{ Owner = "dotps1" } |
| 43 | +.Example |
| 44 | + PS C:\> Set-CsvValue -Path .\my.csv -Key "ComputerName" -Value "MyComputer" -Hashtable @{ Owner = "dotps1"; Make = "Dell"; Model = "XPS 15" } |
| 45 | +.Notes |
| 46 | + If there are multiple values in the column used to key off, each row will be updated. Use a unique column value. |
| 47 | +.Link |
| 48 | + https://dotps1.github.io |
| 49 | +.Link |
| 50 | + https://www.powershellgallery.com/packages/Set-CsvValue |
| 51 | +.Link |
| 52 | + https://grposh.github.io |
| 53 | +
|
| 54 | +#> |
| 55 | + |
| 56 | + |
| 57 | +[CmdletBinding()] |
| 58 | +[OutputType( |
| 59 | + [Void] |
| 60 | +)] |
| 61 | + |
| 62 | +param ( |
| 63 | + [Parameter( |
| 64 | + Mandatory = $true |
| 65 | + )] |
| 66 | + [ValidateScript({ |
| 67 | + if (([System.IO.FileInfo]$_).Extension -eq ".csv") { |
| 68 | + return $true |
| 69 | + } else { |
| 70 | + throw "File must be a comma seperated value (*.csv)." |
| 71 | + } |
| 72 | + })] |
| 73 | + [String] |
| 74 | + $Path, |
| 75 | + |
| 76 | + [Parameter( |
| 77 | + Mandatory = $true |
| 78 | + )] |
| 79 | + [String] |
| 80 | + $Key, |
| 81 | + |
| 82 | + [Parameter( |
| 83 | + Mandatory = $true, |
| 84 | + ValueFromPipeline = $true, |
| 85 | + ValueFromPipelineByPropertyName = $true |
| 86 | + )] |
| 87 | + [String[]] |
| 88 | + $Value, |
| 89 | + |
| 90 | + [Parameter( |
| 91 | + Mandatory = $true |
| 92 | + )] |
| 93 | + [Hashtable] |
| 94 | + $Hashtable |
| 95 | +) |
| 96 | + |
| 97 | +begin { |
| 98 | + $csv = Import-Csv -Path $Path |
| 99 | + $content = Get-Content -Path $Path |
| 100 | + |
| 101 | + if ($content.Count -ne ($csv.Count + 1)) { |
| 102 | + Write-Error -Message "Unsupported index." |
| 103 | + break |
| 104 | + } |
| 105 | + |
| 106 | + if ($Key -notin $csv[0].PSObject.Properties.Name) { |
| 107 | + Write-Error -Message "Column headers do not contain value: '$Key'." |
| 108 | + break |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +process { |
| 113 | + foreach ($v in $Value) { |
| 114 | + $index = 1 |
| 115 | + $csv.ForEach({ |
| 116 | + if ($_.${Key} -eq $v) { |
| 117 | + foreach ($enum in $Hashtable.GetEnumerator()) { |
| 118 | + $_.($enum.Name) = $enum.Value |
| 119 | + } |
| 120 | + |
| 121 | + $content[$index] = $_ | |
| 122 | + ConvertTo-Csv -NoTypeInformation | |
| 123 | + Select-Object -Skip 1 |
| 124 | + } |
| 125 | + |
| 126 | + $index++ |
| 127 | + }) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +end { |
| 132 | + Set-Content -Value $content -Path $Path -ErrorAction Stop |
| 133 | +} |
0 commit comments