简体   繁体   English

Get-ADUser中带变量的通配符

[英]Wildcard with variable in Get-ADUser

Im sure this is just a syntax error, but im trying to search AD users, and I cannot figure out why this does not work: 我确定这只是一个语法错误,但我试图搜索AD用户,我无法弄清楚为什么这不起作用:

Write-Host "Enter LastName or UserName:"
$x = Read-Host
Get-ADUser -Filter { SAMAccountName -like '*$x*' } -Properties DispalyName | FT -Properties DisplayName

Just doesnt return anything. 只是不回报任何东西。 And im sure it is syntax with the "*". 我确定它是带有“*”的语法。 but not sure why. 但不知道为什么。 Thanks for any help. 谢谢你的帮助。

$x is not expanded inside the Filter scriptblock, this should do the job: $ x未在Filter脚本块中展开,这应该可以完成以下任务:

$x = 'mini'
Get-ADUser -Filter "SamAccountName -like '*$x*'" -Properties DisplayName | ft DisplayName 

DisplayName
-----------
Administrator

Alternatively, you could use ldap filter: 或者,您可以使用ldap过滤器:

Get-ADUser -LDAPFilter "(samaccountname=*$x*)"

I agree with the above poster. 我同意上面的海报。 Single quotes prevent variable expansion. 单引号可防止变量扩展。 Double quotes will work. 双引号将起作用。

PS H:\> $s = "smith"
PS H:\> Write-Host '*$s*' 
*$s*
PS H:\> Write-Host "*$s*" 
*smith*

There are still some cases where double quotes won't save you, eg with an object. 在某些情况下,双引号不能保存您,例如使用对象。

PS H:\> $psObj = New-Object PsCustomObject
PS H:\> $psobj | Add-Member -MemberType noteproperty -name s -value "smith"
PS H:\> Write-Host $psobj.s
smith
PS H:\> Write-Host "*$psobj.s*"
*@{s=smith}.s*

In that case, use string formatting: 在这种情况下,使用字符串格式:

PS H:\> Write-Host ("*{0}*" -f $psobj.s)
*smith*

try just changing this: 试着改变这个:

{ SAMAccountName -like "*$x*" }

Edit: 编辑:

this should works: 这应该有效:

$x = '*' + $(Read-Host 'Enter name') + '*'
get-aduser -Filter {name -like $x} -Properties DispalyName | FT -Properties DisplayName

Well, this works, just a little slow: 嗯,这个工作,只是有点慢:

$x = Read-Host "Enter Name"

Get-ADUser -Filter * -Properties SAMAccountName | ? { $_.SAMAccountName -like "*$x*" } | Format-Table -Property SAMAccountName

Little differnt approach, but works non the less! 一点点不同的方法,但工作不少! thanks for all the help. 感谢所有的帮助。

I ran into this in my powershell learning curve when using an object. 在使用对象时,我在PowerShell学习曲线中遇到了这个问题。

I read in a csv file of user id's and needed to search/match/filter on them, and as put before double quotes did not work there. 我读了一个用户id的csv文件,需要对它们进行搜索/匹配/过滤,并且在双引号之前没有用。

My solution was to use ToString() method on my object and set it to a scalar variable then use that variable in the filter. 我的解决方案是在我的对象上使用ToString()方法并将其设置为标量变量,然后在过滤器中使用该变量。 Worked great. 工作得很好。

$user_records=Import-CSV .\20140430.userids.csv

The table had three columns "Name, ID, Department". 该表有三列“名称,ID,部门”。 To get them in a search filter on their user id I used: 为了让他们在用户ID的搜索过滤器中使用我:

foreach ( $thisrow in $user_records ) {
$thisuser=$thisrow.Username.ToString()
Get-ADUser -Filter {SamAccountName -eq $thisuser} -SearchBase "OU=Domain Users,DC=topofthecharts,DC=com" -Properties Department 

} }

This avoided my expansion and quotes troubles completely. 这避免了我的扩张并完全引用了麻烦。

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

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