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



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

下面的脚本是正确的,如果可能的话,我只需要做一些修改,如下所示:

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

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

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

到目前为止,我只能用SMTP显示代理地址,比如:

@{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 + "Apps2.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://learn.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

我不知道你说的正常SMTP协议地址是什么意思。

如果UserPrincipalName值不包含Company1.在microsoft.com上,则SMTP地址列不应显示*@Company1.mail.onmicrosoft.com或*@Companie1.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语句做了一点更改,再次只是想把它缩短一点,并做了一些其他小事情(稍微重新格式化,这样我可以更容易地工作……(。

我没有一个环境来测试这一点,但策略是可靠的。

最新更新