call a function problem

This topic contains 7 replies, has 2 voices, and was last updated by  iiMosh 3 months, 1 week ago.

  • Author
    Posts
  • #91343

    iiMosh
    Participant

    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.

    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")
        }
        
    
    
    		
    	
  • #91346

    Sam Boutros
    Participant

    Why use global variables? That's typically frowned upon..

    • #91349

      iiMosh
      Participant

      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.

    • #91352

      Sam Boutros
      Participant

      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
      

      and can be easily referenced.

    • #91355

      iiMosh
      Participant

      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.

    • #91360

      Sam Boutros
      Participant

      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)

    • #91357

      Sam Boutros
      Participant

      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..

    • #91361

      iiMosh
      Participant

      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
      }
      

You must be logged in to reply to this topic.