如何使用计算 属性 格式化 Out-GridView 显示?
How to format the Out-GridView display using calculated property?
下面的 Powershell 脚本非常适合显示 Azure 用户属性,但需要格式化以删除公司域名的许可证列除外。
这是脚本。
#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(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 ';'
}
}
},
AlternateEmailAddresses,
UsageLocation,
isLicensed,
Licenses,
@{Label = 'License(s)';
Expression = {
($_.Licenses | ForEach-Object { ($_.AccountSkuId | Where-Object { $_.AccountSkuId -like 'CorpFinanceLtd*' }) -replace 'CorpFinanceLtd:' } ) -join ';'
}
},
PasswordNeverExpires,
BlockCredential
$allUsers | Out-GridView
我认为这部分需要修改,但不确定如何修改?
@{Label = 'License(s)';
Expression = {
($_.Licenses | ForEach-Object { ($_.AccountSkuId | Where-Object { $_.AccountSkuId -like 'CorpFinanceLtd*' }) -replace 'CorpFinanceLtd:' } ) -join ';'
}
},
默认许可证列显示许可证信息,如:{CorpFinanceLtd:MCOPSTNC, CorpFinanceLtd:ENTERPRISEPREMIUM, CorpFinanceLtd:EMSPREMIUM, CorpFinanceLtd:RIGHTSMANAGEMENT_ADHOC…}
那么,我该如何删除 CorpFinanceLtd: 前缀部分,使其产生更有意义的结果?
您将 $_.AccountSkuId
的 值 作为管道输入传递,但继续在结果对象上寻找 AccountSkuId
属性 ) - 这是行不通的,因为它只是一个字符串。
您可以将表达式简化如下:
@{
Label = 'License(s)';
Expression = {
($_.Licenses | ForEach-Object { $_.AccountSkuId -replace '^CorpFinanceLtd:' } ) -join ';'
}
},
CorpFinanceLtd:
前面的 ^
确保正则表达式引擎只会匹配字符串的开头,不再需要通配符过滤器
下面的 Powershell 脚本非常适合显示 Azure 用户属性,但需要格式化以删除公司域名的许可证列除外。
这是脚本。
#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(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 ';'
}
}
},
AlternateEmailAddresses,
UsageLocation,
isLicensed,
Licenses,
@{Label = 'License(s)';
Expression = {
($_.Licenses | ForEach-Object { ($_.AccountSkuId | Where-Object { $_.AccountSkuId -like 'CorpFinanceLtd*' }) -replace 'CorpFinanceLtd:' } ) -join ';'
}
},
PasswordNeverExpires,
BlockCredential
$allUsers | Out-GridView
我认为这部分需要修改,但不确定如何修改?
@{Label = 'License(s)';
Expression = {
($_.Licenses | ForEach-Object { ($_.AccountSkuId | Where-Object { $_.AccountSkuId -like 'CorpFinanceLtd*' }) -replace 'CorpFinanceLtd:' } ) -join ';'
}
},
默认许可证列显示许可证信息,如:{CorpFinanceLtd:MCOPSTNC, CorpFinanceLtd:ENTERPRISEPREMIUM, CorpFinanceLtd:EMSPREMIUM, CorpFinanceLtd:RIGHTSMANAGEMENT_ADHOC…}
那么,我该如何删除 CorpFinanceLtd: 前缀部分,使其产生更有意义的结果?
您将 $_.AccountSkuId
的 值 作为管道输入传递,但继续在结果对象上寻找 AccountSkuId
属性 ) - 这是行不通的,因为它只是一个字符串。
您可以将表达式简化如下:
@{
Label = 'License(s)';
Expression = {
($_.Licenses | ForEach-Object { $_.AccountSkuId -replace '^CorpFinanceLtd:' } ) -join ';'
}
},
CorpFinanceLtd:
前面的 ^
确保正则表达式引擎只会匹配字符串的开头,不再需要通配符过滤器