给定一个文件夹中,说\\localhost\c$\work\

我想运行的powershell脚本每15分钟,以确保自由空间5GB可用。

如果<5GB是可用的,工作中除去最近最少使用的文件夹,直到> 5GB可用。

思想?

有帮助吗?

解决方案

要安排任务,您可以使用任务调度器(例如这里

有关的脚本,你可以使用

param($WorkDirectory = 'c:\work'
    , $LogFile = 'c:\work\deletelog.txt' )

#Check to see if there is enough free space
if ( ( (Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace / 1GB ) -lt 5)
{
    #Get a list of the folders in the work directory
    $FoldersInWorkDirectory = @(Get-ChildItem $WorkDirectory | Where-Object {$_ -is [System.IO.DirectoryInfo]} | Sort-Object -Property LastWriteTime -Descending)
    $FolderCount = 0

    while ( ( (Get-WmiObject -Query "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'").FreeSpace / 1GB ) -lt 5)
    {
            #Remove the directory and attendant files and log the deletion
        Remove-Item -Path $FoldersInWorkDirectory[$FolderCount].FullName -Recurse
            "$(Get-Date) Deleted $($FoldersInWorkDirectory[$FolderCount].FullName)" | Out-File -Append $LogFile

            $FolderCount++
    } 
}

其他提示

嗯,这一切都取决于你的文件夹“拿来主义”。如果没有简单的指标,你可以尝试使用.NET FileSystemWatcher的班寻找变化并记住他们,以及在队列中的时间戳,根据访问时间排序。然后,你可以选择下一个文件夹,从队列中删除。但它肯定不漂亮。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top