And I forgot one distinction, if I attempt to pipe in the parameters, it is not 
available in the Begin block. See examples below. I prefaced the variables with 
the block they are in. Not pipelined, followed by $objects output and when 
pipelined:

[cid:image001.png@01D21373.096B27F0]

Although, in attempting this I just realized what I think the problem is-I have 
the pipeline enabled by property name, but not position. So if I pipe $objects 
| Test-cmdletProcess -DC $DC, then it shows up in the Begin block.

Mystery solved. Like I said before, this is my first real attempt to tackle 
full blown advanced functions/cmdlets.

Thanks,
Geoff

From: listsad...@lists.myitforum.com [mailto:listsad...@lists.myitforum.com] On 
Behalf Of Orlebeck, Geoffrey
Sent: Tuesday, September 20, 2016 6:28 PM
To: 'powershell@lists.myitforum.com'
Subject: [powershell] RE: Passing Parameter to a Begin Block:

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?

Thanks,
Geoff

From: listsad...@lists.myitforum.com<mailto:listsad...@lists.myitforum.com> 
[mailto:listsad...@lists.myitforum.com] On Behalf Of Michael B. Smith
Sent: Tuesday, September 20, 2016 4:54 PM
To: powershell@lists.myitforum.com<mailto:powershell@lists.myitforum.com>
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.

From: listsad...@lists.myitforum.com<mailto:listsad...@lists.myitforum.com> 
[mailto:listsad...@lists.myitforum.com] On Behalf Of Orlebeck, Geoffrey
Sent: Tuesday, September 20, 2016 1:08 PM
To: 'powershell@lists.myitforum.com'
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:

<#
.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/<https://urldefense.proofpoint.com/v2/url?u=http-3A__-24URI_PowerShell_&d=CwMFAg&c=GtV7VYka8XzFJya76SH24R7OU_QKFTyBlklHoDMCjFY&r=WF1NZuUqAd1bRIxLFT_0wz8npqTRKjPr3_qzGO_dTx_Q3Taym2JWM42n-cKyG-6W&m=hIkRyC5U6h7bD_7qODgkLmyDOUsUk5kXemE08sFQIdw&s=7g-sYvWWmrhiixleCO3_IPB2XWtqb_-csXZUq_cpBf4&e=>
 -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