1

我对脚本比较陌生,下面是我用来清理一天前的临时文件的脚本,但我想构建一个不会删除活动(登录)用户的临时文件并通知哪些对象的检查被跳过。

#>
$oldTime = [int]1 # 1 day 
# Create array containing all user profile folders
$colProfiles = Get-ChildItem "C:\Documents and Settings\" -Name
# Remove the "All Users" profile from the array
$colProfiles = $colProfiles -ne "All Users"

# Removes temporary files from each user profile folder
ForEach ( $objProfile in $colProfiles ) {
    # Remove all files and folders in user's Temp folder
    Get-ChildItem "C:\Documents and Settings\$objProfile\Local Settings\Temp\*" -recurse | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$oldTime))} | remove-item -force -recurse
    # Remove all files and folders in user's Temporary Internet Files. The -force switch on Get-ChildItem gets hidden directories as well.
    Get-ChildItem "C:\Documents and Settings\$objProfile\Local Settings\Temporary Internet Files\*" -recurse -force | WHERE {($_.CreationTime -le $(Get-Date).AddDays(-$oldTime))} | remove-item -force -recurse
4

1 回答 1

0

您可以在删除文件之前测试用户名:

if ($objProfile -neq ( gwmi Win32_ComputerSystem ).username.split('\')[1]))
{ .. can delete ..}

或者

if ($objProfile -neq [environment]::UserName)
    { .. can delete ..}
于 2012-05-11T19:38:41.120 回答