none
Understanding shell vs task scheduler behavior RRS feed

  • Question

  • Hi, I have some simple code that aims to run cleanmgr.exe via powershell. The problem boils down to this - looking at the following snippet:

    $Process = (Start-Process -FilePath "$env:systemroot\system32\cleanmgr.exe" -ArgumentList "/sagerun:1" -PassThru)
    $Process.WaitForExit()
    write-output "Process ended with exitcode [$($Process.ExitCode)]."

    If this is run via a command prompt, it works fine. If run via Task Scheduler (highest privs, whether user is logged on or not), it runs, but it never complets. In the latter case, a 'Disk Space Cleanup Manager for Windows' background process runs, does its job, but never exits. Using task manager, I can 'end' this process and the above script completes. I have no idea how to address this (or even make it work in both cases). The only 'hack' I can think of is to go into a loop and monitor the CPU usage of the cleanmgr.exe process and wait for it to be complete, then kill the task.

    I am running this on Windows 10 Pro 1903.

    Friday, May 24, 2019 6:56 PM

All replies

  • This is not a task scheduler forum.  s you posted, the script runs correctly.  It is the task scheduler that has the issue.

    There is actually no need to use a script.  Just run the command as a task.  That is how it is setup when you select "autoclean"

    See the documentation for cleanMgr.

    It is also not possible to correctly wait for CleamMgr.


    \_(ツ)_/


    Friday, May 24, 2019 7:08 PM
    Moderator
  • This is not a task scheduler forum.  s you posted, the script runs correctly.  It is the task scheduler that has the issue.

    There is actually no need to use a script.  Just run the command as a task.  That is how it is setup when you select "autoclean"

    See the documentation for cleanMgr.

    It is also not possible to correctly wait for CleamMgr.


    \_(ツ)_/


    I don't understand the 'no need to use a script'. I have an existing infrastructure that uses task scheduler on system boot to download a powershell file from a central server, and execute it. In that powershell script I add various administrative tasks. With the recent windows update I was adding this cleanmgr so i can remove windows.old from several servers. Neither remove-item or even rmdir work without permission issues.

    Forget task scheduler. Run the script using 'psexec -s powershell.exe -executionpolicy bypass' and the same issue occurs. I think it has something to do with running detached from the current session (system user), even though the 'sagerun' requires no user interaction (assuming properly setup sageset - which I didn't include above).

    And what do you mean by 'not possible to correctly wait for cleanmgr'? Running the code via powershell works fine except when running under the psexec -s (or task scheduler) example.

    Friday, May 24, 2019 7:32 PM
  • There is ni need for PsExec.  Just run CLeanMgr directly.

    You cannot wait on CleanMgr,  It runs in the background under the system. 

    cleanmgr /autoclean

    This removes all upgrade and installation files.   You can use a GPO to run this or just schedule it to run on a boot.  It will not effect an upgrade in progress.

    You are overcomplicating you management tasks.  This is normal for new users of Windows who have not learned the overall management philosophy of Windows as defined by Microsoft.


    \_(ツ)_/

    Friday, May 24, 2019 8:06 PM
    Moderator
  • The psexec example was an attempt to reproduce the issue without task scheduler. I was trying to demonstrate the problem so others could reproduce / understand.

    cleanmgr.exe has hardly any documentation that I can find. Are you sure /autoclean removes windows.old? Or is it just telling the process to check if it is 30 days old and then remove it? I want to remove it immediately. I just tried running it and it did not remove windows.old.

    A basic purpose of scripting is to automate things. If there is no way to determine when an action begins or ends, that is not very helpful. In my case, I power some of these machines on to simply update them and power them off when completed..I let the script handle this. Is there no Event that can be monitored when cleanmgr starts / stops running that powershell could intercept?

    As far as overcomplicating...It certainly seemed like adding a few lines to a single script stored in a central location that runs on a dozen machines was easier than having to manipulate each machine individually.

    Friday, May 24, 2019 8:38 PM
  • Friday, May 24, 2019 9:42 PM
    Moderator
  • Excerpt:

    cleanmgr.exe /AUTOCLEAN
     It's the same as above, but the application will remove files from the previous Windows installation or the previous in-place upgrade automatically.
    
    The following folders will be removed:
    C:\$Windows.~BT\*
    C:\$Windows.~LS\*
    C:\$Windows.~WS\*
    C:\ESD\Download\*
    C:\ESD\Windows\*
    C:\$WINDOWS.~Q\*
    C:\$INPLACE.~TR\*
    C:\Windows.old\*
    C:\Windows\Panther
    


    \_(ツ)_/


    Friday, May 24, 2019 9:43 PM
    Moderator
  • That webpage is very helpful and detailed in its description, thank you. Unfortunately, it doesn't work, which makes me wonder if there is some 30 day check or something else. Looking at that log file I see it runs and even counts the 7-8GB, but it doesn't actually remove C:\windows.old.

    2019-05-24 18:08:29, Info                         SetupClnLogInitializationInfo - Getting plugin name
    2019-05-24 18:08:29, Info                         SetupClnLogInitializationInfo - session being initialized for Plugin: Previous Installations
    2019-05-24 18:08:29, Info                         Client Process:C:\WINDOWS\system32\cleanmgr.exe
    2019-05-24 18:08:29, Info                         pszWellKnownDirs:(null)
    2019-05-24 18:08:29, Info                         bCleanWindowsOld:1
    2019-05-24 18:08:29, Info                         bRemoveUninstall:1
    2019-05-24 18:08:29, Info                         bShowOnlyInUpgrade:0
    2019-05-24 18:08:29, Info                         bShowOnlyInESDInstall:0
    2019-05-24 18:08:29, Info                         bSilentCleanup:1
    2019-05-24 18:08:29, Info                         bCleanupTemporaryInstallationFiles:0
    2019-05-24 18:08:29, Info                         CSetupFilesCleanup::GetSpaceUsed - Calculating the space for session: Previous Installations
    2019-05-24 18:08:29, Info                         IsProcessElevated - bResult: 1
    2019-05-24 18:08:34, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\WINDOWS\System32\LogFiles\WMI\RtBackup\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:34, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\WINDOWS\System32\LogFiles\WMI\RtBackup\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\Trace\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\Trace\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\Cyber\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\Cyber\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\Cache\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Windows Defender Advanced Threat Protection\Cache\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Diagnosis\TimeTravelDebuggingStorage\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Diagnosis\TimeTravelDebuggingStorage\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Diagnosis\TenantStorage\P-ARIA\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Diagnosis\TenantStorage\P-ARIA\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Diagnosis\FeedbackHub\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:38, Error                        EnumeratePathEx: FindFirstFile failed for [C:\Windows.old\ProgramData\Microsoft\Diagnosis\FeedbackHub\*]; GLE = 0x5[gle=0x00000005]
    2019-05-24 18:08:39, Info                         CSetupFilesCleanup::CleanVolume - Return status: 0
    2019-05-24 18:08:39, Info                         CSetupFilesCleanup::GetSpaceUsed - Space used : 7903076352
    2019-05-24 18:08:39, Info                         CSetupFilesCleanup::GetSpaceUsed - Block status: 0
    2019-05-24 18:08:39, Info                         Initialization status:0

    Friday, May 24, 2019 10:15 PM
  • I ran it and it removed all of those files.

    You are getting access denied errors (5).   You need to run it elevated.


    \_(ツ)_/

    Friday, May 24, 2019 10:26 PM
    Moderator
  • I ran it and it removed all of those files.

    You are getting access denied errors (5).   You need to run it elevated.


    \_(ツ)_/

    I am and as a confirmation it even says right in the pasted log file. If you were to run it non-elevated it will just say ' Skipping - Previous Installations' and move onto the next item. When I run it using the 'sagerun' method like I started to script via powershell, it works.

    2019-05-24 18:08:29, Info                         IsProcessElevated - bResult: 1

    Friday, May 24, 2019 10:34 PM
  • Remember that you cannot wait on this utility and the cleanup can take a very long time. 

    This is not a scripting issue.  If you are having issues you need to post in the OS version "General" or "installation" forum.


    \_(ツ)_/


    Friday, May 24, 2019 10:36 PM
    Moderator
  • I ran it and it removed all of those files.

    You are getting access denied errors (5).   You need to run it elevated.


    \_(ツ)_/

    I am and as a confirmation it even says right in the pasted log file. If you were to run it non-elevated it will just say ' Skipping - Previous Installations' and move onto the next item. When I run it using the 'sagerun' method like I started to script via powershell, it works.

    2019-05-24 18:08:29, Info                         IsProcessElevated - bResult: 1

    You still cannot wait on the process.  It is not running in your session.  It is being run by the OS.


    \_(ツ)_/

    Friday, May 24, 2019 10:38 PM
    Moderator
  • Remember that you cannot wait on this utility and the cleanup can take a very long time. 


    \_(ツ)_/

    I waited about 10 minutes before I declared it didn't work. The cleanup task/process was no longer running and had closed the logfile. Unless it just queues the request and it runs at some future time/date, I don't know. When I ran using sagerun, I saw the process run immediately, CPU/Disk utilization increased, and it finished in about 4 minutes.

    Thanks for trying to help / testing on your side.

    I think I will simply go back to my original plan which is launch via powershell using sageset/sagerun, then either monitor the CPU usage of the cleanmgr.exe task or monitor the 'C:\Windows\System32\LogFiles\setupcln\setupact.log' file for it indicate completion. I've tried /AUTOCLEAN on 5 different systems and it hasn't worked on any of the them.


    Friday, May 24, 2019 10:47 PM
  • Friday, May 24, 2019 11:03 PM
    Moderator