You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RFI: Is it possible to use Sub-Attributes and foreach loops in templates? #218
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 ($domainin$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=$_.NameFirstName=$firstUser.FirstNameLastName=$firstUser.LastNameUserList=$_.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
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:
# 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.
Activity
FriedrichWeinmann commentedon Jan 12, 2026
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:
Then inside of your scriptblock:
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.