PowerShell的传递变量来 - 筛选与外卡 [英] Powershell Passing Variable to -Filter with Wild Card

查看:189
本文介绍了PowerShell的传递变量来 - 筛选与外卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个困难,这里就这一个,我知道它可能是一个简单的语法问题。我不知道如何给这个变量传递到code块,并请有正确认识。

  $ USER =Some.Person
 

这个工程,我期望的那样。

  GET-ADUser便有-filter {(SAM帐户当量$用户)}
 

这不

  GET-ADUser便有-filter {(通过UserPrincipalName样$用户*)}
 

试图与变化$用户*'$用户*'以及一些其他人也没有用。

最后的结果将是使用类似如下,因为我们有AD帐户,其中UPN比用户名不同,我有我需要确认仍然活跃账户用户名存在格式化值的整体上市。

  GET-ADUser便有-Filter {(通过UserPrincipalName样$用户*) - 或(SAM帐户-eq$用户)} -SearchBase - 服务器要播:3268 
 

解决方案

奇怪的行为,这不是答案,但一转以防万一; personnaly,我用 -LDAPFilter

  GET-ADUser便有-LDAPFilter(通过UserPrincipalName = $用户*)
 

有关过滤器的波兰记号是有点令人不安,在开始时,但在这里,它的过滤使用垫层协议LDAP自然的方式。

  GET-ADUser便有-LDAPFilter(|(通过UserPrincipalName = $用户*)(的samAccountName = $用户))
 

您可以获取有关此语法的详细信息的搜索过滤器语法,你还可以得到相应的过滤器的 About_ActiveDirectory_Filter


如果你真的想使用 -Filter 语法,你可以做以下(我'没那么骄傲的说):

  $ userstar =$用户*
获取-ADUser便有-Filter {(通过UserPrincipalName样$ userstar) - 或(的samAccountName样$用户)}
 

I am having a hard time here on this one and I know its probably a simple syntax problem. I don't know how to pass this variable into the code chunk and have it acknowledged correctly.

$user = "Some.Person"

This works as I would expect.

get-aduser -filter {(Samaccountname -eq $user)}

This does not

get-aduser -filter {(userprincipalname -like $user*)}

tried with variations of "$user*", "'$user*'" as well as some others to no avail.

final outcome would be use like follows because we have AD accounts where UPN is different than UserName and I have a whole listing of Username formatted values I need to confirm still exist with active accounts.

Get-ADUser -Filter {(UserPrincipalName -like "$user*") -or (SamAccountName -eq "$user")} -SearchBase "" -Server "MyServer:3268"

解决方案

Strange behaviour, this is not THE answer but a turn arround ; personnaly, I use -LDAPFilter :

Get-ADUser -LDAPFilter "(userprincipalname=$user*)"

The polish notation for the filter is a bit disconcerting at the begining, but here, it's the natural way of filtering using the underlaying protocol LDAP.

Get-ADUser -LDAPFilter "(|(userprincipalname=$user*)(samAccountName=$user))"

You can get more information about this syntax in Search Filter Syntax, you can also get corresponding filters in About_ActiveDirectory_Filter.


If you really want to use the -Filter syntax you can do the following (I'am not so proud of that):

$userstar = "$user*"
Get-ADUser -Filter {(userprincipalname -like $userstar) -or (samAccountName -like $user)}

这篇关于PowerShell的传递变量来 - 筛选与外卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆