Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have the following PS Function for validating CSV files.

The CSV Files are in the format

EmpNo (int), Type (int), StartDate (DateTime), EndDate (DateTime)

And the powershell script verifies the type of each of column value and also validates that there are no duplicate employee numbers. However I assume this can be done in a more "PowerShell-y" way. Feedback welcome.

function Validate-MinimumGuaranteeCSV($file)
{
    $rawdata = Import-Csv $file
    $count = 0;

    $rawdata | ForEach  {  

        $count++
        [ref]$StartDate = Get-Date
        [ref]$EndDate = Get-Date
        [ref]$EmpNo= 1
        [ref]$Type= 1

        if([DateTime]::TryParse($_.StartDate, $StartDate) -eq $false)
        {
            Write-Host "StartDate on Line $count is invalid" 
        }
        if([DateTime]::TryParse($_.EndDate, $EndDate) -eq $false)
        {
            Write-Host "EndDate on Line $count is invalid" 
        }
        if([Int32]::TryParse($_.EmpNo, $EmpNo) -eq $false)
        {
            Write-Host "EmpNo on Line $count is invalid" 
        }
        if([Int32]::TryParse($_.Type, $Type) -eq $false)
        {
            Write-Host "Type on Line $count is invalid" 
        }
    }

    $rawdata | Group-Object -Property EmpNo | 
        Where { $_.Count -gt 1 } | 
        foreach { Write-Host "EmpNo $($_.Group[0].EmpNo) occurs more than once" }
}
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.