Link to home
Start Free TrialLog in
Avatar of suriyaehnop
suriyaehnopFlag for Malaysia

asked on

Nested Function

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

ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suriyaehnop

ASKER

declined this question