Cel*_*ppy 5 powershell active-directory
从 Active Directory 获取数据然后将其插入 SQL 表时,有没有办法过滤掉空字段?当我使用-Filteron时Get-ADUser,我认为这不是执行此操作的正确语法。
它失败说
输入对象无法绑定到命令的任何参数,因为该命令不采用管道输入,或者输入及其属性与采用管道输入的任何参数都不匹配
$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()
Run Code Online (Sandbox Code Playgroud)
您收到该错误消息是因为Get-ADUser可以从管道读取输入或使用参数-Filter。如果您查看文档,您会发现该 cmdlet 有 3 个参数集:
Get-ADUser
[-AuthType <ADAuthType>]
[-Credential <PSCredential>]
-Filter <String>
[-Properties <String[]>]
[-ResultPageSize <Int32>]
[-ResultSetSize <Int32>]
[-SearchBase <String>]
[-SearchScope <ADSearchScope>]
[-Server <String>]
[<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)
Get-ADUser
[-AuthType <ADAuthType>]
[-Credential <PSCredential>]
[-Identity] <ADUser>
[-Partition <String>]
[-Properties <String[]>]
[-Server <String>]
[<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)
Get-ADUser
[-AuthType <ADAuthType>]
[-Credential <PSCredential>]
-LDAPFilter <String>
[-Properties <String[]>]
[-ResultPageSize <Int32>]
[-ResultSetSize <Int32>]
[-SearchBase <String>]
[-SearchScope <ADSearchScope>]
[-Server <String>]
[<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)
其中第二个将用于Get-ADGroupMember(通过参数-Identity)读取管道输入,但是在指定参数时,-Filter您将强制使用第一个,它不接受您的管道输入。
另外,从您的代码来看,您实际上希望用户对象具有属性mail。
使用Where-Object这样的过滤器,代码应该执行您想要的操作:
$ADARRAY = Get-ADGroupMember -Identity 'Domain Users' -Recursive |
Get-ADUser -Properties Mail |
Where-Object { $_.Mail -like '*' }
Run Code Online (Sandbox Code Playgroud)
如果您确实想要没有属性的用户对象mail,请将条件更改$_.Mail -like '*'为$_.Mail -notlike '*'.
| 归档时间: |
|
| 查看次数: |
7935 次 |
| 最近记录: |