I am trying to resolve why I get an error with the following script. I should prefix this statement by saying that the script does correctly output and function, but always prefixed by the same number of errors.
The CSV file being used is auto-generated and the headers are not on the first line, which is why I have to skip the first 4 lines.
Here is the script:
# Getting the name of PCs in the DNS column.
$data = Get-Content -Path c:\admin\scripts\windows\test.csv |
Select-Object -Skip 4 |
Out-String |
ConvertFrom-Csv |
Select-Object -Property DNS -Unique
$dns = $data.DNS
# REMOVES DOMAIN NAME AND LEAVES JUST THE HOST/USERNAME
[regex]$myregex = "\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S$"
$dns2 = foreach ($DNS in $dns) {
$myregex.split($dns)
}
$getemail = foreach ($DNS in $dns2) {
Get-ADUser $DNS -Properties mail | Select-Object -Property mail -Unique
}
Send-MailMessage -Attachments c:\admin\scripts\windows\test.csv -From [email protected] -To $getemail.mail -Subject "UPDATES OF NON-SUPPORTED APPLICATIONS REQUIRED" -SmtpServer smtp.mydomain.com
This will get the email address for the users and send a copy of a report to each of them. It actually works, but for every correct email address, there is a corresponding error:
Get-ADUser : Cannot bind parameter 'Identity' to the target. Exception setting "Identity": "Cannot validate argument on parameter: 'Identity'. The argument is null or empty. Supply an argument that is not null or empty and then try the command again.
I would just like to stop the errors to avoid any confusion for others who run this script.