简体   繁体   English

Active Directory PowerShell筛选器

[英]Active Directory PowerShell Filter

I'm having issues with a script in powershell using AD Module. 我在使用AD模块的Powershell中的脚本遇到问题。

I know the general rule of thumb as to how AD commands like to receive its queries so I've wrote this script to (what I thought) would fall inline with those guidelines. 我知道关于AD命令如何接收其查询的一般经验法则,所以我已经将此脚本编写为(我认为)会符合那些准则。

$CSV=Import-Csv "c:\temp\deleteduserlist.csv"
foreach ($entry in $CSV)
{
    $filter = "{SamAccountName -like ""$($entry.username)""}"
    Get-ADObject -filter $filter
}

I basically need to be able to query and restore any of the users that have been deleted however it fails with: 我基本上需要能够查询和恢复已删除的任何用户,但是失败并显示:

Error Message: 'syntax error' at position: '1' 错误消息:位置“ 1”处出现“语法错误”

At first I was sending through the filter with single quotations like so: 最初,我通过单引号通过过滤器进行发送,如下所示:

{SamAccountName -like 'xxx'"} {SamAccountName -like'xxx'“}

However I have fixed this now. 但是,我现在已解决此问题。

One thing that puzzles me is that I can literally show the results of $filter, copy them to Get-ADObject -Filter (paste) manually and it works. 让我感到困惑的一件事是,我可以从字面上显示$ filter的结果,然后将它们手动复制到Get-ADObject -Filter(粘贴),并且可以正常工作。 Therefore I cannot understand why Powershell does not like it.. 因此,我不明白为什么Powershell不喜欢它。

Whole Error: 整个错误:

Get-ADObject : Error parsing query: '{SamAccountName -like "xxxx"}' Error M essage: 'syntax error' at position: '1'. Get-ADObject:分析查询时出错:'{SamAccountName -like“ xxxx”}'错误消息:'1'处的'语法错误'。 At C:\\temp\\GetDeleted.ps1:5 char:14 + Get-ADObject <<<< -filter $filter + CategoryInfo : ParserError: (:) [Get-ADObject], ADFilterParsing Exception + FullyQualifiedErrorId : Error parsing query: '{SamAccountName -like "xxx "}' Error Message: 'syntax error' at position: '1'.,Microsoft.ActiveD irectory.Management.Commands.GetADObject 在C:\\ temp \\ GetDeleted.ps1:5 char:14 + Get-ADObject <<<< -filter $ filter + CategoryInfo:ParserError:(:) [Get-ADObject],ADFilterParsing Exception + FullyQualifiedErrorId:错误解析查询:' {SamAccountName -like“ xxx”}'错误消息:位置“ 1”上的“语法错误”。,Microsoft.ActiveD irectory.Management.Commands.GetADObject

One way to do it is this 一种方法是

$CSV=Import-Csv "c:\temp\deleteduserlist.csv"
foreach ($entry in $CSV) {
    ## Set username to entry from csv file
    $directory_username = $entry.username

    ## Build search filter before using it, interested in username and deleted objects
    $directory_filter = {(SamAccountName -like $directory_username) -and (Deleted -eq $true)}

    ## Search for ADObject based on filter and deleted objects explicitely included in the search
    $directory_found_object = Get-ADObject -Filter $directory_filter -IncludeDeletedObjects -Properties sAMAccountName, Deleted
    foreach ($directory_object in $directory_found_object) {
        ### Execute required action on each found $directory_object
        ### Perhaps pipe $directory_object | Restore-ADObject with appropriate attribute values for restore
    }
}

$directory_filter can be of course modified to fit your needs better. 当然可以修改$ directory_filter以更好地满足您的需求。 However one challenge you will be facing still is to decide which of the found objects for given sAMAccountName should be restored. 但是,您仍将面临的一个挑战是确定应还原给定sAMAccountName的找到的对象中的哪个。 This is because any given objects can be in deleted state multiple times. 这是因为任何给定的对象都可以多次处于删除状态。 Perhaps one way to solve for that is to restore object with latest WhenCreated attribute value. 解决该问题的一种方法可能是使用最新的WhenCreated属性值还原对象。

Also I'm not sure what motivation you have to build filter beforehand. 另外,我不确定您必须事先建立过滤器的动机。 It might be useful in case you built in on the fly for different attribute values but it seems not to be the case in your example. 如果您动态构建不同的属性值,这可能会很有用,但在您的示例中似乎并非如此。 So for simplicity it can be also removed and included directly in Get-ADObject call like this 因此,为简单起见,也可以将其删除并直接包含在这样的Get-ADObject调用中

$directory_found_object = Get-ADObject -Filter {(SamAccountName -like $directory_username) -and (Deleted -eq $true)} -IncludeDeletedObjects -Properties sAMAccountName, Deleted

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

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