I have powershell script which does some AD operations from CSV file which has below data with headers<\/p>\n
SamAccountname,country,name<\/p>\n
I need the script to stop executing and send email if any of the column data is empty. I tried the below code but it exists even if all fields have data. Any help will be appreciated.<\/p>\n
$import=Import-Csv $import_file -Encoding Unicode\n\nif ($import)\n\n{\n\nwrite-host \"user list was successfully imported.\"\n\n}\nelse\n\n{write-host \"No user in the file imported.The script will now exit\";Start-Sleep -Seconds 10; exit}\n\nforeach ($userlist in $import)\n{\n \n if ( $null -eq $userlist.SamAccountName -or $null -eq $userlist.country -or $null -eq $userlist.name) {\n \n \n Write-host \"The csv has correct data\"\n }\n else {\n Write-Host \"CSV Empty Field!\"\n send-MailMessage -From $sender -To $recipients -Subject \"ALERT Script Failed\" -Body \"The script has failed due to empty columns please validate the csv file\" -SmtpServer 'mailserver' \n exit;\n }\n}\n<\/code><\/pre>","upvoteCount":4,"answerCount":9,"datePublished":"2022-01-31T14:14:20.000Z","author":{"@type":"Person","name":"spiceuser-6qndp","url":"https://community.spiceworks.com/u/spiceuser-6qndp"},"acceptedAnswer":{"@type":"Answer","text":"foreach($row in $csv){\n write-verbose $row -verbose\n\n if ($row.SamAccountName -and $row.country -and $row.name) {\n Write-host \"The csv has correct data\"\n }\n else{\n \"STOP\"\n }\n}\n<\/code><\/pre>\nVERBOSE: @{SamAccountname=; country=US; name=Mike}\nSTOP\nVERBOSE: @{SamAccountname=bob; country=DE; name=Mark}\nThe csv has correct data\n<\/code><\/pre>","upvoteCount":1,"datePublished":"2022-01-31T14:51:32.000Z","url":"https://community.spiceworks.com/t/powershell-emtpy-column/823589/6","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},"suggestedAnswer":[{"@type":"Answer","text":"I have powershell script which does some AD operations from CSV file which has below data with headers<\/p>\n
SamAccountname,country,name<\/p>\n
I need the script to stop executing and send email if any of the column data is empty. I tried the below code but it exists even if all fields have data. Any help will be appreciated.<\/p>\n
$import=Import-Csv $import_file -Encoding Unicode\n\nif ($import)\n\n{\n\nwrite-host \"user list was successfully imported.\"\n\n}\nelse\n\n{write-host \"No user in the file imported.The script will now exit\";Start-Sleep -Seconds 10; exit}\n\nforeach ($userlist in $import)\n{\n \n if ( $null -eq $userlist.SamAccountName -or $null -eq $userlist.country -or $null -eq $userlist.name) {\n \n \n Write-host \"The csv has correct data\"\n }\n else {\n Write-Host \"CSV Empty Field!\"\n send-MailMessage -From $sender -To $recipients -Subject \"ALERT Script Failed\" -Body \"The script has failed due to empty columns please validate the csv file\" -SmtpServer 'mailserver' \n exit;\n }\n}\n<\/code><\/pre>","upvoteCount":4,"datePublished":"2022-01-31T14:14:21.000Z","url":"https://community.spiceworks.com/t/powershell-emtpy-column/823589/1","author":{"@type":"Person","name":"spiceuser-6qndp","url":"https://community.spiceworks.com/u/spiceuser-6qndp"}},{"@type":"Answer","text":"It looks like you have it written that if $Null equals any of those fields, it will write \" The csv has correct data\". You either need to reverse those two or change it to Not Equal (-NE)<\/p>","upvoteCount":1,"datePublished":"2022-01-31T14:20:36.000Z","url":"https://community.spiceworks.com/t/powershell-emtpy-column/823589/2","author":{"@type":"Person","name":"titusovermyer","url":"https://community.spiceworks.com/u/titusovermyer"}},{"@type":"Answer","text":"
Gorfmaster has stated your problem correctly. However, you can ignore problematic records if you want and continue. I wasn’t sure of your use case so just throwing it out there.<\/p>\n
You could check for the empty data and if found, alert someone then still proceed with the ones that are complete records.<\/p>","upvoteCount":0,"datePublished":"2022-01-31T14:30:12.000Z","url":"https://community.spiceworks.com/t/powershell-emtpy-column/823589/3","author":{"@type":"Person","name":"jrp78","url":"https://community.spiceworks.com/u/jrp78"}},{"@type":"Answer","text":"
it’s not necessary $Null, it could be an empty string or whitespace<\/p>\n
you can try something like so<\/p>\n
if ($userlist.SamAccountName -and $userlist.country -and $userlist.name)\n<\/code><\/pre>\nor if you want to be more specific<\/p>\n
[string]::IsNullOrEmpty()\n<\/code><\/pre>","upvoteCount":2,"datePublished":"2022-01-31T14:31:59.000Z","url":"https://community.spiceworks.com/t/powershell-emtpy-column/823589/4","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"Thanks.<\/p>\n
I do not want the script to continue if there is any empty field or spaces in any of the column. The script should not perform any action of any of the columns even if single empty field in any of the field.<\/p>\n
I updated script below still it does exit.<\/p>\n
if ( $null -NE $userlist.SamAccountName -or $null -NE $userlist.country -or $null -NE $userlist.name)\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2022-01-31T14:48:59.000Z","url":"https://community.spiceworks.com/t/powershell-emtpy-column/823589/5","author":{"@type":"Person","name":"spiceuser-6qndp","url":"https://community.spiceworks.com/u/spiceuser-6qndp"}},{"@type":"Answer","text":"