如何使用If Else语句修改SMTPAddresses的Powershell计算属性?

问题描述 投票:0回答:1

我想修改Powershell以列出Azure AD中的用户,以在SMTP地址列中正确显示信息。

以下脚本是正确的,如果可能的话,我只需要进行如下更改:

  • 如果UserPrincipalName值包含Company1.onmicrosoft.com,则SMTP地址列应按原样显示所有内容,不进行任何处理。

  • 如果UserPrincipalName值不包含Company1.onmicrosoft.com,则SMTP地址列不应显示*@Company1.mail.onmicrosoft.com或*@Company1.onmicrosoft.com,因此仅显示常规SMTP协议地址。

  • “许可证”列不应显示前缀Company1:EnterpriseE1,而应仅显示EnterpriseE1。

到目前为止,我只能使用SMTP显示ProxyAddresses:

@{Label = 'SMTP Address'; Expression = { ($_.proxyAddresses | Where-Object { $_ -like "*smtp*" }) -replace 'smtp:' -join ';' } }

这是我的完整剧本:

#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 = '[email protected]'

#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
powershell active-directory azure-powershell powershell-remoting windows-scripting
1个回答
1
投票

我不确定您的普通SMTP协议地址是什么意思。

如果UserPrincipalName值不包含Company1.onmicrosoft.com,则不应显示“ SMTP地址”列*@Company1.mail.onmicrosoft.com或*@Company1.onmicrosoft.com,因此仅普通的SMTP协议地址。

但是,很容易将逻辑添加到计算出的属性中。希望这个例子可以为您打基础。

可能有点混乱,所以我想将哈希表表达式定义放在单独的变量中,以供以后在Select-Object命令中引用:

...

$SMTPExpression = 
@{
    Label = 'SMTP Address'
    Expression = 
    {
        If( $_.UserPrincipalName -match 'company1,onmicrosoft.com$')
        {
            $_.ProxyAddresses.Where( { $_ -match '^smtp:' } ) -replace 'smtp:'
        }
        Else
        {
            # Add some value or expression here...
            $_.ProxyAddresses.Where( { $_ -match '^smtp:' } )
        }
    }
}

$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, $SMTPExpression, AlternateEmailAddresses, UsageLocation, isLicensed, Licenses, PasswordNeverExpires, BlockCredential |


$allUsers | Out-GridView

我确实对您的where语句进行了一些更改,再次尝试使它简短一些,并进行了其他一些小更改(稍加重新格式化,以便我可以更轻松地工作...。]

我没有测试环境,但是该策略很可靠。让我知道事情的后续。谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.