从筛选的组列表

时间:2017-07-24 19:07:26

标签: powershell active-directory

在任何有关在线论坛的第一个问题上与我联系。 =]

我的意图是使用PowerShell创建一个包含以下信息的csv:

  • 组名(仅打印名称与文本字符串匹配的组#34; O365 ")*
  • 用户是该群组的成员。
  • 每个成员的用户电子邮件是什么

目前,我的脚本如下。

    Get-ADGroup -filter {name -like "O365*"} -properties GroupCategory | 
    Select-Object -ExpandProperty name | 
    get-adgroupmember | 
    where {$_.objectclass -eq 'user'} |
    Get-ADUSer -properties Displayname,Description,Mail |
    Select Name,Displayname,Mail

这个结果为我提供了:

  • 第一栏为Displayname
  • 1列邮件

我觉得好像在研究了一段时间之后我一次又一次地错过了答案。大多数人似乎满足于在不过滤ADGroup cmdlet的情况下执行此类似任务,但这对我来说需要很长时间。

作为一个新手,我很高兴听到任何人花时间回答。 谢谢。

1 个答案:

答案 0 :(得分:1)

因此,您希望作为组成员的用户名称匹配“O365 *并且他们需要显示为

@Component
public class RetryCompoundTriggerAdvice extends AbstractMessageSourceAdvice {

    private final static Logger logger = LoggerFactory.getLogger(RetryCompoundTriggerAdvice.class);

    private final CompoundTrigger compoundTrigger;

    private final Trigger override;

    private final ApplicationProperties applicationProperties;

    private final Mail mail;

    private int attempts = 0;

    private boolean expectedMessage;
    private boolean inProcess;

    public RetryCompoundTriggerAdvice(CompoundTrigger compoundTrigger, 
            @Qualifier("secondaryTrigger") Trigger override, 
            ApplicationProperties applicationProperties,
            Mail mail) {
        this.compoundTrigger = compoundTrigger;
        this.override = override;
        this.applicationProperties = applicationProperties;
        this.mail = mail;
    }

    @Override
    public boolean beforeReceive(MessageSource<?> source) {
        logger.debug("!inProcess is " + !inProcess);
        return !inProcess;
    }

    @Override
    public Message<?> afterReceive(Message<?> result, MessageSource<?> source) {

        if (expectedMessage) {
            logger.info("Received expected load file. Setting cron trigger.");
            this.compoundTrigger.setOverride(null);
            expectedMessage = false;
            return result;
        }

        final int  maxOverrideAttempts = applicationProperties.getMaxFileRetry();

        attempts++;
        if (result == null && attempts < maxOverrideAttempts) {
            logger.info("Unable to find file after " + attempts + " attempt(s). Will reattempt");
            this.compoundTrigger.setOverride(this.override);
        } else if (result == null && attempts >= maxOverrideAttempts) {
            String message = "Unable to find daily file" + 
                    " after " + attempts + 
                    " attempt(s). Will not reattempt since max number of attempts is set at " + 
                    maxOverrideAttempts + "."; 
            logger.warn(message);
            mail.sendAdminsEmail("Missing Load File", message);
            attempts = 0;
            this.compoundTrigger.setOverride(null);
        } else {
            attempts = 0;
            // keep periodically checking until we are certain
            // that this message is the expected message
            this.compoundTrigger.setOverride(this.override);
            inProcess = true;
            logger.info("Found load file");
        }
        return result;
    }

    public void foundExpectedMessage(boolean found) {
        logger.debug("Expected message was found? " + found);
        this.expectedMessage = found;
        inProcess = false;
    }

}

简单地看一下这个问题,人们可以认为下面的代码可以解决这个问题而且它有效但是有一个问题......

Group.Name | User.DisplayName | User.Mail

如果o365组包含其他组,则不会包含这些子组的成员。

输出示例

cls
$ErrorActionPreference = "stop"
#$VerbosePreference = "Continue"
#$DebugPreference = "Continue"

Write-Verbose "Retrieving the ADGroups"
$Groups = Get-ADGroup -filter {name -like "O365*"}  
Write-Debug  ($Groups | Select Name | Format-Table | Out-String)

$UserList = @()   

Foreach ($Group in $Groups) {
    Write-Verbose "Processing Group $Group"
    $GroupMembers = $Group | Get-ADGroupMember
    Write-Debug  "GroupMembers:  $($GroupMembers | Select Name | Format-Table | Out-String)"

    Foreach ($GroupMember in $GroupMembers) {
        Write-Verbose "Processing GroupMember $GroupMember"
        Write-Debug  "GroupMember $($GroupMember | Out-String)"

        if (!($GroupMember.objectClass -eq "user")) {

            Write-Warning "GroupMember $($GroupMember) is not of type 'User', Skipping"
            Continue

        }

        else {

            Try {
                $UserList += ($GroupMember | Get-ADUSer -properties Mail | Select @{N="Group"; E={$Group.Name}}, Name, Mail)
                Write-Verbose "user $GroupMember has been added to the list."
            }

            Catch {
                Write-Warning "An error ocurd while adding user $GroupMember to the list."
                Continue
            }

        }
    }
}

$UserList