簡體   English   中英

用於查詢Active Directory的Powershell腳本

[英]Powershell Script to query Active Directory

我試圖查詢具有相同名稱的多個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-不合規用戶”的組中:

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 }
    }
}

似乎其中一個變量是錯誤的,因為PowerShell為我提供了以下內容:

Get-ADPrincipalGroupMembership:無法驗證參數'Identity'上的參數。 參數為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 }
    }
}

查看原始腳本片段:

$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不是變量名的一部分。

現在,還請注意,這不會將結果保存在任何地方,只會將其刷新到屏幕上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM