在Get-ADUser过滤器参数中传递字符串会导致错误-在pscustomobject中找不到属性 [英] Passing string in Get-ADUser filter parameter causes error - property not found in pscustomobject
问题描述
我正在尝试创建一个新的Active Directory用户,但首先我要使用 Get-ADUser
验证该用户是否不存在。
我从人力资源部门导入用户数据并构建自定义属性:
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:\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),但这没有帮助;我收到另一个错误
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:\Users\bphillips.NEWHOPEOFIN\Dropbox\Powershell\NewHire\AddNewDSP.ps1:50
char:15
+ $exists = Get-ADUser -Filter {UserPrincipalName -eq $("$newUser.UPN")} -Prop ...
$ newUser
是一个字符串,所以我不明白为什么会引起问题。像 test@ourcompany.com这样的UserPrincipalName可以进行硬编码,但是 $ 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
包含有效的字符串值
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.
< filter> :: = {< FilterComponentList> }
< FilterComponentList> :: =< FilterComponent> | < FilterComponent> < JoinOperator> < FilterComponent> | < NotOperator> < FilterComponent>
< FilterComponent> :: = < attr> < FilterOperator> < value> | (< FilterComponent>)
< FilterOperator> :: = -eq | -le | -ge | -ne | -lt | -gt | -大约 | -bor | 乐队 | -递归匹配 | -like | -notlike
< JoinOperator> :: = -and | -or
< NotOperator> :: = -not
< attr> :: =< PropertyName> | <属性的LDAPDisplayName>
< value> :: =<将该值与< attr>进行比较。通过使用指定的< FilterOperator>>
<filter> ::= "{" <FilterComponentList> "}"
<FilterComponentList> ::= <FilterComponent> | <FilterComponent> <JoinOperator> <FilterComponent> | <NotOperator> <FilterComponent>
<FilterComponent> ::= <attr> <FilterOperator> <value> | "(" <FilterComponent> ")"
<FilterOperator> ::= "-eq" | "-le" | "-ge" | "-ne" | "-lt" | "-gt"| "-approx" | "-bor" | "-band" | "-recursivematch" | "-like" | "-notlike"
<JoinOperator> ::= "-and" | "-or"
<NotOperator> ::= "-not"
<attr> ::= <PropertyName> | <LDAPDisplayName of the attribute>
<value>::= <compare this value with an <attr> by using the specified <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屋!