PowerShellでIISのHTTP応答ヘッダー(共通)を変更


PowerShellを活用すれば、IISのあらゆる操作がコマンドで実行可能です。
環境構築の自動化・効率化に是非お役立てください。

今回はクライアントキャッシュの設定変更。

IISを立ち上げて、該当のサイトを選択して、「HTTP応答ヘッダー」を選択して、右メニューの「共通ヘッダーの設定」をクリックして…なんて手順書とはもうお別れです。

おまじない

#管理者権限で実行
Start-Process powershell.exe -Verb runas
#サイト名
$siteName = 'Default Web Site'

クライアントキャッシュの削除設定

設定しない(「期限切れのWebコンテンツ」のチェックを外す)
Set-WebConfigurationProperty `
    -PSPath "MACHINE/WEBROOT/APPHOST/$siteName" `
    -Name . -filter "system.webServer/staticContent/clientCache" `
    -Value (@{cacheControlMode="NoControl";})
即時
Set-WebConfigurationProperty `
    -PSPath "MACHINE/WEBROOT/APPHOST/$siteName" `
    -Name . -filter "system.webServer/staticContent/clientCache" `
    -Value (@{cacheControlMode="DisableCache";})
失効までの期間
Set-WebConfigurationProperty `
    -PSPath "MACHINE/WEBROOT/APPHOST/$siteName" `
    -Name . -filter "system.webServer/staticContent/clientCache" `
    -Value (@{cacheControlMode="UseMaxAge";cacheControlMaxAge="02:00:00";})
有効期限
Set-WebConfigurationProperty `
    -PSPath "MACHINE/WEBROOT/APPHOST/$siteName" `
    -Name . -filter "system.webServer/staticContent/clientCache" `
    -Value (@{cacheControlMode="UseExpires";httpExpires="Tue, 19 Jan 2038 03:14:07 GMT";})

正しく設定が反映されたか、IISの画面から(一度は)確認しておきましょう。

参考:公式マニュアル
https://docs.microsoft.com/en-us/iis/configuration/system.webserver/staticcontent/clientcache