【问题标题】:How to match up a User Profile with a file in their corresponding C:\User folder in a Powershell Script?如何将用户配置文件与 Powershell 脚本中相应 C:\User 文件夹中的文件匹配?
【发布时间】: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


【解决方案1】:

这是一种方法来做我认为你想要的...... [grin]

它的作用...

  • 将本地配置文件抓取到一个数组中
    包裹在调用周围的 @() 可确保您始终获得一个数组。
  • 以上内容也被过滤以排除 .Loaded.Special 配置文件
  • 定义要排除的本地用户目录
    你通常有一些你想保留的。 [咧嘴一笑]
  • 从该集合构建一个正则表达式 OR
  • 获取C:\Users 中的目录并排除“放手!”项目
  • 根据本地 Users 目录过滤 CIM 配置文件列表
  • 显示涉及的各种值
  • 显示与本地 c:\Users 目录中匹配的任何 CIM 配置文件

代码...

$ProfileList = @((Get-CimInstance -ClassName Win32_UserProfile |
    Where-Object {
        -not $_.Special -and
        -not $_.Loaded
        }))

$ExcludedAccountDirs = @(
    'public'
    'default'
    )
$RegexEAD = $ExcludedAccountDirs -join '|'

$LocalUserDirList = @((Get-ChildItem -LiteralPath 'C:\Users' -Directory |
    Where-Object {
        $_.Name -notmatch $RegexEAD
        }).FullName)

$PL_in_LUDL = $ProfileList |
    Where-Object {
    $_.LocalPath -in $LocalUserDirList
    }

'=' * 30
$ProfileList.LocalPath
'=' * 30
$LocalUserDirList
'=' * 30
$PL_in_LUDL

输出...

==============================
C:\Users\AltUserName
==============================
C:\Users\MyUserName
C:\Users\AltUserName
==============================


AppDataRoaming                   : Win32_FolderRedirectionHealth
Contacts                         : Win32_FolderRedirectionHealth
Desktop                          : Win32_FolderRedirectionHealth
Documents                        : Win32_FolderRedirectionHealth
Downloads                        : Win32_FolderRedirectionHealth
Favorites                        : Win32_FolderRedirectionHealth
HealthStatus                     : 3
LastAttemptedProfileDownloadTime : 
LastAttemptedProfileUploadTime   : 
LastBackgroundRegistryUploadTime : 
LastDownloadTime                 : 
LastUploadTime                   : 
LastUseTime                      : 2021-06-15 3:00:31 AM
Links                            : Win32_FolderRedirectionHealth
Loaded                           : False
LocalPath                        : C:\Users\AltUserName
Music                            : Win32_FolderRedirectionHealth
Pictures                         : Win32_FolderRedirectionHealth
RefCount                         : 
RoamingConfigured                : False
RoamingPath                      : 
RoamingPreference                : 
SavedGames                       : Win32_FolderRedirectionHealth
Searches                         : Win32_FolderRedirectionHealth
SID                              : S-1-5-21-3587298852-2732022926-2745136909-1001
Special                          : False
StartMenu                        : Win32_FolderRedirectionHealth
Status                           : 0
Videos                           : Win32_FolderRedirectionHealth
PSComputerName                   : 

【讨论】:

  • 非常感谢您的贡献!非常感谢,我感谢您抽出时间来帮助我。但是,我想做的有点不同。我想根据 C:\Users\InsertUsernameHere\AppData\Local\IconCache.db 文件的最后修改日期过滤和删除用户配置文件
  • @JellyfishPirate - 你说的是what can I enter in the parenthesis to compare the LocalPath value from the CimInstance to the path in C:\users?。我想一旦你有了它,你就可以检查目标文件的日期了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
  • 2017-11-06
  • 2013-06-29
  • 1970-01-01
相关资源
最近更新 更多