call part of a function?

Tagged: 

This topic contains 6 replies, has 2 voices, and was last updated by  Tony Antony 7 months, 3 weeks ago.

  • Author
    Posts
  • #63411

    Tony Antony
    Participant

    Hello,

    I have a function below. so if I call it,

    Get_Membership $Get_Email

    it works fine.

    My question is, what if I want to get only $Look_Up from the function below without creating a different function just to get that?

    function Get_Membership ([string] $email)
    { 
        $Read_INI = Get-Content $INI     
        $email =    Find_User_Index $email
        $Line_Num = ($Read_INI | Select-String -AllMatches "^\d+=$email$").LineNumber
        foreach ($Items in $Line_Num)        
        { 
            $GroupCalendars= ($Read_INI | Select-String -AllMatches "[GroupCalendars]" -SimpleMatch).LineNumber    
            $Users= ($Read_INI | Select-String -AllMatches "[Users]" -SimpleMatch).LineNumber - 2
            $Calender_List = $Read_INI[$GroupCalendars..$Users]        
            $Look_Up = $Read_INI[0..($Items- 1)]                  
            $Get_Cal_Memb = ($Look_Up | Select-String -AllMatches "[MembersOf" -SimpleMatch | Select-Object -Last 1)
            $Cal_Index = $Get_cal_Memb -replace '\D+'             
            $Calender_List[$Cal_Index- 1]
        }
    }     

    Thank you,

    Tony

  • #63414

    Don Jones
    Keymaster

    You would rewrite the function to return an object, having the properties you require. Consider Get-Service – you don't have separate commands for getting a name and a status; one command returns objects that have both pieces of information

  • #63415

    Don Jones
    Keymaster

    I'm also a bit confused on the underscore in the function name, as an aside.

    • #63417

      Tony Antony
      Participant

      So in this example, would I do this? Line 11.

      Just using underscore as a person preference. Should I be using a dash?

      function Get_Membership ([string] $email)
      { 
          $Read_INI = Get-Content $INI     
          $email =    Find_User_Index $email
          $Line_Num = ($Read_INI | Select-String -AllMatches "^\d+=$email$").LineNumber
          foreach ($Items in $Line_Num)        
          { 
              $GroupCalendars= ($Read_INI | Select-String -AllMatches "[GroupCalendars]" -SimpleMatch).LineNumber    
              $Users= ($Read_INI | Select-String -AllMatches "[Users]" -SimpleMatch).LineNumber - 2
              $Calender_List = $Read_INI[$GroupCalendars..$Users]        
              return $Look_Up = $Read_INI[0..($Items- 1)]                  
              $Get_Cal_Memb = ($Look_Up | Select-String -AllMatches "[MembersOf" -SimpleMatch | Select-Object -Last 1)
              $Cal_Index = $Get_cal_Memb -replace '\D+'             
              $Calender_List[$Cal_Index- 1]
          }
      }
  • #63424

    Don Jones
    Keymaster

    You should. You want to follow native patterns, not create new ones.

    And no, you'd probably not use return anyway, as it's misleading outside of a class. This is all in "The PowerShell Scripting and Toolmaking Book," but briefly:

    $props = @{'Property1'=$value1
               'Property2'=$value2}
    New-Object -Type PSObject -Prop $props
    

    Just add in however many bits of info you intend to output (here, I've done two, named Property1 and Property2). That'll output an object. Whatever runs your function would refer to the desired property. E.g.,

    $result = My-Function
    $result.Property1
    
  • #63427

    Don Jones
    Keymaster

    And BTW, having co-authored it, I do heartily recommend the book – http://leanpub.com/powershell-scripting-toolmaking. It's obvious you have some prior programming experience, and it's going to lead you down a dark path with PowerShell. The book is intended to get you on the right page with your approach, so that you're working WITH the shell, instead of against it.

  • #63430

    Tony Antony
    Participant

    Thanks Don, I do have some basic programming experience, but not professional. I'm the only one at my company that does stuff in PowerShell, so I can't ask a live person a question. I'll read your book.

    Thanks again,

    Tony

You must be logged in to reply to this topic.