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

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 3 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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.
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post 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.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post 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?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

If it works you don't need a function. The function is just a way to simplify the approach to the code.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

Does the Function or splatting make the code run any faster?
/* IT Engineer */
This topic is 3 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked