LoginSignup
0
0

More than 3 years have passed since last update.

PowerShellでweb.configのsystem.webを編集

Last updated at Posted at 2021-04-08

以下のような設定ファイルがあるとして、sessionStateのtimeoutを120分から180分に変えたい。

web.config抜粋
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
        <system.web>
                <sessionState mode="InProc" stateConnectionString="tcpip:xx.xx.xx.xx"
                sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes"
                cookieless="false" timeout="120" />
        </system.web>
</configuration>

PowerShellから設定ファイルを編集するには、xmlをパースしてやりくりする方法もあるが、ここでは.NETのSystem.Configrationなるオブジェクトの力を借りたい。その名の通り設定ファイルを処理することに特化したオブジェクトである。

#configファイルをマッピング
Add-Type -AssemblyName System.Configuration
$Map = New-Object System.Configuration.ExeConfigurationFileMap
$Map.ExeConfigFilename = '.\web.config';
$config = [System.Configuration.ConfigurationManager]::OpenMappedExeConfiguration($Map, [System.Configuration.ConfigurationUserLevel]::None)

#セッションタイムアウトの設定
$config.GetSectionGroup('system.web').SessionState.Timeout = '03:00:00';

#ファイルに反映
$config.Save();

実行してみると、"03:00:00"が勝手にminuteに変換され"180"で更新されていることが確認できます。非常に優秀。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0