Below example will provide quick snippets to provide validation on the CSV headers to make sure expected header within selected CSV file is provided. If expected CSV header is not found, script will stop.
First function is to select CSV file and import it
#(1)_.Run Function to get CSV file -GUI lets administrators
locate the CSV file
Function Import-CSVfile($inDir) { [System.Reflection.Assembly]::LoadWithPartialName('System.windows.forms') |
Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.initialDirectory = $inDir $OpenFileDialog.filter = 'All files (*.*)|
*.*' $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename $inDir = 'C:\Temp\Migration_CSV_Files\' } |
#(2)_.Import CSV file
$csvfile
= Import-CSVfile -initialDirectory $inDir
$UsersArray = Import-Csv -path $csvfile
-ErrorAction Stop |
#(3)_.Validate CSV Header value, expected header from CSV file
is, in this example $header = 'sAMAccountName'
$varInf = 'sAMAccountName' if (!($header -like $varInf)
){ Write-Host
'Header DOES NOT have correct value' -ForegroundColor Red Write-Host
'Script will STOP' Start-Sleep
-Seconds 5 break; } |
#(4)_.Same task, Validate CSV header stop script if expected
header is not found
$header = $UsersArray[0].psobject.Properties.Name $varInf = 'sAMAccountName' if ($header
-like $varInf
){ Write-Host 'Header has correct value' -ForegroundColor
Green }else{ Write-Host
'Header DOES NOT have correct value' -ForegroundColor Red } |
Azure Solutions Architect
AWS Certified Cloud Practitioner
Azure Certified Security Engineer Associate
https://simplepowershell.blogspot.com
https://cloudsec365.blogspot.com
https://msazure365.blogspot.com
https://twitter.com/Message_Talk
No comments:
Post a Comment