powershell - 您可以在 Get-ADUser 上使用 -Filter 来过滤掉空字段吗?

标签 powershell active-directory

从 Active Directory 抓取数据然后将其插入 SQL 表时,有没有办法过滤掉空字段?当我使用 -FilterGet-ADUser我认为这不是这样做的正确语法。

它没有说

The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input


$ADARRAY = (Get-ADGroupMember -Identity "Domain Users" -Recursive |
           Get-ADUser -Filter {-not (Mail -like "*")} -Properties Mail)

foreach($OBJECT in $ADARRAY) {
    $NAME = $OBJECT.Name 
    $USER = $OBJECT.SamAccountName
    $EMAIL = $OBJECT.Mail

    $INSERT = "INSERT $TABLE VALUES ('$USER','$EMAIL', '$NAME')"
    $SQL.CommandText = $INSERT 

    $SQL.ExecuteNonQuery()
}

$SQLCON.Close()

最佳答案

您收到该错误消息是因为 Get-ADUser可以 要么 从管道读取输入 使用参数 -Filter .如果你看一下 documentation您会看到 cmdlet 有 3 个参数集:

Get-ADUser
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   -Filter <String>
   [-Properties <String[]>]
   [-ResultPageSize <Int32>]
   [-ResultSetSize <Int32>]
   [-SearchBase <String>]
   [-SearchScope <ADSearchScope>]
   [-Server <String>]
   [<CommonParameters>]
Get-ADUser
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   [-Identity] <ADUser>
   [-Partition <String>]
   [-Properties <String[]>]
   [-Server <String>]
   [<CommonParameters>]
Get-ADUser
   [-AuthType <ADAuthType>]
   [-Credential <PSCredential>]
   -LDAPFilter <String>
   [-Properties <String[]>]
   [-ResultPageSize <Int32>]
   [-ResultSetSize <Int32>]
   [-SearchBase <String>]
   [-SearchScope <ADSearchScope>]
   [-Server <String>]
   [<CommonParameters>]

其中的第二个将用于读取来自 Get-ADGroupMember 的管道输入。 (通过参数 -Identity ),但是当指定参数 -Filter 时您正在强制使用第一个,它不接受您的管道输入。

此外,从您的代码来看,您实际上想要具有 mail 的用户对象。属性。

使用 Where-Object像这样过滤,代码应该做你想要的:
$ADARRAY = Get-ADGroupMember -Identity 'Domain Users' -Recursive |
           Get-ADUser -Properties Mail |
           Where-Object { $_.Mail -like '*' }

如果你真的想要没有 mail 的用户对象属性改变条件$_.Mail -like '*'$_.Mail -notlike '*' .

关于powershell - 您可以在 Get-ADUser 上使用 -Filter 来过滤掉空字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50867371/

相关文章:

php - 递归显示用户所属的所有 Active Directory 组

powershell - 导入 PSSession 在脚本中失败,在 shell 中工作

PowerShell:如何在 PowerShell 中将数组对象转换为字符串?

Azure 单点登录

java - 跨多个域的 Tomcat 集成 Windows 身份验证

java - 在 Java 中使用 MS 的 Integer8/LargeInteger?

powershell - 筛选包含美元符号的字符串

powershell - 如何在PowerShell中使用可执行脚本 block 和ApartmentState参数设置线程?

powershell - Windows PowerShell 更改标志的文本颜色

linux - 在 Debian 8.x 上使用 nslcd 进行 LDAP 用户身份验证