简体   繁体   English

解决AD用户时出现参数错误

[英]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. 正在使用的CSV文件是自动生成的,并且标题不在第一行,这就是为什么我必须跳过前4行。

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而不是隐式的-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: 或者,如果您不确定确切匹配哪个属性,请使用-LDAPFilter歧义名称解析

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: 第一个foreach循环将相同的变量用于循环变量和元素列表:

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

Don't do that. 不要那样做 PowerShell variable names aren't case-sensitive. PowerShell变量名称不区分大小写。

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 杰西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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