I'm just diving into advanced functions/cmdlets and am wondering is it 
supported or possible to pass a parameter into a Begin block? I'm working on a 
script that will dynamically discover an Exchange server one time during the 
Begin block, then enable mailboxes for any/all users passed through to the 
function. There is one parameter that I want to pass through from the user add 
function, but it needs to be passed to the Begin block (it is not referenced in 
the Process/End blocks). I'm trying to search online, but I'm not finding any 
relevant results. Here is the full code, thank you for any help in adavance:

<#
.Synopsis
   Adds mailbox to new AD user without requiring EMS 2010 Mgmt tools
.DESCRIPTION
   Dynamically discovers Exchange Mailbox servers in specified domain
   and enables mailbox for specified users without requiring EMS
  tools on local endpoint
.EXAMPLE
   #Pending Update#
#>
#Requires -Version 3

function Enable-UserMailbox
{
    [CmdletBinding()]
    [Alias()]
    Param
    (
        # AD user to enable mailbox
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $UserID,
        # Primary SMTP address for user
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        $EmailAddress,
        # Domain controller for Exchange server discovery
        # Only referenced once in begin block
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        $DC,
        # Alias for user's new mailbox
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true)]
        $Alias
    )

    Begin
    {
        $ADRoot = Get-ADRootDSE -Server $DC
        $SearchBase = $ADRoot.configurationNamingContext
        $search = new-object 
DirectoryServices.DirectorySearcher([ADSI]"LDAP://$SearchBase")
        $objectClass = "objectClass=msExchExchangeServer"
        $version = "versionNumber>=1937801568"
        $search.Filter = "(&($objectClass)($version))"
        $search.PageSize=1000
        [void] $search.PropertiesToLoad.Add("name")
        [void] $search.PropertiesToLoad.Add("msexchcurrentserverroles")
        [void] $search.PropertiesToLoad.Add("networkaddress")
        $ExServers = $search.FindAll() | Foreach{
            New-Object PSObject -Property @{
                   Name = $_.Properties.name[0]
                   FQDN = $_.Properties.networkaddress | %{if ($_ -match 
"ncacn_ip_tcp") {$_.split(":")[1]}}
                   Roles = $_.Properties.msexchcurrentserverroles[0]
                }
            }

        # Label Exchange server roles for selection
        $role = @{2  = "MB"
                  4  = "CAS"
                  16 = "UM"
                  32 = "HT"
                  64 = "ET"}

        $ExchSrvrs = foreach ($server in $ExServers)
        {
            $roles = ($role.keys | ?{$_ -band $server.roles} | 
%{$role.Get_Item($_)}) -join ","
            $server | select Name,FQDN, @{n="Roles";e={$roles}}
        }
        $ExSrvr = $ExchSrvrs | Where {$_.Roles -like "*MB*"}
        $URI = $ExSrvr[0].FQDN

        # Establish Exchange Server connection
        $Session = New-PSSession -ConfigurationName Microsoft.Exchange 
-ConnectionUri http://$URI/PowerShell/ -Authentication Kerberos
        Import-PSSession $Session -DisableNameChecking
    }
    Process
    {
        Enable-Mailbox $UserID -PrimarySMTPAddress $EmailAddress -Alias $Alias
    }
    End
    {
        # END BLOCK REQUIREMENTS STILL IN PROGRESS #
        # Return output confirming mailbox addition?
        # Remove PS Sessions?
    }
}

Confidentiality Notice: This is a transmission from Community Hospital of the 
Monterey Peninsula. This message and any attached documents may be confidential 
and contain information protected by state and federal medical privacy 
statutes. They are intended only for the use of the addressee. If you are not 
the intended recipient, any disclosure, copying, or distribution of this 
information is strictly prohibited. If you received this transmission in error, 
please accept our apologies and notify the sender. Thank you.



Reply via email to