【发布时间】: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