【问题标题】:Powershell Script to query Active Directory用于查询 Active Directory 的 Powershell 脚本
【发布时间】:2019-03-13 21:07:04
【问题描述】:

我正在尝试查询多个同名 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"
}

如果找不到文件。我想将用户添加到组中,但这是踢球者。必须将该帐户添加到该帐户所属组织的违规组中。

在以下位置找到一个管理员帐户:

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 中只有一个组。

标签: powershell active-directory


【解决方案1】:

嗯,你的帖子有点混乱,没有办法真正验证,因为我没有这样的设置。

然而,查询所有 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 }
    }
}

【讨论】:

    【解决方案2】:

    似乎其中一个变量不正确,因为 PowerShell 给了我以下信息:

    Get-ADPrincipalGroupMembership:无法验证参数“身份”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后尝试该命令 再次。

    好的,根据您在 Postanote 上的帖子,这是我目前所拥有的:

    # query all users in multiple OUs
    (Get-ADOrganizationalUnit -Filter "Name -eq 'Admin-User-Accounts'") |
    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=$UserOrg,OU=ORGS,DC=domain,DC=net"
            $NonCompliantOU = "OU=Groups,OU=$UserOrg,OU=Information Assurance,OU=ORGS,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 "$UserOrg IA - Non-Compliant Users" -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 }
        }
    }
    

    【讨论】:

    • @postanote 我尝试了您的脚本并进行了与我的设置相关的修改,并且 powershell 吐出以下 Get-ADPrincipalGroupMembership :无法验证参数“身份”的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。 脚本在上面。
    【解决方案3】:

    查看原始脚本片段:

    $ous = Get-ADOrganizationalUnit -Filter "Name -eq 'Admin-User-Accounts'" 
    $ous | ForEach-Object {
        $AccountName = Get-ADUser -Filter * -SearchBase $_.DistinguishedName |
                       Select SamAccountName   # note 1
        Test-Path "\\domain.net\SYSVOL\domain.net\IA\$AccountName.pdf"   # note 2
    }
    

    注意 1:你最终会得到 $accountname.accountname 持有你的价值。我认为你会想要扩展它。 注意2:Powershell 可能会感到困惑并认为您正在寻找变量 $accountname.pdf

    相反,试试这个...

    $ous = Get-ADOrganizationalUnit -Filter "Name -eq 'Admin-User-Accounts'" 
    $ous | ForEach-Object {
        $AccountName = $(Get-ADUser -Filter * -SearchBase $_.DistinguishedName).SamAccountName 
        Test-Path "\\domain.net\SYSVOL\domain.net\IA\$($AccountName).pdf"   
    }
    

    在这里,我们将查询的 .SamAccountName 的值保存到 $AccountName,并通过添加 $($accountname) 来明确我们想要的变量,并且 .pdf 不是变量名的一部分。

    现在,请注意,这不会将结果保存在任何地方,它只会将它们闪现到屏幕上。

    【讨论】:

    • 您好,我尝试了您的脚本版本。但是,在执行“write-host $($AccountName)”时,我注意到如果用户名在同一个 OU 中,变量会在同一行吐出用户名。我需要每个用户名都在不同的行上,没有空格,否则在测试路径时,它不会正确返回正确的数据。
    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 2020-05-08
    相关资源
    最近更新 更多