簡體   English   中英

如何使用計算屬性格式化 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\2.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:前面的^ :確保正則表達式引擎只匹配字符串的開頭,不再需要通配符過濾器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM