I have a bigger script, my pain part of that script currently… the follwing…
I defined global variables for differnt subjects and bodys.
Calling the function “Send_InfoMail” works if I use the variables…bit if I try to “build/create” my varible only the “string” passed and not the “variable” them self…
Could you please give me an adivse to realize that… thanks a lot.
Clear-Host
#User Notifications
# 699
$global:my699_NotificationSubject="699 Mail Notification text from variable"
#849
$global:my849_NotificationSubject="849 Mail Notification text from variable"
function Send_InfoMail {
param(
[parameter(Position=0)]
$mailaddress,
[parameter(Position=1)]
$subject,
[parameter(Position=2)]
$body
)
#during testing I use Write-host instead of SEnd-Mailmessage...
Write-host "to $mailaddress with subject $subject "
}
$AllMailboxes = @()
$AllMailboxes.Clear()
$obj = New-Object PSObject
$obj | Add-Member NoteProperty -name "Mailaddress" -value "Test-699@test.loc" -Force
$obj | Add-Member NoteProperty -name "Domain" -value "my-699.loc" -Force
$AllMailboxes += $obj
$obj = New-Object PSObject
$obj | Add-Member NoteProperty -name "Mailaddress" -value "Test-849@test-loc" -Force
$obj | Add-Member NoteProperty -name "Domain" -value "my-849.loc" -Force
$AllMailboxes += $obj
foreach ($Mailbox in $AllMailboxes){
#Work
Write-Host "Works with passing the variable:" -ForegroundColor Green
Send_InfoMail -mailaddress $Mailbox.Mailaddress -subject $my699_NotificationSubject
Write-Host "Don't work with assamling the subject variable" -ForegroundColor Red
#Mailbox.Domain contain a value like "my-699.loc" or "my-399.loc"
#don't work, Powershell pass the "string" and not the variable..
Send_InfoMail -mailaddress $Mailbox.Mailaddress -subject $("$"+ $Mailbox.Domain.Replace("-","").Replace(".loc","") + "_NotificationSubject")
}
I recommend building your all your data into $AllMailboxes array of PS objects like:
$MyIDList = @(699,849)
$AllMailboxes = $MyIDList | foreach {
[PSCustomObject][Ordered]@{
ID = $PSItem
Mailaddress = "Test-$PSItem@test.loc"
Domain = "my-$PSItem.loc"
Subject = "$PSItem Mail Notification text from variable"
}
}
Your data will look like:
PS D:\Sandbox> $AllMailboxes
ID Mailaddress Domain Subject
-- ----------- ------ -------
699 Test-699@test.loc my-699.loc 699 Mail Notification text from variable
849 Test-849@test.loc my-849.loc 849 Mail Notification text from variable
That would work for that example with two entries. But my “Allmailboxes” contain over 6000 user. And my Idea was depending from which domain they are a former defined subject and body for that domain will be used in that function.
For the actual use case, you would read the data from whatever source, being the output of another cmdlet, or CSV file, or Excel sheet, or database, then
Assemble a data structure that suits the processing need, then
Process your data, then
spit out the desired output
The step of assembling a data structure that bridges the gap between input format and the format/structure optimum for processing is critical and should not be overlooked in my opinion.
(@Will the blessed ‘confidential’ bot keeps removing my code, which is sanitized BTW)
Thanks for your hints and tips. I’ll think about the “re-deign” of the data structure…
For my current path I was able to use your hint with the “Get-Variable”… of course it doesn’t look so nice.