# Validate the path to the input CSV file, if it fails validation, exit
if (validatePath) { exit }
Now that the input file path for the CSV has been confirmed, the next code segment handles loading the
file. This code segment is very similar to that used for the same purpose in the simple version of the
script, but adds error checking for importing the contents of a CSV file.
# Read input from the CSV file, if there is a failure reading the file, exit
$Users = (Import-Csv $CSVUPath)
When running this script it is possible to enter the path of a file that is not a CSV file. When the Import-
Csv cmdlet attempts to read this file, it can report an error if it finds the file is either improperly
formatted or the file cannot be opened for one reason or another. Because the result of Import-Csv is
stored in the $users variable, it can be tested with another if statement to see if the result is true
(importing the file succeeded) or false (importing the file failed).
Part IV: Automating Administration
438
If $users is false , NOT changes the result of the conditional test to true and the code block executes.
The Write-Host cmdlet displays a warning message telling the user the file specified is either invalid or
is not a properly formatted CSV file. After that the script exits.
Pages:
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600