简体   繁体   中英

Parameter error when resolving AD user

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 admin@mydomain.com -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.

Your input file contains non-existing user identities. To have Get-ADUser skip over them without complaining use -Filter instead of the implicit -Identity :

Get-ADUser -Filter "SamAccountName -eq '$DNS'" -Properties mail |
  Select-Object -Property mail -Unique

or use -LDAPFilter with Ambiguous Name Resolution if you're not sure which attribute exactly to match:

Get-ADUser -LDAPFilter "(anr=$DNS)" -Properties mail |
  Select-Object -Property mail -Unique

Edit: To extract the part before a hyphen in a string it might be better to do something like this:

$getemail = $data.DNS | ForEach-Object {
  $_ -replace '-.*'
} | ForEach-Object {
  Get-ADUser -LDAPFilter "(anr=$_)" -Properties mail |
    Select-Object -Property mail -Unique
}

Some additional comments on the rest of your code. The first foreach loop uses the same variable for loop variable and element list:

$dns2 = foreach ($DNS in $dns) {
    $myregex.split($dns)
}

Don't do that. PowerShell variable names aren't case-sensitive.

Also, your regular expression

[regex]$myregex = "\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S\S$"

can be simplified like this:

[regex]$myregex = "\S{16}$"

or (if you don't care about the exact number of characters) like this:

[regex]$myregex = "\S+$"

Thank you again Ansgar!

After a little more tweaking, I incorporated your suggestions into the script and this is the one that now works perfectly:

$data = Get-Content -path c:\admin\scripts\windows\test.csv | select-object` 
-skip 4 | Out-String | ConvertFrom-Csv | select-object -property DNS -unique

$getuser = $data.dns | ForEach-Object {$_ -replace '-.*'}

$getemail = foreach($dns in $getuser) {get-aduser -filter "(samaccountname` 
-eq '$dns')" -properties mail | select-object -property mail -unique} 

send-mailmessage -attachments c:\admin\scripts\windows\test.csv` 
-from admin@mydomain.com -to $getemail.mail`
-subject "UPDATES OF NON-SUPPORTED APPLICATIONS REQUIRED"`
-smtpserver smtp.mydomain.com

I appreciate your assistance!

JC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM