无法使用字符串数组模式筛选用户DisplayName



我无法将下面的显示名称模式的长字符串转换为下面Powershell中的字符串数组:

其目的是能够在没有特定字符串模式的情况下显示用户。

$startsWith = @(
'Test'
'Sync_'
'admin'
)
$endsWith = @(
'365'
'$'
'svc'
'Sync'
'user'
)
$uPNpattern = '^({0})|({1})$' -f $($startsWith -join '|'), $($endsWith -join '|')
$dnPattern = @(
'Admin'
'Calendar'
'Room'
'Prod'
'Account'
'Fax'
'Team'
'Office'
'Test'
'User'
'insp'
)
$displayNamePattern = '^({0})|({1})$' -f $($dnPattern -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 $uPNpattern) -and
($_.UserPrincipalName -notlike '*#EXT#*') -and
($_.DisplayName -notmatch $displayNamePattern)
} | 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 'Company1Ltd*' }) -replace 'Company1Ltd:' } ) -join ';'
}
},
PasswordNeverExpires, 
BlockCredential
$allUsers | Out-GridView -Title "$(($allUsers).Count.ToString()) user accounts in total"

错误代码:

Error formatting a string: Index (zero based) must be greater than or equal to zero and less than the size of the argument list..
At line:102 char:44
+ $displayNamePattern = '^({0})|({1})$' -f $($dnPattern -join '|')
+                                            ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (^({0})|({1})$:String) [], RuntimeException
+ FullyQualifiedErrorId : FormatError

然而,如果过滤部分变成这个长字符串,它就可以工作:

($_.DisplayName -notmatch 'Admin|Calendar|Room|Prod|Account|Fax|Team|Office|Test|User')

这里只有-join就足够了:

$displayNamePattern = $dnPattern -join '|'

您可以将select字符串与模式列表一起使用。

$list = echo ^one two three$
echo one | where { $_ | select-string -notmatch $list } 
echo four | where { $_ | select-string -notmatch $list }
four

echo four | where { select-string -notmatch $list -inputobject $_ }
four

最新更新