powershell - 用于查询 Active Directory 的 Powershell 脚本
问题描述
我正在尝试查询多个同名 OU 中的所有用户。获取SamAccountName
属性,然后在特定位置检查具有该名称的文件。
这是我到目前为止所拥有的:
$ous = Get-ADOrganizationalUnit -Filter "Name -eq 'Admin-User-Accounts'"
$ous | ForEach-Object {
$AccountName = Get-ADUser -Filter * -SearchBase $_.DistinguishedName |
Select SamAccountName
Test-Path "\\domain.net\SYSVOL\domain.net\IA\$AccountName.pdf"
}
如果找不到文件。我想将用户添加到组中,但这是踢球者。必须将该帐户添加到该帐户所属组织的不合规组。
IE 下找到的管理员帐户:
OU=Admin-User-Accounts,OU=Administration,OU=ORG1,OU=ORGS,DC=domain,DC=net
将添加到名为“ORG1 IA - Non-Compliant Users”的组中,该组位于:
OU=Groups,OU=ORG1,OU=Information Assurance,OU=ORGS,DC=domain,DC=net
解决方案
好吧,您的帖子有点令人困惑,并且无法真正验证,因为我没有这样的设置。
然而,查询所有OU或企业中的用户是一件很平常的事情。
但是,OU 名称与任何其他 AD 对象名称一样,必须是唯一的。因此,在单个 AD 林/域中查询相同的 OU 名称不是一件事情。如果您的意思是向每个 OU 查询相同的用户名,那么就可以了。
通过逐步解释您已布置的用例的解释方式。
(尽管也许您想编辑您的帖子以使其更清晰,无论如何对我来说...)
使用伪代码,然后尝试将其映射出来......并且没有真正的方法来确定您的帖子/示例中的几件事是什么意思。所以,下面是一个粗略的第一个例子,说明我将如何处理这个......再次,这是未经测试的,所以,我把作业留给你。
# query all users in multiple OUs
(Get-ADOrganizationalUnit -Filter *).DistinguishedName |
ForEach{
# Collect all members of the current OU
$AccountNames = Get-ADUser -SearchBase $PSItem -Filter *
# Process each member in the current OU collection
ForEach($AccountName in $AccountNames)
{
"Processing $($AccountName.SamAccoutnName)`n"
# Initialize properties needed for processing
$UserOrg = $AccountName.DistinguishedName.split(",")[1]
$MemberCheckOU = "OU=Admin-User-Accounts,OU=Administration,OU=ORG1,OU=$UserOrg,DC=domain,DC=net"
$NonCompliantOU = "OU=Groups,OU=ORG1,OU=Information Assurance,OU=$UserOrg,DC=domain,DC=net"
# Validate user file existence for the current user
If(-Not (Test-Path -LiteralPath "\\domain.net\SYSVOL\domain.net\IA\$($AccountName.SamAccoutnName).pdf)"))
{
# if no file Process the user groupmebership modification
"Processing $($AccountName.SamAccoutnName)"
# Notify that the file was not found and processing is required
Write-Warning -Message "$($($AccountName.SamAccoutnName).pdf) not found. Process group modify actions"
# If the current user is in the MemberCheckOU, add to the NonCompliantOU
If(Get-ADPrincipalGroupMembership -Identity $($AccountName.SamAccoutnName) | Where-Object -Property DistinguishedName -Match $MemberCheckOU )
{ Add-ADGroupMember -Identity $NonCompliantOU -Members $($AccountName.SamAccoutnName) }
Else
{
# Do something else
}
}
Else
{
# Notify that the file was found and no processing required
Write-Host "$($AccountName.pdf) found. No further actions taken" -ForegroundColor Green }
}
}
推荐阅读
- python - 如何对views.py 进行编码,以便表单数据存储在我的数据库(/admin)中?使用 Django (v2)
- python - 使用 Python/Selenium 访问 iframe 内的链接并获取 url
- c - opencl中循环的时间测量
- javascript - 在 Ionic 中导入 TranslateModule
- jboss - 检测到无效的控制台附加配置,控制台流在 Jboss 5.1 中循环
- c - 如何获取磁盘的 INODE 扇区号。C、Linux?
- kubernetes - Kubernetes - kubectl 或 istioctl 列出所有内部 dns
- vba - Powerpoint VBA 插入图像并更改大小
- uwp - 桌面桥应用程序启动和本地化问题
- php - pdf2text 转换器插件返回不可读的格式