Page 1 of 1

Need some help in modifying the Powershell Calculated Property with If Else statement for SMTPAddresses?

Posted: Wed Apr 29, 2020 6:01 am
by ITEngineer
Hi People,

I need some help in modifying the Powershell to list users from Azure AD to display the information correctly in the SMTP Address Column.

The below script is correct, I just need to make some changes like below if possible:

If the UserPrincipalName value contains Company1.onmicrosoft.com, then SMTP Address column should show everything as it is, do not process anything.

If the UserPrincipalName value does NOT contain Company1.onmicrosoft.com, then SMTP Address column should NOT display the *@Company1.mail.onmicrosoft.com or *@Company1.onmicrosoft.com, so only the normal SMTP protocol address.

The Licenses column should NOT display the prefix Company1:EnterpriseE1, but just EnterpriseE1.

So far I can only display the ProxyAddresses with SMTP with the below line:

Code: Select all

@{Label = 'SMTP Address'; Expression = { ($_.proxyAddresses | Where-Object { $_ -like "*smtp*" }) -replace 'smtp:' -join ';' } }
I wonder if anyone can assist me in fixing the code below:

Code: Select all

#Import Module
If (!(Get-Module "*MSOnline*")) {Import-Module MSOnline}
If (!(Get-Module "*Exchange*")) {Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse).FullName | ?{ $_ -notmatch "_none_" } | select -First 1)}

#Set admin UPN
$UPN = 'Global.Admin@domain.com'

#This connects to Azure Active Directory & Exchange Online
Connect-MsolService
$EXOSession = New-ExoPSSession -UserPrincipalName $UPN
Import-PSSession $EXOSession -DisableNameChecking -AllowClobber

$startsWith = @(
	'Test'
    'Sync_'
)

$endsWith = @(
    '365'
    '\$'
    'svc'
    'Sync'
	'user'
)

$pattern = '^({0})|({1})$' -f $($startsWith -join '|'), $($endsWith -join '|')

# Member Outputs for Microsoft.Online.Administration.User based on https://docs.microsoft.com/en-us/powershell/module/msonline/get-msoluser?view=azureadps-1.0
$allUsers = @()
$allUsers = Get-MsolUser -All -EnabledFilter EnabledOnly | Where-Object {
        ($_.UserPrincipalName -notmatch $pattern) -and
        ($_.UserPrincipalName -notlike '*#EXT#*') -and
        ($_.DisplayName -notmatch 'Admin|Calendar|Room|Prod|Account|Fax|Team|Office|Test|User')
} | Select-Object FirstName, LastName, UserPrincipalName, @{Label = 'SMTP Address'; Expression = { ($_.proxyAddresses | Where-Object { $_ -like "*smtp*" }) -replace 'smtp:' -join ';' } }, AlternateEmailAddresses, UsageLocation, isLicensed, Licenses, PasswordNeverExpires, BlockCredential

$allUsers | Out-GridView
Thanks in advance.

Re: Need some help in modifying the Powershell Calculated Property with If Else statement for SMTPAddresses?

Posted: Wed Apr 29, 2020 7:54 am
by jvierra
Here is an easier way to build and test a complex filter-converter. Use a function then just call the function in the calculated properties.

Design the function to consume the proxyaddresses and spit out the results. You can test the function at a prompt with dummy data until it behaves as you need.

Re: Need some help in modifying the Powershell Calculated Property with If Else statement for SMTPAddresses?

Posted: Wed Apr 29, 2020 7:45 pm
by ITEngineer
Mr. Vierra,

I finally got it working by using:

Code: Select all

    @{Label = 'SMTP Address(es)'; 
        Expression = { 
            If (( $_.UserPrincipalName -match 'onmicrosoft.com$')) {
              ($_.proxyAddresses | Where-Object { ($_ -like 'SMTP*') -and ($_ -like '*onmicrosoft.com') }) -replace 'smtp:' -join ';'
            } Else {
                ($_.proxyAddresses | Where-Object { ($_ -like 'SMTP*') -and ($_ -notlike '*onmicrosoft.com') }) -replace 'smtp:' -join ';'
            }
        } 
    },
So how do I transform that into the function that I can call in the Calculated property?

Re: Need some help in modifying the Powershell Calculated Property with If Else statement for SMTPAddresses?

Posted: Wed Apr 29, 2020 7:50 pm
by jvierra
If it works you don't need a function. The function is just a way to simplify the approach to the code.

Re: Need some help in modifying the Powershell Calculated Property with If Else statement for SMTPAddresses?

Posted: Wed Apr 29, 2020 10:42 pm
by ITEngineer
Does the Function or splatting make the code run any faster?