我试图创建一个脚本来检测长达15天的域用户配置文件并删除它们。这将捆绑在一个工作中,在运行之前将会注销任何闲置会话。脚本没有正确地将WMI对象分配给阵列
我知道这通常是通过GPO完成的,但是由于各种原因它不适合用于这个特定业务领域。
这里是我的代码:
#Assign how old the user profile must be for deletion
[int]$NoOfDays = 15
#Get WMI object where it is a domain account and over 15 days old
$Objects = @(Get-WmiObject -Class Win32_UserProfile | Where {
(!$_.Special) -and
$_.ConvertToDateTime($_.LastUseTime) -lt (Get-Date).AddDays(-15)
})
if ($Objects -eq $null) {
#If no users returned, write to host.
Write-Host "no profiles found for deletion"
} else {
#If users are found, do the following
foreach ($Object in $Objects) {
Write-Host "'$($object.LocalPath)' has been identified for deletion"
}
foreach ($Object in $Objects) {
try {
Write-Host "Attempting to delete profile '$($Object.LocalPath)'"
Remove-WmiObject
Write-Host "Deleted profile '$($Object.LocalPath)' successfully."
} catch {
Write-Host "Unable to delete profile '$($Object.LocalPath)'" -ErrorAction Continue
}
}
}
没有输出,它只是返回命令没有错误行,立竿见影。
我试图注释掉删除配置文件的位,在每个语句的第一个语句之后结束else,看看删除了哪些内容,但它没有起作用。
它似乎对删除-WmiObject可以cmdlet的一个部分的输入参数在上面的代码中给定的被错过。 –
@Andrei Odegov对不起,谢谢你指出。我设法使删除工作正常。 –