【发布时间】:2021-08-05 12:52:07
【问题描述】:
我目前的任务是从我的域中的数百台计算机中删除超过两周的用户配置文件。
由于任务繁琐,我决定研究脚本以尝试简化流程。我对 PowerShell 非常陌生,经过大量挖掘以了解如何解决这个问题,我想出了以下内容:
Get-CimInstance -class Win32_UserProfile | Where {(!$_.Special) -and ($_.LastUseTime) -lt (Get-Date).AddDays(-30)}| Remove-CimInstance
但是,由于 Windows 更新和防病毒扫描,我无法依赖上面提到的 LastUseTime 属性。经过更多研究,我能找到的唯一可靠时间戳是用户最后一次登录计算机的时间是他们 C:\Users 文件夹中 IconCache.db 文件的最后修改日期。
我对脚本的想法是这样的:
#for each user in C:Users (excluding system accounts)
#get the last modified date of their IconCache.db file,
#if the last modified date of their IconCache.db file is less than 30 days,
#find their corresponding user profile,
#delete the corresponding user profile,
我已经尽我所能实现了上述想法,如下所示
$Path = "C:\Users"
$UserFolders = $Path | GCI -Directory
ForEach ($User in $UserFolders)
{Get-ChildItem C:\Users\$User\AppData\Local\IconCache.db -Hidden
if ($_.LastWriteTime -lt (Get-Date).AddDays(-30)
{
Get-CimInstance -class Win32_UserProfile | Where {(!$_.Special) -and ($_.LocalPath -eq (???))} | Remove-CimInstance
}}
使用此脚本,我试图将 foreach 循环中的每个 $User 与 Win32_UserProfile 类中的相应用户配置文件进行匹配。但是,我不确定如何将 LocalPath 属性与 $Users 中的每个用户匹配。任何帮助将不胜感激。
【问题讨论】:
-
[1] 您的实际具体问题是什么? ///// [2] 在我的系统上,
ntuser.dat文件似乎没有受到我的反恶意软件工具的影响。 ///// [3] 如果您的实际问题是如何将用户帐户与特定配置文件匹配...我系统上的用户帐户名称是.LocalPath值的最后一部分。 -
对不起,如果我的问题含糊不清。我是 powershell 的完全新手,发现很难以简洁的方式交流想法。我的问题是:在“Get-CimInstance -class Win32_UserProfile | Where {(!$_.Special) -and ($_.LocalPath -eq (???))}”行中,我可以在括号中输入什么将 CimInstance 中的 LocalPath 值与 C:\users 中的路径进行比较?
-
请看我的回答。它认为它涵盖了您要询问的内容-如果没有,请问我! [咧嘴一笑]
标签: windows powershell user-profile