Perhaps a bit misguided, but most of the videos/tutorials I see focus on not 
statically assigning anything for portability/flexibility. Although perhaps I 
could have a default action to dynamically discover unless the server parameter 
is specified.

Mostly it's an exercise to try and expand upon my PowerShell skills, so it 
doesn't feel like a wasted effort.

Regarding the PowerShell URI, is there any performance issue or problem if this 
were going to create 15-20 mailboxes and calling it repeatedly? As opposed to a 
single session being established and then processing all the mailbox creations?


From: [] On 
Behalf Of Michael B. Smith
Sent: Tuesday, September 20, 2016 4:54 PM
Subject: [powershell] RE: Passing Parameter to a Begin Block:

ATTENTION: This email came from an external source. DO NOT open attachments or 
click on links from unknown senders or unexpected emails.

Are you suggesting that the value of $DC is not available to the begin{} block? 
It should be.

I'm a little surprised that you would go to this much effort instead of just 
passing in a PowerShell URI.

Third comment: this won't work with Exchange 2016 or Office 365.

[] On Behalf Of Orlebeck, Geoffrey
Sent: Tuesday, September 20, 2016 1:08 PM
To: ''
Subject: [powershell] Passing Parameter to a Begin Block:

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:

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

function Enable-UserMailbox
        # AD user to enable mailbox
        # Primary SMTP address for user
        # Domain controller for Exchange server discovery
        # Only referenced once in begin block
        # Alias for user's new mailbox

        $ADRoot = Get-ADRootDSE -Server $DC
        $SearchBase = $ADRoot.configurationNamingContext
        $search = new-object 
        $objectClass = "objectClass=msExchExchangeServer"
        $version = "versionNumber>=1937801568"
        $search.Filter = "(&($objectClass)($version))"
        [void] $search.PropertiesToLoad.Add("name")
        [void] $search.PropertiesToLoad.Add("msexchcurrentserverroles")
        [void] $search.PropertiesToLoad.Add("networkaddress")
        $ExServers = $search.FindAll() | Foreach{
            New-Object PSObject -Property @{
                   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 
 -Authentication Kerberos
        Import-PSSession $Session -DisableNameChecking
        Enable-Mailbox $UserID -PrimarySMTPAddress $EmailAddress -Alias $Alias
        # 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