如何用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协议地址。

  • Licenses一栏不应该显示前缀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协议地址。

然而,在计算的属性中添加逻辑是非常直接的。 希望这个例子能给你打下基础。

这可能会有点乱,所以我喜欢把Hash表表达式定义停在一个单独的变量中,以便以后在这个变量中进行引用。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.