Powershell 使用通配符将变量传递给 -Filter [英] Powershell Passing Variable to -Filter with Wild Card

查看:22
本文介绍了Powershell 使用通配符将变量传递给 -Filter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到了困难,我知道这可能是一个简单的语法问题.我不知道如何将此变量传递到代码块中并正确确认.

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"

这符合我的预期.

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

这个没有

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

尝试了 "$user*""'$user*'" 以及其他一些变体,但无济于事.

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

最终结果将如下使用,因为我们有 AD 帐户,其中 UPN 与用户名不同,我有一个完整的用户名格式值列表,我需要确认活动帐户仍然存在.

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"

推荐答案

奇怪的行为,这不是答案,而是转身;个人而言,我使用 -LDAPFilter :

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

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

过滤器的波兰语表示法一开始有点令人不安,但在这里,这是使用底层协议 LDAP 进行过滤的自然方式.

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))"

您可以在 搜索过滤器语法,您也可以在About_ActiveDirectory_Filter.

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

如果您真的想使用 -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 使用通配符将变量传递给 -Filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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