【问题标题】:Issue With Powershell Loop IterationPowershell 循环迭代的问题
【发布时间】:2020-05-26 08:26:00
【问题描述】:

当尝试运行以下代码时,它似乎在我的初始 foreach 循环中运行了两次。我没看到什么?感谢您的帮助。

$DC = Get-ADDomainController
$OUs = Get-ADOrganizationalUnit -Filter 'Name -eq "test"'
$TimeStamp = get-date -format D
$description = "Disabled on " + $TimeStamp
$canNotDisableUser =  Get-ADGroupMember -Identity DoNotDisableUsers -Recursive | Select -ExpandProperty Name
$accounts = $null

    # Search for User Accounts inactive for XX Days and Disable if not in DoNotDisable Security Group
    $accounts = Search-ADAccount -SearchBase $OU -AccountInactive -TimeSpan ([timespan]90d) -UsersOnly
        foreach($account in $accounts){
        If ($canNotDisableUser -notmatch $account.Name){
         Disable-ADAccount -Identity $account.DistinguishedName -Verbose
        }

    # Disable Protected from Accidental Deletion from OU
    Get-ADOrganizationalUnit -LDAPFilter '(name=*)'  -SearchBase $OU.DistinguishedName -Server $DC | Set-ADObject -ProtectedFromAccidentalDeletion:$false -Verbose -WhatIf


    # Move Disabled Users to Disabled Users OU & Add Timestamp to Description
    Search-ADAccount –AccountDisabled –UsersOnly –SearchBase $OU.DistinguishedName | Foreach-object {
    Set-ADUser $_ -Description $description -Verbose -WhatIf
    Move-ADObject $_ –TargetPath “OU=Disabled Users, DC=xxx,DC=net” -Verbose -WhatIf
    }

    # Enable Protected from Accidental Deletion from OU
    Get-ADOrganizationalUnit -LDAPFilter '(name=*)'  -SearchBase $OU.DistinguishedName -Server $DC | Set-ADObject -ProtectedFromAccidentalDeletion:$true -Verbose -WhatIf
}

【问题讨论】:

  • 您是否尝试过使用断点来查看为什么它会像您所说的那样循环两次
  • 我会研究如何做到这一点。

标签: powershell loops foreach active-directory powershell-5.0


【解决方案1】:

在解决此类问题时可以节省大量时间的一件事是“缩进”。养成始终确保它们正确缩进的习惯。

# Search for User Accounts inactive for XX Days and Disable if not in DoNotDisable Security Group
    $accounts = Search-ADAccount -SearchBase $OU -AccountInactive -TimeSpan ([timespan]90d) -UsersOnly
    foreach($account in $accounts){
        If ($canNotDisableUser -notmatch $account.Name){
            Disable-ADAccount -Identity $account.DistinguishedName -Verbose
        }

     ### YOU probably intend to close the foreach loop here. If so, Move the LAST brace to this place.

        # Disable Protected from Accidental Deletion from OU
        Get-ADOrganizationalUnit -LDAPFilter '(name=*)'  -SearchBase $OU.DistinguishedName -Server $DC | Set-ADObject -ProtectedFromAccidentalDeletion:$false -Verbose -WhatIf


        # Move Disabled Users to Disabled Users OU & Add Timestamp to Description
        Search-ADAccount –AccountDisabled –UsersOnly –SearchBase $OU.DistinguishedName | Foreach-object {
            Set-ADUser $_ -Description $description -Verbose -WhatIf
            Move-ADObject $_ –TargetPath “OU=Disabled Users, DC=xxx,DC=net” -Verbose -WhatIf
        }

        # Enable Protected from Accidental Deletion from OU
        Get-ADOrganizationalUnit -LDAPFilter '(name=*)'  -SearchBase $OU.DistinguishedName -Server $DC | Set-ADObject -ProtectedFromAccidentalDeletion:$true -Verbose -WhatIf
    }

已更正

    # Search for User Accounts inactive for XX Days and Disable if not in DoNotDisable Security Group
    $accounts = Search-ADAccount -SearchBase $OU -AccountInactive -TimeSpan ([timespan]90d) -UsersOnly
    foreach($account in $accounts){
        If ($canNotDisableUser -notmatch $account.Name){
            Disable-ADAccount -Identity $account.DistinguishedName -Verbose
        }
    }

    # Disable Protected from Accidental Deletion from OU
    Get-ADOrganizationalUnit -LDAPFilter '(name=*)'  -SearchBase $OU.DistinguishedName -Server $DC | Set-ADObject -ProtectedFromAccidentalDeletion:$false -Verbose -WhatIf


    # Move Disabled Users to Disabled Users OU & Add Timestamp to Description
    Search-ADAccount –AccountDisabled –UsersOnly –SearchBase $OU.DistinguishedName | Foreach-object {
        Set-ADUser $_ -Description $description -Verbose -WhatIf
        Move-ADObject $_ –TargetPath “OU=Disabled Users, DC=xxx,DC=net” -Verbose -WhatIf
    }

    # Enable Protected from Accidental Deletion from OU
    Get-ADOrganizationalUnit -LDAPFilter '(name=*)'  -SearchBase $OU.DistinguishedName -Server $DC | Set-ADObject -ProtectedFromAccidentalDeletion:$true -Verbose -WhatIf

【讨论】:

  • Move Disabled Users to Disabled Users OU & Add Timestamp to Description 被执行了两次...对于$accounts 中的每个$account 执行一次
猜你喜欢
  • 2016-01-14
  • 1970-01-01
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-04
  • 2021-11-25
  • 2015-10-06
相关资源
最近更新 更多