简体   繁体   English

无需用户干预即可自动执行磁盘清理 cleanmgr.exe 的过程

[英]Automate process of Disk Cleanup cleanmgr.exe without user intervention

I am developing a powershell script file which shall execute some disk cleanup without user intervention.我正在开发一个 powershell 脚本文件,该文件将在没有用户干预的情况下执行一些磁盘清理。 The user shall not be able to configure anything.用户不能配置任何东西。

When I run cleanmgr.exe /dc: sageset:1 a popup window appears to select files/folders to be cleaned(cleanup options).当我运行cleanmgr.exe /dc: sageset:1时,会出现一个弹出窗口来选择要清理的文件/文件夹(清理选项)。

This will create a registry entry containing the settings with the cleanup options and after this, you can run cleanmgr.exe /sagerun:1 which will actually execute the cleanup.这将创建一个注册表项,其中包含带有清理选项的设置,之后,您可以运行cleanmgr.exe /sagerun:1 ,这将实际执行清理。

Is there a way to specify the cleanup options directly with powerhell/command line(without the need to manually select things to be deleted)?有没有办法直接使用 powerhell/命令行指定清理选项(无需手动选择要删除的东西)?

The following Powershell script automates CleanMgr.exe.以下 Powershell 脚本自动执行 CleanMgr.exe。 In this case, it removes temporary files and runs the Update Cleanup extension to purge superseded Service Pack Backup files (Windows 10 now does this automatically via a scheduled task).在这种情况下,它会删除临时文件并运行更新清理扩展以清除被取代的 Service Pack 备份文件(Windows 10 现在通过计划任务自动执行此操作)。 To automate other extensions, create a "StateFlags0001" property in the corresponding Registry key, as done in the New-ItemProperty lines.要自动化其他扩展,请在相应的注册表项中创建一个“StateFlags0001”属性,如在 New-ItemProperty 行中所做的那样。 You will find the Registry key names in the "VolumeCaches" branch.您将在“VolumeCaches”分支中找到注册表项名称。

As far as being silent, this script attempts to start CleanMgr.exe in a hidden window.至于保持沉默,此脚本会尝试在隐藏窗口中启动 CleanMgr.exe。 However, at some point CleanMgr spawns new processes which are visible and must be waited on separately.但是,在某些时候 CleanMgr 会产生新的进程,这些进程是可见的并且必须单独等待。

Write-Host 'Clearing CleanMgr.exe automation settings.'
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' -Name StateFlags0001 -ErrorAction SilentlyContinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue

Write-Host 'Enabling Update Cleanup. This is done automatically in Windows 10 via a scheduled task.'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -Name StateFlags0001 -Value 2 -PropertyType DWord

Write-Host 'Enabling Temporary Files Cleanup.'
New-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -Name StateFlags0001 -Value 2 -PropertyType DWord

Write-Host 'Starting CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden -Wait

Write-Host 'Waiting for CleanMgr and DismHost processes. Second wait neccesary as CleanMgr.exe spins off separate processes.'
Get-Process -Name cleanmgr,dismhost -ErrorAction SilentlyContinue | Wait-Process

$UpdateCleanupSuccessful = $false
if (Test-Path $env:SystemRoot\Logs\CBS\DeepClean.log) {
    $UpdateCleanupSuccessful = Select-String -Path $env:SystemRoot\Logs\CBS\DeepClean.log -Pattern 'Total size of superseded packages:' -Quiet
}

if ($UpdateCleanupSuccessful) {
    Write-Host 'Rebooting to complete CleanMgr.exe Update Cleanup....'
    SHUTDOWN.EXE /r /f /t 0 /c 'Rebooting to complete CleanMgr.exe Update Cleanup....'
}

您可以使用cleanmgr /verylowdisk以静默方式自动执行所有清理步骤。

The PowerShell logic provided below is dynamic and ready for use or automation with the sageset options all being selected and no user interaction being required.下面提供的 PowerShell 逻辑是动态的,可随时使用或自动化,所有sageset选项均已选中,无需用户交互。 This was inspired by multiple answers and comments from this post.这是受到这篇文章的多个答案和评论的启发。

Note: I've adjusted for my needs and used successfully without any issues on multiple remote and local Windows 10 systems in particular.注意:我已经根据自己的需要进行了调整,并且在多个远程和本地 Windows 10 系统上成功使用并没有任何问题。

Run on Local System在本地系统上运行

Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' | % {
    New-ItemProperty -Path $_.PSPath -Name StateFlags0001 -Value 2 -PropertyType DWord -Force
   };
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' ##-WindowStyle Hidden

Run on Remote System在远程系统上运行

$cred = Get-Credential "domain\administrator";
Invoke-Command -ComputerName "computer004" {
    Process {
        Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' | % {
            New-ItemProperty -Path $_.PSPath -Name StateFlags0001 -Value 2 -PropertyType DWord -Force
           };
        Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:1' -WindowStyle Hidden
    }
} -AsJob -Credential $cred 

Supporting Resources支持资源

The only solution I found is to manually set the registry values like this:我找到的唯一解决方案是手动设置注册表值,如下所示:

... ...

#Set StateFlags0012 setting for each item in Windows 8.1 disk cleanup utility
if (-not (get-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -ErrorAction SilentlyContinue)) {
set-itemproperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders' -name StateFlags0012 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache' -name StateFlags0012 -type DWORD -Value 2
set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files' -name StateFlags0012 -type DWORD -Value 2

... ...

see full example 查看完整示例

I ran into the same issue.我遇到了同样的问题。 Researching the possible ways, I have found the following: http://stealthpuppy.com/cleaning-up-and-reducing-the-size-of-your-master-image/研究可能的方法,我发现了以下内容: http ://stealthpuppy.com/cleaning-up-and-reducing-the-size-of-your-master-image/

It shows how to create the sageset registry settings via cmd.它展示了如何通过 cmd 创建 sageset 注册表设置。 You can then use the sagerun:# cmd.然后,您可以使用 sagerun:# cmd。 I have not tried it via script yet, but have validated that it works...我还没有通过脚本尝试过它,但已经验证它可以工作......

This script will get all the Volume Caches from the Registry, enable them to be cleaned and run the CLEANMGR.EXE for all caches.此脚本将从注册表中获取所有卷缓存,使它们能够被清理并为所有缓存运行 CLEANMGR.EXE。

$VolumeCachesRegDir = "hklm:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
$CacheDirItemNames = Get-ItemProperty "$VolumeCachesRegDir\*" | select -ExpandProperty PSChildName

$CacheDirItemNames | 
    %{
        $exists = Get-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name "StateFlags6553" -ErrorAction SilentlyContinue
        If (($exists -ne $null) -and ($exists.Length -ne 0))
            {
                Set-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name StateFlags6553 -Value 2
            }
        else
            {
                New-ItemProperty -Path "$VolumeCachesRegDir\$_" -Name StateFlags6553 -Value 0 -PropertyType DWord
            }
     }
Start-Sleep -Seconds 3


Write-Host 'Running CleanMgr.exe...'
Start-Process -FilePath CleanMgr.exe -ArgumentList '/sagerun:65535' -WindowStyle Hidden -PassThru
cls

Running CleanMgr.exe in a powershell script or by itself seems to work fine as long as you run it locally with an account that has local admin rights.只要您使用具有本地管理员权限的帐户在本地运行 CleanMgr.exe,在 powershell 脚本中或单独运行它似乎都可以正常工作。 But try running it remotely via any remote management tool or remote scripting command (Invoke-Command) and it does not run.但是尝试通过任何远程管理工具或远程脚本命令(Invoke-Command)远程运行它,它不会运行。 You might see the process running on the remote system but it doesn't seem to cleanup anything and the process never ends.您可能会看到该进程在远程系统上运行,但它似乎没有清理任何东西,并且该进程永远不会结束。 I would be interested if anyone has been able to get cleanmgr.exe to run remotely without any user interaction.如果有人能够让 cleanmgr.exe 在没有任何用户交互的情况下远程运行,我会很感兴趣。 EG ConfigMgr Right Click Tools, ConfigMgr App or PKG, Task Scheduler. EG ConfigMgr 右键单击​​工具、ConfigMgr 应用程序或 PKG、任务计划程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM