如何使用 SMTPAddresses 的 If Else 语句修改 Powershell Calculated 属性?
How to modify the Powershell Calculated Property with If Else statement for SMTPAddresses?
我想修改 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.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
我不确定您所说的普通 SMTP 协议地址是什么意思。
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.
但是,向计算的 属性 添加逻辑非常简单。希望这个例子能给你打下基础。
它可能有点乱,所以我喜欢将散列 table 表达式定义放在一个单独的变量中,以便稍后在 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 语句,再次尝试使其更短一些,并做了一些其他小事(稍微重新格式化,以便我可以更轻松地工作....)。
我没有测试这个的环境,但这个策略是可靠的。
我想修改 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.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
我不确定您所说的普通 SMTP 协议地址是什么意思。
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.
但是,向计算的 属性 添加逻辑非常简单。希望这个例子能给你打下基础。
它可能有点乱,所以我喜欢将散列 table 表达式定义放在一个单独的变量中,以便稍后在 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 语句,再次尝试使其更短一些,并做了一些其他小事(稍微重新格式化,以便我可以更轻松地工作....)。
我没有测试这个的环境,但这个策略是可靠的。