Author Posts

February 2, 2017 at 7:13 pm

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

February 2, 2017 at 7:16 pm

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

February 2, 2017 at 7:17 pm

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

February 2, 2017 at 7:20 pm

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]
    }
}

February 2, 2017 at 8:04 pm

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

February 2, 2017 at 8:06 pm

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.

February 2, 2017 at 8:24 pm

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