call a function problem

Hi all,

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.


#User Notifications
    # 699
    $global:my699_NotificationSubject="699 Mail Notification text from variable"
    $global:my849_NotificationSubject="849 Mail Notification text from variable"

    function Send_InfoMail {
     #during testing I use Write-host instead of SEnd-Mailmessage...   
     Write-host "to $mailaddress with subject  $subject "

$AllMailboxes = @()

$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){
    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")

Why use global variables? That’s typically frowned upon…

Hi Sam,

was just a try to see if this could help me. The main question is how I pass over that “assmbled variable” to that function.

I recommend building your all your data into $AllMailboxes array of PS objects like:

$MyIDList = @(699,849) 
$AllMailboxes = $MyIDList | foreach {
        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

and can be easily referenced.

Hi Sam,

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.

I respectfully disagree. You think the main question is how to use a variable variable-name, the answer of which is:

# Using a variable variable-name

$Cat = 'blue'

$ccc = 'c'
$aaa = 'a'
$ttt = 't'

$Cat # evaluates to 'blue'

(Get-Variable -Name "$ccc$aaa$ttt").Value # Also evaluates to 'blue'

In my humble opinion you need to revisit data structures and variable scopes…

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)

Hi Sam,

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.

foreach ($Mailbox in $AllMailboxes){
    $DomainNumber = $mailbox.Domain.Replace(".loc","").Replace("my-","")
    $MAilSubject = '_NotificationSubject'
    $MAilBody= '_NotificationBody'

Send_InfoMail -mailaddress $Mailbox.Mailaddress -subject (Get-Variable -Name "$DomainNumber$MAilSubject").Value -body (Get-Variable -Name "$DomainNumber$MAilBody").Value