在 Get-ADUser 过滤器参数中传递字符串会导致错误 - 在 pscustomobject 中找不到属性 [英] Passing string in Get-ADUser filter parameter causes error - property not found in pscustomobject
问题描述
我正在尝试创建一个新的 Active Directory 用户,但首先我使用 Get-ADUser
验证该用户不存在.我从我们的 HR 部门导入用户数据并构建自定义属性:
I'm trying to create a new Active Directory user, but first I verify that the user doesn't exist already with Get-ADUser
.
I import the user data from our HR department and build custom properties:
$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 中搜索用户:
When I loop through the objects from the CSV file, I use the UPN property to search for the user in 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:Usersphillips.NEWHOPEOFINDropboxPowershellNewHireAddNewDSP.ps1:50
char:15
+ $exists = Get-ADUser -Filter {UserPrincipalName -eq $newUser.UPN} -Propertie ...
我试过这样做: -Filter {UserPrincipalName -eq $("$newUser.UPN") 但这没有帮助;我收到另一个错误
I've tried doing this: -Filter {UserPrincipalName -eq $("$newUser.UPN") but that doesn't help; I get another error
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:Usersphillips.NEWHOPEOFINDropboxPowershellNewHireAddNewDSP.ps1:50
char:15
+ $exists = Get-ADUser -Filter {UserPrincipalName -eq $("$newUser.UPN")} -Prop ...
$newUser
是一个字符串,所以我不明白为什么它会导致问题.对 UserPrincipalName 进行硬编码,例如test@ourcompany.com"有效,但 $newUser.UPN
不起作用.**
$newUser
is a string, so I don't understand why it causes a problem. Hard-coding a UserPrincipalName like, "test@ourcompany.com" works, but the $newUser.UPN
won't work.**
PS C:> $newUser.UPN.GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
和
PS C:> $newUser.UPN | gm
TypeName: System.String
$newUser.UPN
包含一个有效的字符串值
$newUser.UPN
contains a valid string value
PS C:> $newUser.UPN
ypope@ourcompany.net
我该怎么做才能让 $newUser.UPN
被识别为过滤器参数的字符串?我不明白发生了什么?
What do I have to do to get $newUser.UPN
to be recognized as a string for the filter parameter? What's going on that I don't understand?
推荐答案
BNF对于过滤查询字符串 不允许表达式作为比较中的第二个操作数,只有值(强调我的):
The BNF for filter query strings does not allow expressions as the second operand in a comparison, only values (emphasis mine):
语法:
下面的语法使用 Backus-Naur 形式来展示如何使用 PowerShell 表达式语言来处理这个参数.
Syntax:
The following syntax uses Backus-Naur form to show how to use the PowerShell Expression Language for this parameter.
<过滤器>::= "{" <过滤器组件列表>"}"
<过滤器组件列表>::= <过滤器组件>|<过滤器组件><JoinOperator><过滤器组件>|<NotOperator>
<过滤器组件>::= <属性><过滤器运算符><值> |"(" <过滤器组件> ")"
<过滤器运算符>::= "-eq" |"-le" |"-ge" |"-ne" |"-lt" |"-gt"|"-大约" |"-bor" |带" |"-recursivematch" |"-like" |-不喜欢"
<JoinOperator>::= "-and" |-或"
<NotOperator>::= "-不"
<属性>::= <属性名称>|<属性的LDAPDisplayName>
<value>::= <将此值与 <attr> 进行比较通过使用指定的 <FilterOperator>>
将要比较的属性值放入一个变量中,并在比较中使用该变量.您可能还想将过滤器定义为实际字符串,只是为了清楚起见(尽管过滤器看起来不是脚本块).
Put the value of the property you want to compare against in a variable and use that variable in the comparison. You may also want to define the filter as an actual string, if only for clarity (despite what it looks like the filter is not a scriptblock).
$upn = $newUser.UPN
$exists = Get-ADUser -Filter "UserPrincipalName -eq '$upn'" ...
这篇关于在 Get-ADUser 过滤器参数中传递字符串会导致错误 - 在 pscustomobject 中找不到属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!