如果它与另一个数组中的值匹配,如何从数组中删除值?

问题描述 投票:0回答:0

在 Powershell 中,我正在运行以下命令

$AllU365Users = Get-MgUser -Filter 'assignedLicenses/$count eq 0' -ConsistencyLevel eventual -CountVariable unlicensedUserCount -All
以获取未分配给他们的 O365 许可证的用户列表,并将其存储在变量 $AllU365Users 中。
$AllU365Users[0]
的输出如下所示:

Id                                   DisplayName  Mail UserPrincipalName     UserType
--                                   -----------  ---- -----------------     --------
002b603e-4da8-4375-a9d9-d69a22a50ef9 WCSNCservice      [email protected]

我有另一个变量 $UnwantedUsers,它包含一个名称列表,它与 $AllU365Users 中某些用户的 DisplayName 相关联,看起来像这样

$script:UnwantedUsers = "ABM User, WCSNCservice, Afaria Service Account, APC, etc

我希望发生的是,对于存储在 $AllU365Users 中且具有与 $UnwantedUsers 中的值匹配的 DisplayName 的所有未经许可的用户,将它们从 $AllU365Users 中删除/过滤掉。可以将它们存储在同一个变量或一个新变量中,比如 $FilteredUsers。我尝试使用 Where-Object 根据我通过执行以下操作找到的不同的 SO 帖子进行过滤:

$FilteredUsers = $AllU365Users | ? ($_.DisplayName -notin $script:UnwantedUsers)
但是这不起作用并且 $FilteredUsers 只是空白。我只是不知道足够的 PS 来解决这个问题,没有一个例子可以解决。

powershell
© www.soinside.com 2019 - 2024. All rights reserved.