我正在尝试创建一个任务,该任务以后将与本地计算机上的taskch一起执行,该任务每天都会运行。
任务是,如果用户尚未登录计算机,则应将其从计算机中删除。
问题出在我的代码有问题,我不知道是什么 现在,我试图将该脚本删除testuser 1,2,3,并在testuser 4上使用lastusetime表示Account不是旧的。
操作系统名称:Microsoft Windows 10 Enterprise 操作系统版本:10.0.17763 N / A Build 17763
@ {Sid = S-1-5-21-3824012622-276487612-2647460976-1105; LocalPath = C:\ Users \ testuser4; LastUseTime = 2019-05-13 19:27:57}
@ {Sid = S-1-5-21-3824012622-276487612-2647460976-1109; LocalPath = C:\ Users \ testuser3; LastUseTime = 2019-05-10 14:54:07}
@ {Sid = S-1-5-21-3824012622-276487612-2647460976-1108; LocalPath = C:\ Users \ testuser2; LastUseTime = 2019-05-10 14:54:07}
@ {Sid = S-1-5-21-3824012622-276487612-2647460976-1107; LocalPath = C:\ Users \ testuser1; LastUseTime = 2019-05-10 13:49:16}
# Start PS-Code
$DeleteIfLastUseTimetIsOlderThen = (get-date).AddDays(-5).tostring("yyyy-MM-dd hh:mm:ss”)
$GetLocalAccounts = Get-WmiObject -ComputerName localhost -Filter "Special=False" -Class Win32_UserProfile |
Select-Object Sid, LocalPath, @{Label='LastUseTime';Expression={$_.ConvertToDateTime($_.LastUseTime)} }
foreach ($UserAccount in $GetLocalAccounts)
{
if ($GetLocalAccounts.LastUseTime -Ge $DeleteIfLastUseTimetIsOlderThen )
{ Write-host "Account is old, Remove me"}
Else
{ Write-host "Account is not old"}
}
# End PS-Code
问题是,将值“ $ DeleteIfLastUseTimetIsOlderThen”更改为1,2,3,4或55都没关系,
答案 0 :(得分:0)
这里的问题是双重的。一种是由于过去的日期小于将来的日期,因此您想使用小于比较来查找较旧的日期。其次,您想将集合($GetLocalAccounts
)中的每个日期与特定的固定日期($DeleteIfLastUseTimeIsOlderThan
)进行比较。要使用代码结构实现此目的,您将需要检查for循环中的当前对象($UserAccount
)。
# Start PS-Code
$DeleteIfLastUseTimetIsOlderThan = (get-date).AddDays(-5).tostring("yyyy-MM-dd hh:mm:ss”)
$GetLocalAccounts = Get-WmiObject -ComputerName localhost -Filter "Special=False" -Class Win32_UserProfile |
Select-Object Sid, LocalPath, @{Label='LastUseTime';Expression={$_.ConvertToDateTime($_.LastUseTime)} }
foreach ($UserAccount in $GetLocalAccounts)
{
if ($UserAccount.LastUseTime -le $DeleteIfLastUseTimetIsOlderThan )
{ Write-host "Account is old, Remove me"}
Else
{ Write-host "Account is not old"}
}
# End PS-Code