Nested Function

suriyaehnop
suriyaehnop used Ask the Experts™
on
I had a powershell whereby, it will delete the secndary email address. The nested function is to differentiate between Get-MailContact and Get-Distribution. The script work but the Output on Outer Function didn't carried correct result, please help.

When I check the script using Set-Debug -Step

PS C:\PS> Set-PSDebug -Step
PS C:\PS> .\Test6.ps1

I need to apply $2 (the result of Outer function ) to Set-DistributionGroup -Identity $group.DL -EmailAddresses $NewEmail. However $NewEmail contains this:

MailUniversalDistributionGroup
tt.fat                        tt.fat                        Universal                     tt.fat@contosocrest.com
tt.fat                        tt.fat                        Universal                     tt.fat@contosocrest.com

SmtpAddress        : tt.fat@contosocrest.com
AddressString      : tt.fat@contosocrest.com
ProxyAddressString : SMTP:tt.fat@contosocrest.com
Prefix             : SMTP
IsPrimaryAddress   : True
PrefixString       : SMTP


SmtpAddress        : tt.fat@contoso.com
AddressString      : tt.fat@contoso.com
ProxyAddressString : smtp:tt.fat@contoso.com
Prefix             : SMTP
IsPrimaryAddress   : False
PrefixString       : smtp


SmtpAddress        : tt.fat@contosocrest.com
AddressString      : tt.fat@contosocrest.com
ProxyAddressString : SMTP:tt.fat@contosocrest.com
Prefix             : SMTP
IsPrimaryAddress   : True
PrefixString       : SMTP

Pleae advice


Continue with this operation?
  1+ <<<< .\Test6.ps1
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG:  1+ <<<< .\Test6.ps1

Continue with this operation?
  2+ $groups = <<<< Import-Csv C:\PS\SCPortal1.csv
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG:  2+ $groups = <<<< Import-Csv C:\PS\SCPortal1.csv

Continue with this operation?
  5+ Foreach <<<< ( $group in $groups )
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG:  5+ Foreach <<<< ( $group in $groups )

Continue with this operation?
  7+ <<<< New-DistributionGroup -Name $group.DL -SamAccountName $group.DL -Type Distribution `
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG:  7+ <<<< New-DistributionGroup -Name $group.DL -SamAccountName $group.DL -Type Distribution `

Name             DisplayName          GroupType           PrimarySmtpAddress
----             -----------          ---------           ------------------
tt.fat            tt.fat            Universal           tt.fat@contoso.com

Continue with this operation?
 10+ do <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  10+ do <<<<

Continue with this operation?
 12+ $mailgroup = <<<< Get-DistributionGroup $group.DL -ErrorAction SilentlyContinue
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  12+ $mailgroup = <<<< Get-DistributionGroup $group.DL -ErrorAction SilentlyContinue

Continue with this operation?
 12+ $mailgroup = <<<< Get-DistributionGroup $group.DL -ErrorAction SilentlyContinue
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> Get-DistributionGroup -Identity $group.DL

Name             DisplayName          GroupType           PrimarySmtpAddress
----             -----------          ---------           ------------------
tt.fat            tt.fat            Universal           tt.fat@contoso.com


PS C:\PS>>> exit

Continue with this operation?
 12+ $mailgroup = <<<< Get-DistributionGroup $group.DL -ErrorAction SilentlyContinue
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  12+ $mailgroup = <<<< Get-DistributionGroup $group.DL -ErrorAction SilentlyContinue

Continue with this operation?
 16+ <<<< Set-DistributionGroup -Identity $group.DL -PrimarySMTPAddress ($group.DL+'@contosocrest.com') `
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  16+ <<<< Set-DistributionGroup -Identity $group.DL -PrimarySMTPAddress ($group.DL+'@contosocrest.com') `

Continue with this operation?
 20+ Function RemoveSecondaryEmailAddress <<<< {
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> Get-DistributionGroup -Identity $group.DL

Name             DisplayName          GroupType           PrimarySmtpAddress
----             -----------          ---------           ------------------
tt.fat            tt.fat            Universal           tt.fat@contoso.com


PS C:\PS>>> Get-DistributionGroup -Identity $group.DL | ft emailaddresses

EmailAddresses
--------------
{SMTP:tt.fat@contosocrest.com, smtp:tt.fat@contoso.com}


PS C:\PS>>> exit

Continue with this operation?
 20+ Function RemoveSecondaryEmailAddress <<<< {
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  20+ Function RemoveSecondaryEmailAddress <<<< {

Continue with this operation?
 55+ $NewEmail = <<<< (RemoveSecondaryEmailAddress $group.DL)
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  55+ $NewEmail = <<<< (RemoveSecondaryEmailAddress $group.DL)

Continue with this operation?
 21+ $groupname = <<<< $group.DL
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  21+ $groupname = <<<< $group.DL

Continue with this operation?
 23+ $groupDL = <<<< Get-DistributionGroup -Identity $groupname
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> $groupname
tt.fat
PS C:\PS>>> exit

Continue with this operation?
 23+ $groupDL = <<<< Get-DistributionGroup -Identity $groupname
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  23+ $groupDL = <<<< Get-DistributionGroup -Identity $groupname

Continue with this operation?
 24+ $groupDL. <<<< RecipientType
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  24+ $groupDL. <<<< RecipientType

Continue with this operation?
 25+ $2 = <<<< $null
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  25+ $2 = <<<< $null

Continue with this operation?
 26+ Function Email <<<< ($1) {
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  26+ Function Email <<<< ($1) {

Continue with this operation?
 38+ $2 <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"):
DEBUG:  38+ $2 <<<<

Continue with this operation?
 39+ if <<<< ($groupDL.RecipientType -eq 'MailUniversalDistributionGroup')
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  39+ if <<<< ($groupDL.RecipientType -eq 'MailUniversalDistributionGroup')

Continue with this operation?
 41+ $1= <<<< Get-DistributionGroup -Identity $groupname
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  41+ $1= <<<< Get-DistributionGroup -Identity $groupname

Continue with this operation?
 42+ $1 <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> $1

Name             DisplayName          GroupType           PrimarySmtpAddress
----             -----------          ---------           ------------------
tt.fat            tt.fat            Universal           tt.fat@contosocrest.com


PS C:\PS>>> exit

Continue with this operation?
 42+ $1 <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  42+ $1 <<<<

Continue with this operation?
 43+ <<<< Email $1
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> $1

Name             DisplayName          GroupType           PrimarySmtpAddress
----             -----------          ---------           ------------------
tt.fat            tt.fat            Universal           tt.fat@contosocrest.com


PS C:\PS>>> exit

Continue with this operation?
 43+ <<<< Email $1
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  43+ <<<< Email $1

Continue with this operation?
 27+ $1 <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  27+ $1 <<<<

Continue with this operation?
 28+ $2= <<<< $1.EmailAddresses
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  28+ $2= <<<< $1.EmailAddresses

Continue with this operation?
 30+ $2 <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> $2


SmtpAddress    : tt.fat@contosocrest.com
AddressString   : tt.fat@contosocrest.com
ProxyAddressString : SMTP:tt.fat@contosocrest.com
Prefix       : SMTP
IsPrimaryAddress  : True
PrefixString    : SMTP

SmtpAddress    : tt.fat@contoso.com
AddressString   : tt.fat@contoso.com
ProxyAddressString : smtp:tt.fat@contoso.com
Prefix       : SMTP
IsPrimaryAddress  : False
PrefixString    : smtp



PS C:\PS>>> exit

Continue with this operation?
 30+ $2 <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  30+ $2 <<<<

Continue with this operation?
 32+ $3= <<<< $groupname +'@contoso.com'
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  32+ $3= <<<< $groupname +'@contoso.com'

Continue with this operation?
 34+ $2 -= <<<< $3
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> $3
tt.fat@contoso.com
PS C:\PS>>> exit

Continue with this operation?
 34+ $2 -= <<<< $3
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  34+ $2 -= <<<< $3

Continue with this operation?
 35+ Return <<<< $2
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): s
PS C:\PS>>> $2


SmtpAddress    : tt.fat@contosocrest.com
AddressString   : tt.fat@contosocrest.com
ProxyAddressString : SMTP:tt.fat@contosocrest.com
Prefix       : SMTP
IsPrimaryAddress  : True
PrefixString    : SMTP



PS C:\PS>>> exit

Continue with this operation?
 35+ Return <<<< $2
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  35+ Return <<<< $2

Continue with this operation?
 56+ $group. <<<< DL
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  56+ $group. <<<< DL
tt.fat

Continue with this operation?
 57+ $NewEmail <<<<
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): y
DEBUG:  57+ $NewEmail <<<<
MailUniversalDistributionGroup
tt.fat            tt.fat            Universal           tt.fat@contosocrest.com
tt.fat            tt.fat            Universal           tt.fat@contosocrest.com

SmtpAddress    : tt.fat@contosocrest.com
AddressString   : tt.fat@contosocrest.com
ProxyAddressString : SMTP:tt.fat@contosocrest.com
Prefix       : SMTP
IsPrimaryAddress  : True
PrefixString    : SMTP


SmtpAddress    : tt.fat@contoso.com
AddressString   : tt.fat@contoso.com
ProxyAddressString : smtp:tt.fat@contoso.com
Prefix       : SMTP
IsPrimaryAddress  : False
PrefixString    : smtp


SmtpAddress    : tt.fat@contosocrest.com
AddressString   : tt.fat@contosocrest.com
ProxyAddressString : SMTP:tt.fat@contosocrest.com
Prefix       : SMTP
IsPrimaryAddress  : True
PrefixString    : SMTP


Continue with this operation?
 58+ $4 = <<<< Set-DistributionGroup -Identity $group.DL -EmailAddresses $NewEmail

As you can see that the final $2 result is:

SmtpAddress        : tt.fat@contosocrest.com
AddressString      : tt.fat@contosocrest.com
ProxyAddressString : SMTP:tt.fat@contosocrest.com
Prefix             : SMTP
IsPrimaryAddress   : True
PrefixString       : SMTP

Open in new window

#Import data from CSV file
$groups = Import-Csv C:\PS\SCPortal1.csv

#The CSV file must have these header:DL,Contact,Organization
Foreach ( $group in $groups )
{
New-DistributionGroup -Name $group.DL -SamAccountName $group.DL -Type Distribution `
-Organization $group.organization 

do
{
$mailgroup = Get-DistributionGroup $group.DL -ErrorAction SilentlyContinue
}
while ($mailgroup -eq $null)

Set-DistributionGroup -Identity $group.DL -PrimarySMTPAddress ($group.DL+'@contosocrest.com') `
-EmailAddressPolicyEnabled:$false -RequireSenderAuthenticationEnabled:$false

#Function Get list of Email Addresses
Function RemoveSecondaryEmailAddress{
$groupname = $group.DL
#To Check type
$groupDL = Get-DistributionGroup -Identity $groupname
$groupDL.RecipientType
$2 = $null
Function Email ($1) {
$1
$2=$1.EmailAddresses 
#List of Email Addresses for new DL
$2
#Remove the SMTP with @contoso.com
$3=$groupname +'@contoso.com'
#Final list of Email Addresses
$2 -=$3
Return $2
}
#2 Return $2
$2
if ($groupDL.RecipientType -eq 'MailUniversalDistributionGroup')
{ 
$1=Get-DistributionGroup -Identity $groupname
$1
Email $1
}
else
{
$1 = Get-MailContact -Identity $groupname
$1
Email $1
}
#Return $2
}
#1.Return $2
#Set the final Email Addreses to new DL
$NewEmail = (RemoveSecondaryEmailAddress $group.DL)
$group.DL
$NewEmail
$4 = Set-DistributionGroup -Identity $group.DL -EmailAddresses $NewEmail
$4


New-MailContact -Name $group.Contact -Alias $group.DL -Firstname $group.Firstname `
-Lastname $group.Lastname -ExternalEmailAddress ($group.DL + '@contosoportal.com') -Organization $group.organization

#To avoid AD latency with two conditions
do
{
$mailcontact = Get-MailContact $group.Contact -ErrorAction SilentlyContinue
}
while ($mailgroup -eq $null)

#Set UserID@sapuracrest.com.my as Primary SMTP
#$email = $group.DL+'@sapuracrest.com.my'
Set-MailContact -Identity $group.Contact -HiddenFromAddressList:$true
Add-DistributionGroupMember -Identity $group.DL -Member $group.Contact 
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
PowerShell Developer
Top Expert 2010
Commented:
You don't half make things hard work for yourself. Nested functions? The function "Email" only exists within the context of RemoveSecondaryEmailAddress.

To be honest, I'm not going to pick that apart, it's foul. If you state your goals I can help you make something I consider to be more sensible / logical though :) All you're wanting to do is knock the second SMTP address off a group that's just been mail enabled?

Chris

Author

Commented:
declined this question

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial