-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathFunction-ValidateCSV.ps1
More file actions
38 lines (34 loc) · 1.36 KB
/
Function-ValidateCSV.ps1
File metadata and controls
38 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
function import-ValidCSV {
<#
.SYNOPSIS
Imports a CSV file, validating that it has the columns specified in the -requiredColumns parameter.
Throws a critical error if a required column is not found.
.NOTES
IMPORTANT INFORMATION:
Please always mention the original authors of scripts or script extracts, it helps for traceability, and
more importantly gives back to Caesar what belong to Caesar ;-)
Author : Jason Coleman
Role: Californian PowerShell and Virtualization Genius
Link: https://virtuallyjason.blogspot.com/2016/08/import-validcsv-powershell-function.html
.LINK
https://virtuallyjason.blogspot.com/2016/08/import-validcsv-powershell-function.html
#>
param
(
[parameter(Mandatory=$true)]
[ValidateScript({test-path $_ -type leaf})]
[string]$inputFile,
[string[]]$requiredColumns
)
$csvImport = import-csv $inputFile
$inputTest = $csvImport | gm
foreach ($requiredColumn in $requiredColumns)
{
if (!($inputTest | ? {$_.name -eq $requiredColumn}))
{
write-error "$inputFile is missing the $requiredColumn column"
exit 10
}
}
$csvImport
}