Hi Team,

Please help on how to send email if the script found a blank space on the csv file and parse the result in csv file if not found

i just found script wrote by neally but i dont know how to start to add send mail.


```
$csv=Import-Csv -Path C:\temp\credential.csv
$columns = $csv | get-member -MemberType 'noteproperty' | select -ExpandProperty 'Name'

foreach($column in $columns){
    $count   = 1
    foreach($line in $($csv.$column)){
        if(-not($line)){
            Write-host "Column '$column', Line '$count' has Empty or blank ,please supply required information" -ForegroundColor Red
        
        }
         $count++
    }
    
}
```

thanks

4 Spice ups

What have you tried though???

If you post code, please use the ‘Insert Code’ button and mark it as powershell! Please and thank you!

codebutton_small.png

1 Spice up

Hi Neally,

i just change the insert code to powershell, because sometimes its not work when i insert using powershell.

apology for that

still seems to be text… if you mark it as powershell, it syntax highlights which makes reading it much easier.

Please also post what you have tried?

you can e.g. assign the whole thing to a CSV and then send an email?

$csv=Import-Csv -Path C:\temp\credential.csv
$columns = $csv | get-member -MemberType 'noteproperty' | select -ExpandProperty 'Name'

foreach($column in $columns){
    $count   = 1
    foreach($line in $($csv.$column)){
        if(-not($line)){
            Write-host "Column '$column', Line '$count' has Empty or blank ,please supply required information" -ForegroundColor Red
         $count++
        }
        Else{
        "testmal only"
        }
       
    }
    
}

Hi Neally,

I tired also to make a else if no blank row found. i just only use a message that information, but when i execute it repeat the results,

Here is the sample results

testmal only
testmal only
testmal only
Column ‘server’, Line ‘1’ has Empty or blank ,please supply required information
testmal only
testmal only

1 Spice up

Do you have a sample CSV file?

What blank are you talking about?
There is $null, there is whitespace "test ", and there is an empty string " ", all three of those cause issues.
For an entire ROW to be blank ALL columns must be null.

ok, what exactly did you expect as result if that is NOT what you want?

Blank i mean a null or empty,

see attached sample csv file

Here is the sample results i tried to remove the -not condition.my goal is , if the script found a null or empty row. the script would stop and send the error using send mail

Column ‘password’, Line ‘1’ has Empty or blank ,please supply required information
testmal only
Column ‘server’, Line ‘2’ has Empty or blank ,please supply required information
testmal only
Column ‘user’, Line ‘2’ has Empty or blank ,please supply required information
testmal only

how to send the three message in one send mail?

$csv=Import-Csv -Path C:\temp\credential.csv
$columns = $csv | get-member -MemberType 'noteproperty' | select -ExpandProperty 'Name'

foreach($column in $columns){
    $count   = 1
    foreach($line in $($csv.$column)){
        if(($line)){
            #Write-host "Column '$column', Line '$count' has Empty or blank ,please supply required information" -ForegroundColor Red
         
        }
        Else{
          Write-host "Column '$column', Line '$count' has Empty or blank ,please supply required information" -ForegroundColor Red
         
        "testmal only"

        }
       $count++
    }
    
}

credential.txt (60 Bytes)

here you go

function start-csvcheck {
    param(
        [parameter(mandatory)]
        $csv
    )

    $csv = import-csv $csv
    $noteproperties = ($csv | get-member | where-object MemberType -eq 'noteproperty').name
    $counter = 2

    $report = 
    foreach ($row in $csv) {
        $propertycheck =
        foreach ($property in $noteproperties) {
            if ([string]::IsNullOrEmpty($row.$property) -or [string]::IsNullOrWhiteSpace($row.$property)) {
                write-output $property
            }
            elseif ($row.$property -match '\s$+') {
                write-output $property
            }
        }
   
        if ($propertycheck) {
            [pscustomobject]@{
                Status = 'Error'
                Row    = $counter
                Column = $propertycheck -join ','
            }
        }
        ++$counter
    }
    $report
}

$csvcheck = start-csvcheck "C:\Users\neally\Desktop\Book2.csv"

# display to host
$csvcheck

if ($csvcheck) {

$Head = 
@"
<style>
body { background-color:#f6f6f6;}
TABLE {border-width: 0px;border-style: solid;border-color: black;border-collapse: collapse;}
TH {border-width: 1px;padding: 1px;border-style: solid;border-color: black;background-color: #34495e ; color:white;text-align: center;}
TD {border-width: 1px;padding: 1px;border-style: solid;border-color: black;text-align: center;}
TR:Nth-Child(Even) {Background-Color: #dddddd;text-align: center;}
</style>
"@
    $mailer = @{
        To         = "to@mycompany.com"
        From       = "from@mycompany.com"
        Subject    = "Error in CSV"
        Body       = $csvcheck | convertto-html -Head $head | out-string
        BodyAsHtml = $true
        Smtpserver = "smtphost"
    }
    Send-MailMessage @mailer
}
else {
    Write-Output "CSV check pass"
}

Wow, its superb.

Thanks Neally for this.

right now, ill be adding if the file is exist or not to the script then send mail.