7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

IIS上の設定をPowerShellから操作する。

Posted at

アプリのデプロイ時に仮想ディレクトリの参照先を変更する仕組みが必要になった。
実際にやったことのメモなので仮想ディレクトリの物理パス(physicalPath)の設定を例に。

前提

WebAdministration モジュールを読み込むと、IISの管理階層をPSドライブとして読み込むことができる。

# IISを操作するモジュールの読み込み
PS> Import-Module WebAdministration
PS> cd iis:\
PS> ls

Name       
----       
AppPools   
Sites      
SslBindings

値の取得

値の取得方法はいくつかあるのでお好みで。
ここではデフォルトサイト直下に sampleDir という仮想ディレクトリがある前提で。

PS> Get-ItemProperty -Path 'IIS:\Sites\Default Web Site\sampleDir' -Name physicalPath | select -Exp value
PS> Get-Item 'IIS:\Sites\Default Web Site\sampleDir' | select -exp physicalPath

# WebAdministration のCmdletを使うと以下の感じ
PS> Get-WebVirtualDirectory -Site "Default Web Site" -Name sampleDir | select -exp physicalPath

値の設定

値の設定もいくつかやり方ありそうな気がするが、簡単だったのが以下の方法

PS> Set-ItemProperty -Path 'IIS:\Sites\Default Web Site\sampleDir' -Name physicalPath -Value C:\tmp\sample2

所感

  • 個人的には取得は Get-Item が扱いやすかった。
    • サイト名とかアプリ名とかバラバラに設定持つ場合は Get-WebVirtualDirectory の方がいいけど、コンフィグにフルパス書いた方が楽。
  • Set-WebConfigurationProperty とか使えたほうが便利なのかもしれないけど、調べてた時にいまいちピンと来なかった。
  • Get-Item で取った情報を変更して直接更新かけれると楽そうなんだけど、SaveとかUpdate系の扱い方がわからなかった。
7
8
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
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?