Solved

Powershell - check csv format

Posted on 2016-11-15
4
30 Views
Last Modified: 2016-11-17
Hello I want to import a users.csv to create new AD users but I want to check its in the correct format first, how can I do this in powershell? Checking correct column headings, maybe also checking the entire contents not containing illegal characters like hyphens, apostrophes etc..

Any advice please?
0
Comment
Question by:Pete
4 Comments
 
LVL 6

Expert Comment

by:Niten Kumar
Comment Utility
What is the format of your csv file?  Best would be if you use a proper template where the required data is input correctly then there should be no issues.  See attached .xls file.  You can modify it to your requirements.  The instructions are on the first sheet.
MassUserCreationScriptSource.xls
0
 
LVL 1

Author Comment

by:Pete
Comment Utility
That is not the answer to the question, I know what a properly formatted csv should look like, I want to script in some protection in case it is wrong.
0
 
LVL 3

Expert Comment

by:Kevin Stanush
Comment Utility
What Niten is suggesting, and I'll second, is that Powershell might not be the best tool for doing this 'validation'.  You are going to have to read each line from the CSV file, and then perform custom checks on it, based on your own needs, like not allowing certain characters, checking length, if each element is supplied, etc.

It might be easier and more efficient to perform these checks before the file is created.
0
 
LVL 82

Accepted Solution

by:
oBdA earned 500 total points
Comment Utility
Here's a starting point. The first part checks if all expected headers appear in the csv; this can be used pretty universally.
The content check you're going to have to customize yourself; I've put some samples in on how it could be done.
$Csv = Import-Csv -Path "C:\Temp\ADUser.csv"

## List all columns that MUST be in the csv:
$ColumnsExpected = @(
	'SamAccountName',
	'DisplayName'
)
## Error and help messages for the content:
Function Set-ContentError($Index, $Column, $Value) {
	"Row $($Index), column '$($Column)': value '$($Row.$Column)' is invalid!" | Write-Host -ForegroundColor Red
	$HelpMessage = Switch ($Column) {
		'SamAccountName'	{'Allowed characters are a-z, 0-9, -, maximum length 20.'}
		'DisplayName'		{'No single quotes, please.'}
	}
	If ($HelpMessage) {$HelpMessage | Write-Host -ForegroundColor Yellow}
	Return $False
}

## Verify that all expected columns are there (additional columns in the csv will be ignored)
$ColumnsOK = $True
$ColumnsCsv = $Csv | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name

$ColumnsExpected | ForEach-Object {
	If ($ColumnsCsv -notcontains $_) {
		$ColumnsOK = $False
		"Expected column not found: '$($_)'" | Write-Host -ForegroundColor Red
	}
}

If (-not $ColumnsOK) {
	Throw "The csv format is incorrect!"
}

## Verify that the contents are OK:
$ContentOK = $True
$RowIndex = 0
ForEach ($Row In $Csv) {
	$RowIndex++
	$Column = 'SamAccountName'
	If (-not ($Row.$Column -match '\A[-a-z0-9]{1,20}\Z')) {
		$ContentOK = Set-ContentError -Index $RowIndex -Column $Column -Value $Row.$Column
	}
	$Column = 'DisplayName'
	If ($Row.$Column.Contains("'")) {
		$ContentOK = Set-ContentError -Index $RowIndex -Column $Column -Value $Row.$Column
	}
}

If (-not $ContentOK) {
	Throw "The csv content is incorrect!"
}

Open in new window


A sample csv:
"samAccountName","DisplayName"
"DoeJohn", "John Doe"
"DoeJohn", "John'Doe"
"DoeJane12345678901234", "Jane Doe"

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now