i have just run the script, it ran successfully and saved the output for the first 500,…
- User
i have just run the script, it ran successfully and saved the output for the first 500, then after running the second pass, it saved some records but also got an error: Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null. At C:\Users\ROG\Desktop\email\verify_emails.ps1:55 char:20
- ... OutputFile -NoTypeInformation -Append
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : InvalidData: (:) [Export-Csv], ParameterBindingValidationException
- FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportCs
vCommand before it continued with its execution verify_emails.ps1: # verify_emails.ps1
EmailFile = "input.csv" # Input CSV file with email and name OutputFileBase = "results_" LastFile = "last_progress.txt" Results = @()
Check if progress file exists
if (Test-Path ProgressFile) { ProgressData = Get-Content ProgressFile | Out-String | ForEach-Object { _.Trim() } ProgressData[0] ProgressData[1] Write-Host "Resuming from email index LastOutputFile" } else { LastOutputFile = "" }
Read emails from the CSV file
EmailFile
Process emails
for (StartIndex; Emails.Count; i++) { EmailEntry = i] EmailEntry.email.Trim() EmailEntry.name.Trim()
Write-Host "Processing email $($i + 1) of $($Emails.Count): $Email" # Call the CLI tool to check if the email is reachable $Result = & .\check_if_email_exists.exe $Email # Parse the JSON result try { $JsonResult = $Result | ConvertFrom-Json $isReachable = $JsonResult.is_reachable } catch { Write-Host "Error parsing JSON for email: $Email. Result: $Result" $isReachable = "unknown" } # Add result to the array if it's safe if ($isReachable -eq "safe") { $Results += [PSCustomObject]@{ email = $Email name = $Name } } # Save results every batch if (($i + 1) % $BatchSize -eq 0) { $OutputFile = "${OutputFileBase}$($i + 1).csv" $Results | Export-Csv -Path $OutputFile -NoTypeInformation -Append Write-Host "Saved results for emails up to index $($i + 1) in file $OutputFile" # Save progress $i | Out-File -FilePath $ProgressFile $OutputFile | Out-File -FilePath $LastFile $Results.Clear() # Clear results for the next batch }}
Save any remaining results
if (Results.Count -gt 0) { OutputFile = "(Results | Export-Csv -Path OutputFile."
# Save progress $Emails.Count | Out-File -FilePath $ProgressFile $OutputFile | Out-File -FilePath $LastFile}
Clear progress file
Remove-Item $ProgressFile -ErrorAction SilentlyContinue Write-Host "Verification complete."
- Assistant
The error you're encountering indicates that the
$Resultsvariable is null when you're trying to export it to a CSV file usingExport-Csv. This typically happens when the$Resultsarray is empty, and you're trying to perform anExport-Csvoperation on it.To address this issue, there are a few steps you can take to ensure that you're not attempting to export an empty or null
$Resultsarray:-
Check Before Exporting: Ensure that
$Resultsis not empty or null before callingExport-Csv. -
Handle Initialization Properly: Ensure that
$Resultsis always initialized and populated correctly. -
Provide More Robust Logging: This will help you understand when and why
$Resultsmight be null or empty.
Here's a revised version of your script with checks and logging added:
# verify_emails.ps1 $EmailFile = "input.csv" # Input CSV file with email and name $OutputFileBase = "results_" $ProgressFile = "progress.txt" $LastFile = "last_progress.txt" $BatchSize = 500 $Results = @() # Check if progress file exists if (Test-Path $ProgressFile) { $ProgressData = Get-Content $ProgressFile | Out-String | ForEach-Object { $_.Trim() } $StartIndex = [int]$ProgressData[0] $LastOutputFile = $ProgressData[1] Write-Host "Resuming from email index $StartIndex in file $LastOutputFile" } else { $StartIndex = 0 $LastOutputFile = "" } # Read emails from the CSV file $Emails = Import-Csv -Path $EmailFile # Process emails for ($i = $StartIndex; $i -lt $Emails.Count; $i++) { $EmailEntry = $Emails[$i] $Email = $EmailEntry.email.Trim() $Name = $EmailEntry.name.Trim() Write-Host "Processing email $($i + 1) of $($Emails.Count): $Email" # Call the CLI tool to check if the email is reachable $Result = & .\check_if_email_exists.exe $Email # Parse the JSON result try { $JsonResult = $Result | ConvertFrom-Json $isReachable = $JsonResult.is_reachable } catch { Write-Host "Error parsing JSON for email: $Email. Result: $Result" $isReachable = "unknown" } # Add result to the array if it's safe if ($isReachable -eq "safe") { $Results += [PSCustomObject]@{ email = $Email name = $Name } } # Save results every batch if (($i + 1) % $BatchSize -eq 0) { if ($Results.Count -gt 0) { $OutputFile = "${OutputFileBase}$($i + 1).csv" $Results | Export-Csv -Path $OutputFile -NoTypeInformation -Append Write-Host "Saved results for emails up to index $($i + 1) in file $OutputFile" # Save progress $i | Out-File -FilePath $ProgressFile $OutputFile | Out-File -FilePath $LastFile $Results.Clear() # Clear results for the next batch } else { Write-Host "No results to save for emails up to index $($i + 1)." } } } # Save any remaining results if ($Results.Count -gt 0) { $OutputFile = "${OutputFileBase}$($Emails.Count).csv" $Results | Export-Csv -Path $OutputFile -NoTypeInformation -Append Write-Host "Saved remaining results in file $OutputFile." # Save progress $Emails.Count | Out-File -FilePath $ProgressFile $OutputFile | Out-File -FilePath $LastFile } else { Write-Host "No remaining results to save." } # Clear progress file Remove-Item $ProgressFile -ErrorAction SilentlyContinue Write-Host "Verification complete."Changes Made:
- Added Check Before Exporting: Added
if ($Results.Count -gt 0)checks before callingExport-Csvto avoid trying to export an empty array. - Logging: Added additional logging to indicate when there are no results to save.
This should help address the null binding error you're experiencing and provide better clarity about the script’s execution.
-