我正在尝试创建一个新的 Active Directory 用户,但首先我验证该用户不存在Get-ADUser
。
我从人力资源部门导入用户数据并构建自定义属性:
$newUsers = Import-Csv $csvFile |
Select-Object -Property @{n='EmpNum';e={$_.'Employee Number'}},
@{n='UPN';e={$_.'Email Address'}},
@{n='Alias';e={$_.'Email Address'.Split("@")[0]}} #### etc
当我循环访问 CSV 文件中的对象时,我使用 UPN 属性在 Active Directory 中搜索用户:
foreach ($newUser in $newUsers) {
$exists = Get-ADUser -Filter {UserPrincipalName -eq $newUser.UPN} -Properties * -Server $adServer -Credential $adCred
...
}
过滤器导致错误:
Get-ADUser : Property: 'UPN' not found in object of type:
'System.Management.Automation.PSCustomObject'. At
C:\Users\bphillips.NEWHOPEOFIN\Dropbox\Powershell\NewHire\AddNewDSP.ps1:50
char:15
+ $exists = Get-ADUser -Filter {UserPrincipalName -eq $newUser.UPN} -Propertie ...
我尝试这样做: -Filter {UserPrincipalName -eq $("$newUser.UPN") 但这没有帮助;我收到另一个错误
Get-ADUser : Cannot process argument because the value of argument
"path" is not valid. Change the value of the "path" argument and run
the operation again. At
C:\Users\bphillips.NEWHOPEOFIN\Dropbox\Powershell\NewHire\AddNewDSP.ps1:50
char:15
+ $exists = Get-ADUser -Filter {UserPrincipalName -eq $("$newUser.UPN")} -Prop ...
$newUser
是一个字符串,所以我不明白为什么它会导致问题。对 UserPrincipalName 进行硬编码,例如“[电子邮件受保护] /cdn-cgi/l/email-protection“ 有效,但是$newUser.UPN
行不通。**
PS C:\> $newUser.UPN.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
and
PS C:\> $newUser.UPN | gm
TypeName: System.String
$newUser.UPN
包含有效的字符串值
PS C:\> $newUser.UPN
[email protected] /cdn-cgi/l/email-protection
我需要做什么才能得到$newUser.UPN
被识别为过滤器参数的字符串?这是怎么回事我不明白?