Powershell - 使用 Quest CMDlet 搜索和比较数组

发布于 2024-08-02 00:20:22 字数 396 浏览 1 评论 0原文

尝试确定网络上是否存在没有关联用户帐户的用户文件夹。 当大多数结果应返回“找到”时,所有结果都返回“缺失”。 有任何想法吗?

$Dir = "\\ServerName\Share\"
$FolderList = Get-ChildItem($Dir) | where {$_.psIsContainer -eq $true}
$UserList = get-qaduser -sizelimit 0 | select LogonName

foreach ($Folder in $FolderList)
{
if ($UserList -contains $Folder.name)
{
"Found:  " + $Folder.name
}
Else
{
"Missing:  " + $Folder.name
}
}

Trying to determine if there are any user folders on the network that don’t have an associated user account. All results return "Missing" when the majority should return "Found". Any ideas?

$Dir = "\\ServerName\Share\"
$FolderList = Get-ChildItem($Dir) | where {$_.psIsContainer -eq $true}
$UserList = get-qaduser -sizelimit 0 | select LogonName

foreach ($Folder in $FolderList)
{
if ($UserList -contains $Folder.name)
{
"Found:  " + $Folder.name
}
Else
{
"Missing:  " + $Folder.name
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第七度阳光i 2024-08-09 00:20:22

尝试一种稍微不同的方法,使用哈希表(它提供了极其快速的键查找):

$users = @{}
Get-QADUser -sizelimit 0 | Foreach {$users["$($_.LogonName)"] = $true}
$dir = "\\ServerName\Share\"
Get-ChildItem $dir | Where {$_.PSIsContainer -and !$users["$($_.Name)"]}

如果文件夹名称与 LogonName 不完全匹配,那么正如 EBGreen 所说,您将需要调整键($users[" $($.LogonName)"]) 或使用它来索引哈希表时的文件夹名称 (!$users["$($.Name)"])。

How about trying a slightly different approach that uses a hashtable (which offers exceptionally fast lookups of keys):

$users = @{}
Get-QADUser -sizelimit 0 | Foreach {$users["$($_.LogonName)"] = $true}
$dir = "\\ServerName\Share\"
Get-ChildItem $dir | Where {$_.PSIsContainer -and !$users["$($_.Name)"]}

If the folder name doesn't exactly match the LogonName, then as EBGreen notes, you will need to adjust the key ($users["$($.LogonName)"]) or the folder name when you use it to index the hashtable (!$users["$($.Name)"]).

汹涌人海 2024-08-09 00:20:22

如果集合中的项目与您正在测试的项目相同,则 -contains 将匹配,因此请确保 $Folder.Name 与 LogonName 完全相同。 通常情况下不会。 大多数公司将用户名为 foo 的文件夹名称设置为 foo$。

-contains will match if the item in the collection is identical to what you are testing so be sure that the $Folder.Name is exactly the same as LogonName. Usually it wouldn't be. Most companies would have the folder name be foo$ for a user named foo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文