none
Return a value in Powershell function RRS feed

  • Question

  • Hi
    Am working on a powershell script which will check if the input SMTP address has been assigned to any mailbox. If it has been assigned, then to give appropriate response. The following script works fine.
    -----------start code-----------
    #check if a secondary smtp address is in use
    #assuming that entered address is valid (i.e. with @ and .)
    $SecondaryAdd = Read-Host "Enter the address:"
    function ValidSecondaryAdd {
    param ([string]$SecondaryAdd) 
    $LdapFilter = "(&(objectClass=*)(ProxyAddresses=smtp:$SecondaryAdd))"
    $Searcher = New-Object DirectoryServices.DirectorySearcher($Null, $LdapFilter)
    If ($Searcher.FindOne()) { Write-Host "SMTP Address is in use!!!" }
    else { Write-Host "SMTP Address is unique!!!" }
    }
    $Result = ValidSecondaryAdd($SecondaryAdd)
    -----------end code-----------
    My query is, instead of writing to host that the address exists or not, I would want the function to return a value, (say a global variable $i) that i can use to code further tasks. 
    Any help would be appreciated.
    Thursday, July 7, 2011 1:39 PM

Answers

  • $SecondaryAdd = Read-Host "Enter the address:"

    function ValidSecondaryAdd {
    param ([string]$SecondaryAdd)
    $LdapFilter = "(&(objectClass=*)(ProxyAddresses=smtp:$SecondaryAdd))"
    $Searcher = New-Object DirectoryServices.DirectorySearcher($Null, $LdapFilter)
    If ($Searcher.FindOne()) {
         Write-Host "SMTP Address is in use!!!"
         Return $false
        }
    else {
         Write-Host "SMTP Address is unique!!!"
         Return $true
          }
    }

    $Result = ValidSecondaryAdd $SecondaryAdd

    $Result will contain either $true or $false, depending on whether the address was unique. 

    Also, you don't enclose arguments to powershell functions in parens.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Thursday, July 7, 2011 3:15 PM

All replies

  • $SecondaryAdd = Read-Host "Enter the address:"

    function ValidSecondaryAdd {
    param ([string]$SecondaryAdd)
    $LdapFilter = "(&(objectClass=*)(ProxyAddresses=smtp:$SecondaryAdd))"
    $Searcher = New-Object DirectoryServices.DirectorySearcher($Null, $LdapFilter)
    If ($Searcher.FindOne()) {
         Write-Host "SMTP Address is in use!!!"
         Return $false
        }
    else {
         Write-Host "SMTP Address is unique!!!"
         Return $true
          }
    }

    $Result = ValidSecondaryAdd $SecondaryAdd

    $Result will contain either $true or $false, depending on whether the address was unique. 

    Also, you don't enclose arguments to powershell functions in parens.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
    Thursday, July 7, 2011 3:15 PM
  • Thank you for your help. This worked for me.
    Sunday, July 10, 2011 10:10 AM