none
[PowerShell] - Return value from a function RRS feed

  • Question

  • Hello everybody!

    INFOS{

    -PowerShell 2.0

    -win 7 ultimate 64-bit VM

    -Exchange managementshell installed

    }

    PROBLEM{

    Ive got a function called 'GetSMTPAddresses'.

    Which doest not more then:

    function GetSMTPAddresses ($LogonName){
    	$MailBox = Get-Mailbox $LogonName
    	$Addresses = $MailBox.EmailAddresses
    	return $Addresses 
    }
    

    I Use this function like this:

    $Addresses = GetSMTPAddresses $LogonName
    	
    $Addresses.Remove($SelectedItem)
    	
    	
    Set-Mailbox -Identity $LogonName -EmailAddresses $Addresses
    

    but i get an error:

    ERROR: Method invocation failed because [System.Object[]] doesn't contain a method named 'Remove'.

     

    but if i do it without the function, i can use the method Remove.

    how does that come?

     

    Friday, May 13, 2011 10:49 AM

Answers

  • Does this work better?

    function GetSMTPAddresses ($LogonName){
     $MailBox = Get-Mailbox $LogonName
     $Addresses = $MailBox.EmailAddresses
     return ,$Addresses
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, May 13, 2011 12:58 PM
    Moderator

All replies

  • Try Changing the Return value like this:

    function GetSMTPAddresses ($LogonName){
    	$MailBox = Get-Mailbox $LogonName
        return $MailBox.EmailAddresses
    }
    

    And then Use the Function the Same way.

    I believe that you will get the result you need.

     

    Good Luck,


    Assaf Miron https://sites.google.com/site/AssafMiron
    Friday, May 13, 2011 12:13 PM
  • thanks for your answer!

     

    tested it right away and the same result as before. =/

    the return changes the value type from ' Microsoft.Exchange.Data.ProxyAddressBaseCollection' to 'system.array'

     


    Friday, May 13, 2011 12:35 PM
  • Does this work better?

    function GetSMTPAddresses ($LogonName){
     $MailBox = Get-Mailbox $LogonName
     $Addresses = $MailBox.EmailAddresses
     return ,$Addresses
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Friday, May 13, 2011 12:58 PM
    Moderator
  • Works perfect!

    thank you verry much!

     

    but can you explain me, what this ',' is doing?

    Friday, May 13, 2011 1:05 PM
  • Hi,

    The leading comma "," forces the PowerShell function to return the value as an array.

    Bill

    Friday, May 13, 2011 2:31 PM
    Moderator
  • thank you!

    are there some documentation about this?

    would like to know more about this.

    Friday, May 13, 2011 2:45 PM
  • Thanks for the info. I was pulling out my hair looking for this.  Ah yes, a single comma costs many hours; ain't coding great?  Thanks again!
    Thursday, September 10, 2015 3:50 PM