Skip to content

RFI: Is it possible to use Sub-Attributes and foreach loops in templates? #218

@Callidus2000

Description

@Callidus2000

Hi,
I'm developing a template based mail library. As one example I've got the following "data gatherer":

$forest = Get-ADForest
$domains = $forest.Domains

$allUsers = foreach ($domain in $domains) {
    Get-ADUser -Server $domain -Filter "mail -like '*'" -Properties givenName,surname,mail,userPrincipalName |
        Select-Object @{Name='Domain';Expression={$domain}},
                      @{Name='FirstName';Expression={$_.givenName}},
                      @{Name='LastName';Expression={$_.surname}},
                      @{Name='Mail';Expression={$_.mail}},
                      @{Name='UPN';Expression={$_.userPrincipalName}}
}

# Group by mail address and create array with required structure
$userArray = $allUsers | Group-Object -Property Mail | ForEach-Object {
    $firstUser = $_.Group | Select-Object -First 1
    [PSCustomObject]@{
        Mail      = $_.Name
        FirstName = $firstUser.FirstName
        LastName  = $firstUser.LastName
        UserList  = $_.Group
    }
}

I'd like to use a template which creates a mail with a list of all users, but the loop is the problem (see it as pseudo code):

# Hello þFirstNameþ þLastNameþ,

Below you will find a list of your accounts grouped by domain:


## Accounts by Domain
þ{UserList | ForEach-Object {
    "### Domain: $($_.Domain)"
    "- UPN: $($_.UPN)"
} | Join-String -Separator "`n"}þ

Best regards,
Your IT Team

-Sascha aka callidus2000

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions