尝试确定网络上是否存在没有关联用户帐户的用户文件夹。当大多数结果应返回“找到”时,所有结果都返回“缺失”。有什么想法吗?
$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
}
}
最佳答案
尝试一种稍微不同的方法,使用哈希表(它提供非常快速的键查找):
$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)"])。
关于arrays - Powershell - 使用 Quest CMDlet 搜索和比较数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1273323/