Skip to content

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

Open

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

Activity

  1. FriedrichWeinmann commented on Jan 12, 2026

    @FriedrichWeinmann
    Member

    Sorry about the terribly late response!
    This is possible right now, but if you wish to access variables you prepared beforehand, you will basically have to do one of two options:

    • Use global variables
    • Provide it as a parameter when invoking the template:
    Invoke-PSMDTemplate UserAccountList -Parameters @{ UserList = $userList }

    Then inside of your scriptblock:

    # Hello þFirstNameþ þLastNameþ,
    
    Below you will find a list of your accounts grouped by domain:
    
    
    ## Accounts by Domain
    þ{$Parameters.UserList | ForEach-Object {
        "### Domain: $($_.Domain)"
        "- UPN: $($_.UPN)"
    } | Join-String -Separator "`n"}þ
    
    Best regards,
    Your IT Team

    Note: In the current state, scriptblocks are executed exactly once per template invocation - you cannot dynamically create multiple files through it, or run the same code with different datasets.
    vNEXT has a few plans for that, but I don't know (yet) how exactly that will end up looking and how much of it makes it into the first release, vs. what will come later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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