【问题标题】:Simple script to delete old user profiles删除旧用户配置文件的简单脚本
【发布时间】:2017-09-25 19:28:19
【问题描述】:

我正在编写一个简单的脚本,用于删除超过 90 天的用户配置文件。我可以捕获我想要的配置文件,但是当谈到“面包和黄油”时,我很难过。

我的代码:

$localuserprofiles = Get-WmiObject -Class Win32_UserProfile | Select-Object localPath,@{Expression={$_.ConvertToDateTime($_.LastUseTime)};Label="LastUseTime"}| Where{$_.LocalPath -notlike "*$env:SystemRoot*"} #Captures local user profiles and their last used date
$unusedday = 90 # Sets the unused prifile time threshold
$excludeduserpath = $excludeduser.LocalPath # Excludes the DeltaPC user account
$profilestodelete = $LocalUserProfiles | where-object{$_.lastusetime -le (Get-Date).AddDays(-$unusedday) -and $_.Localpath -notlike "*$excludeduserpath*"} #Captures list of user accounts to be deleted

#Deletes unused Profiles

Foreach($deletedprofile in $profilestodelete)
    {
        $deletedprofile.Delete()
    }

代码返回此错误:

Method invocation failed because [Selected.System.Management.ManagementObject] does not contain a method named 'Delete'. 
At line:3 char:13 
+ $deletedprofile.Delete()} 
+ ~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo : InvalidOperation: (Delete:String) [], RuntimeException 
    + FullyQualifiedErrorId : MethodNotFound

【问题讨论】:

  • 发生了什么/没有发生什么?
  • 不删除,给出此错误:方法调用失败,因为 [Selected.System.Management.ManagementObject] 不包含名为“删除”的方法。在 line:3 char:13 + $deletedprofile.Delete()} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Delete:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound

标签: powershell powershell-4.0


【解决方案1】:

自定义对象$deletedprofile 上没有定义任何Delete() 方法。使用

Foreach($deletedprofile in $profilestodelete)
    {
        $aux = Get-Item $deletedprofile.localPath
        $aux.Delete()
    }

或者干脆

Foreach($deletedprofile in $profilestodelete)
    {
        (Get-Item $deletedprofile.localPath).Delete()
    }

您可能需要指定.Delete($true)

PS C:\Windows\system32> Get-Item  $profilestodelete[0].localPath | Get-Member -Name Delete


   TypeName: System.IO.DirectoryInfo

Name   MemberType Definition
----   ---------- ----------
Delete Method     void Delete(), void Delete(bool recursive)

编辑

作为Mark Wragg mentioned,不建议简单地删除用户配置文件目录,因为这不会从注册表中删除与配置文件关联的数据。另请参阅 Helge Klein(delprof2 tool 的作者)撰写的详尽文章 Deleting a Local User Profile - Not as easy as one Might Assume

但是,有一个纯 PowerShell 脚本,其中包含 一个用于删除用户配置文件的函数 (Remove-UserProfile),以及 C:\Users 目录的其他内容(如果指定) ) 在本地计算机上 gallery.technet.microsoft.com: Remove-UserProfile - Remove Local User Profiles and Clean C:\Users Directory

【讨论】:

  • 此方法使用Get-Item cmdlet 返回代表配置文件目录的System.IO.DirectoryInfo 对象。该 PowerShell 对象有一个删除方法,可以删除目录及其文件,但请注意,这不是删除用户配置文件的好方法,因为它会留下许多其他数据,并会导致这些用户在未来登录时出现问题。请参阅我的答案以了解替代方法。
【解决方案2】:

根据另一个答案,WMI 提供的用户配置文件没有 Delete() 方法。虽然您可以只删除配置文件目录,但通常不建议这样做,因为您会留下各种其他数据(例如注册表项),并且如果这些用户随后重新登录到计算机,可能会导致登录问题。

有一个名为 delprof2.exe 的免费 3rd 方工具:https://www.sepago.com/blog/2011/05/01/new-free-delprof2-user-profile-deletion-tool

我没有亲自使用过,所以请小心使用,但它似乎已经有一个选项可以删除已停用 x 天的个人资料,例如:

Delprof2 /d:90

现在,如果您只是删除用户配置文件目录会发生什么 在 C:\Users 下面而不修改注册表?下次用户 在 Windows 上登录会显示一个气球提示,抱怨 Windows 无法 加载用户配置文件并且用户使用临时登录 轮廓。那不好吗?是的!临时配置文件是最后的手段,如果 Windows 无法加载用户配置文件。注销后,它们将被删除并 所有数据都丢失了。这当然是避免使用它们的原因。

【讨论】:

    【解决方案3】:

    因为您正在获取 WMI 对象,所以您可以使用 Remove-WMIObject cmdlet。

    因此,像这样简单地修改删除循环应该可以正确且完全地删除所需的配置文件:

    Foreach($deletedprofile in $profilestodelete)
        {
            Remove-WMIObject $deletedprofile
        }
    

    【讨论】:

      猜你喜欢
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      相关资源
      最近更新 更多