SCCMでディスクを自動クリーンアップ

SCCMを使っていると、いろいろなことをやってみたくなります。
今回は、SCCMを使ってディスクを自動クリーンアップすることにします。

ただし、普通にパッケージを展開しても芸がありません。
ミソは、C:ドライブの空き容量を定期的にチェックし、一定の容量を下回った場合に自動クリーンアップされるようにする、というものです。

アプリケーションの作成

  1. まず、自動クリーンアップするためのアプリケーションを作成します。ここでパッケージではなくアプリケーションを使用する理由は後述します。

    1. 今回は、2つのスクリプトを用意しました。
      1つにまとめても良いのですが、もともと、こちらの情報を参考にcleanmgr.exe を自動実行するPowershellスクリプトを作成し、これに加えてテンポラリディレクトリ、SCCMのキャッシュフォルダのクリアもしたかったため、このような構成になりました。単に手抜きです。

      [参考] Greg's Systems Management Blog - Automating the Disk Cleanup Utility
      https://gregramsey.net/2014/05/14/automating-the-disk-cleanup-utility/

      こちらのバッチファイルで、テンポラリディレクトリのクリア、別のPowershellスクリプトの呼び出しを行います。

      auto_cleanmgr.bat

      powershell.exe -command Set-ExecutionPolicy Unrestricted
      del /Q c:\windows\temp\*.*
      powershell.exe -command \\m-mgmt-sccm01\smspkgg$\scripts\auto_cleanmgr.ps1
      exit 0
      こちらPowershellスクリプトで、SCCMキャッシュのクリア、cleanmgr.exeの自動実行を行います。

      auto_cleanmgr.ps1

      ######################################################### Capture current free disk space on Drive C

      $FreespaceBefore = (Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'" | select Freespace).FreeSpace/1GB

      ######################################################### Clear ccmcache of SCCM client

      $resman = New-Object -ComObject "UIResource.UIResourceMgr"
      $cacheInfo = $resman.GetCacheInfo()
      "clearing ccmcache. . ."
      $cacheInfo.GetCacheElements() | ForEach{ $cacheInfo.DeleteCacheElement($_.CacheElementID) }

      ######################################################### CleanMgr.exe Automation
      #
      #http://gregramsey.net
      #more info here:
      #http://support.microsoft.com/kb/253597

      #ensure we're running on windows 8.1 first
      #if ((Get-CimInstance win32_operatingsystem).version -eq '6.3.9600') {

      #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
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Memory Dump Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Previous Installations' -name StateFlags0012 -type DWORD -Value 2
      # set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Service Pack Cleanup' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Setup Log Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error memory dump files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\System error minidump files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Setup Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Thumbnail Cache' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Update Cleanup' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Upgrade Discarded Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\User file versions' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Defender' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Archive Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting Queue Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Archive Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Error Reporting System Queue Files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows ESD installation files' -name StateFlags0012 -type DWORD -Value 2
      set-itemproperty -path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Windows Upgrade Log Files' -name StateFlags0012 -type DWORD -Value 2
      }

      cleanmgr /sagerun:12

      do {
      "waiting for cleanmgr to complete. . ."
      start-sleep 15
      } while ((get-wmiobject win32_process | where-object {$_.processname -eq 'cleanmgr.exe'} | measure).count)

      $FreespaceAfter = (Get-WmiObject win32_logicaldisk -filter "DeviceID='C:'" | select Freespace).FreeSpace/1GB

      "Free Space Before: {0}" -f $FreespaceBefore
      "Free Space After: {0}" -f $FreespaceAfter

      #}

    2. 通常通り、auto_cleanmgr.batを実行するアプリケーションを作成します。

      ここで1つ仕掛けをします。
      アプリケーションの展開の種類で、下記のような検出方法を設定します。

      • 「カスタムスクリプトを使用してこの展開の種類のプレゼンスを検出する」を選択
      • スクリプトの種類」はVBScriptを選択して、下記のようなスクリプトを設定
        set fsobj = CreateObject("Scripting.FileSystemObject")
        set d1 = fsobj.GetDrive("C")
        If d1.FreeSpace/1024/1024>10024 Then
        WScript.StdOut.WriteLine d1.FreeSpace/1024/1024
        End If
        WScript.Quit(0)
      検出方法のスクリプトでは、スクリプトの出力がなにかあった場合、正常に検出されたことになります。逆に、何も出力が無いと、アプリケーションは未検出となり、インストールを実行しようとします。これを利用して、スクリプトでディスクの空き容量をチェックし、一定容量(このスクリプトでは約1GB)を上回った場合だけメッセージを出力するようにしておきます。このことで、検出の結果、アプリケーションが未検出と認識されて、スクリプトが実行されます。
      これが、パッケージではなくアプリケーションを使用した理由です。

    3. あとは、通常通りこのアプリケーションを展開するだけです。
以上の方法で実際に動作させることができますが、これとは別にディスクの空き容量が少ないPCだけを選択するデバイスコレクションを作成しました。このコレクションに展開設定をしておけば、実行が不要なデバイスにも展開する必要がなくなります。

コレクションの作成

方法は下記のとおりです。
  1. あらかじめ、ハードウェアインベントリのなかでディスクの空き容量を取得するよう、クライアント設定を変更します。これは、デフォルトではハードウェアインベントリでディスクの空き容量は取得する設定になっていないためです。

    1. 適用されているクライアント設定のダイアログで、ハードウェアインベントリ - ハードウェアインベントリクラスの「クラスの設定」ボタンをクリックします
    2. ツリーの中にある、論理ディスク - 空き容量(MB)のチェックを入れます

  2. バイスコレクションの作成の際、メンバシップ規則に、下記のようなクエリを設定します。
    select * from SMS_R_System full join SMS_G_System_LOGICAL_DISK as LDISK on LDISK.ResourceID = SMS_R_System.ResourceId where LDISK.FreeSpace <= 1024 and LDISK.DeviceID = "C:"
    この例では、C:ドライブの空き容量が1GBを下回ったデバイスのみがデバイスコレクションのメンバーになります。