Reputation: 1139
I'm unable to convert the below long string of Display Name pattern into an array of strings in the Powershell below:
The purpose is to be able to display user without specific string patterns.
$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 code:
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
However, if the filtering section changed into this long string, it works:
($_.DisplayName -notmatch 'Admin|Calendar|Room|Prod|Account|Fax|Team|Office|Test|User')
Upvotes: 0
Views: 409
Reputation: 27423
You can use select-string with a list of patterns.
$list = echo ^one two three$
echo one | where { $_ | select-string -notmatch $list }
echo four | where { $_ | select-string -notmatch $list }
four
Or
echo four | where { select-string -notmatch $list -inputobject $_ }
four
Upvotes: 1
Reputation: 15478
Only -join is enough here:
$displayNamePattern = $dnPattern -join '|'
Upvotes: 2