PowerShell是可能运行广告过滤器搜索并将其与另一个用户列表进行比较

发布于 2025-01-20 20:51:50 字数 573 浏览 1 评论 0原文

早晨,我要实现的目标是,我在下面看到了一个get -aduser -filter搜索,

但是我也希望它要做的就是将其与我拥有的用户列表进行比较,如果用户出现在搜索中希望它不要与列表1中的用户做任何事情,而要把所有的帐户移动,以固定的天数移至不同的ou,所以我可能会使这个歉意。 将不胜感激。

$Folder="C:\Temp"
$Days=10 
$ExportedReport = "$Folder\Report_10_days.csv" 
Get-ADUser -Filter {Enabled -eq $False} -SearchBase "OU=MY OU" -Properties SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-$Days)) -and ($_.LastLogonDate -ne $NULL)} | Sort | Select Name,SamAccountName,LastLogonDate | export-csv $ExportedReport -nti 

如果我使用比较对象之类的东西来实现这一目标,

Morning all, so what I am trying to achieve is that I do a Get-ADUser -Filter search seen below

but what I also want it to do is compare it to a list of users I have and if the user appears in the search I want it to not do anything with the users in List 1 but move all the accounts with the set amount of days to be moved to different ou I may be over complicating this so apologies. Any help or guidance would be appreciated

$Folder="C:\Temp"
$Days=10 
$ExportedReport = "$Folder\Report_10_days.csv" 
Get-ADUser -Filter {Enabled -eq $False} -SearchBase "OU=MY OU" -Properties SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-$Days)) -and ($_.LastLogonDate -ne $NULL)} | Sort | Select Name,SamAccountName,LastLogonDate | export-csv $ExportedReport -nti 

should I use something like Compare-Object to achieve this a little stuck on what to do next.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦初启 2025-01-27 20:51:50

因此,首先您需要导入 .csv,这样做:

$data = Import-Csv -Path "C:\folder\mydata.csv"

然后您需要循环浏览每个条目并从邮件地址获取全名(我只是假设您的 AD 对象没有电子邮件) - 并添加一些添加多个过滤器的变量:

Foreach ($entry in $data) {
$newEmail = $entry.Split("@")[0]
$fullname = $newEmail.Split(".")[1] + " " + $newEmail.Split(".")[0]
$enabled = "Enabled"
$name = "Name"
Get-ADUser -Filter {$enabled -eq $False -and $name -like $fullname} -Properties SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-$Days)) -and ($_.LastLogonDate -ne $NULL)} | Sort | Select Name,SamAccountName,LastLogonDate | export-csv $ExportedReport -nti -Append
}

我在导出 csv 中添加了“-append”,否则循环的每次迭代都会删除文件而不是附加条目。

显然,您仍然需要之前为 Get-Aduser 行定义的变量,但除此之外,这应该可以满足您的要求。

我们为 -filter 使用变量的原因是,当使用多个过滤器时,语法在解释引号中声明的内容时会遇到麻烦。在 .Split() 命令中,方括号中的数字是 split 返回的数组的索引。如果你拆分“Forename.Surname”,你可以想象它是[0].[1]。

So first you need to import your .csv, so do that:

$data = Import-Csv -Path "C:\folder\mydata.csv"

Then you need to cycle through each entry and get the full name from the mail address (I just assume your AD objects don't have the email) - and add a few variables to add multiple filters:

Foreach ($entry in $data) {
$newEmail = $entry.Split("@")[0]
$fullname = $newEmail.Split(".")[1] + " " + $newEmail.Split(".")[0]
$enabled = "Enabled"
$name = "Name"
Get-ADUser -Filter {$enabled -eq $False -and $name -like $fullname} -Properties SamAccountName,LastLogonDate | Where {($_.LastLogonDate -lt (Get-Date).AddDays(-$Days)) -and ($_.LastLogonDate -ne $NULL)} | Sort | Select Name,SamAccountName,LastLogonDate | export-csv $ExportedReport -nti -Append
}

I added "-append" to the export-csv, since otherwise each iteration of the loop would erase the file instead of appending the entry.

Obviously, you still need the variables you defined before, for the Get-Aduser line, but otherwise this should do what you wanted.

The reason we use variables for the -filter is, that the syntax has troubles with interpreting whatever is stated in quotes when using more than one filter. In the .Split() command, the number in square brackets is the index of an array that is returned from split. If you split "Forename.Surname", you can imagine it being [0].[1].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文