qshinoの日記

Powershell関係と徒然なこと

IIS FTPサイト構築 by Powershell

IIS-FTPサイト自動構築

Windows 2012R2 のPowerShellIIS WebAdministration Moduleを使って自動構築を試みる。

これらをMSではPowerShell IISスナップインと呼んでいる。ここでは以後スナップインと呼ぶ。

スナップインは3つのカテゴリに分かれる。

  1. プロバイダー階層で必要なコマンドレット
  2. 低レベル構成用のコマンドレット
  3. タスクベースのコマンドレット

IISの階層

  1. サイト
  2. アプリケーション
  3. 仮想ディレクト
  4. アプリケーションプール

作業概要

  1. IIS FTPサーバー導入

    サーバーマネージャーからFTPサーバーを導入。FTP拡張は未導入

  2. FTPサイト構築
  3. フォルダ マップ
  4. ユーザーアクセス許可
  5. Firewall許可

IIS-FTPサーバー導入

下記に基づきFTPサーバー導入

https://technet.microsoft.com/ja-jp/library/ee790599.aspx

FTPサイト構築

ここから先はPowerShellにて。まずはモジュールのインポート

Import-module WebAdministration

その後、FTPサイト構築

$ftpsite = "myftpsite"
$ftpdir = "C:\ftproot"
New-WebFtpSite -Name $ftpsite -Port 21 -PhysicalPath $ftpdir
ls iis:\sites

ここまででFTPサイト構築完了

フォルダmap

ユーザー作成

$computer = [ADSI]"WinNT://."
$user = $computer.Create("user", "DemoAppPoolUser")
$user.SetPassword("Secret!!Pw3009")
$user.SetInfo()

ユーザーアクセス許可

IIS上のユーザー作成と許可

$username = "ninja"
$password = "Koga4649!"
$access = "Read,Write"
# $pspath は下記の何方かの書式
$pspath = "computername/webroot/apphost"
$pspath = "IIS:\Sites\$ftpsite"
$pspath = IIS:\
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Management")  
[Microsoft.Web.Management.Server.ManagementAuthentication]::CreateUser($username, $password) 
[Microsoft.Web.Management.Server.ManagementAuthorization]::Grant($username, $ftpsite, $false)

Add-WebConfigurationProperty -pspath $pspath -location "$ftpsite" -filter "system.ftpServer/security/authorization" -name "." -value @{accessType='Allow';users="$username";permissions="$access"}
  • Add-WebConfigurationProperty

https://technet.microsoft.com/ja-jp/library/ee790572.aspx

  • 関連

https://technet.microsoft.com/ja-jp/library/ee155433.aspx

FireWall許可

サイト停止と起動

標準ではFTPサイト構築時にstateがStartedになり起動する。サイトの停止、起動方法。

(get-Website -Name $ftpsite).ftpserver.stop()

(get-Website -Name $ftpsite).ftpserver.start()

構築後記

PSで何度設定しても、IIS:\ではStatusがStartedにならず、$site.ftpserver.start() ではクラス名のエラーになり悩んでいたら、FTPサーバーを導入していなかったという顛末。導入したら、サクッとStartedになり、今までの苦労が何だったのか!

と言う訳で、皆様が同じ轍を踏まない事を祈ります。

参考

ftpserver.userisolation.mode

  • 0 NoIsolate, StartInUserDir
  • 1 Isolate, Share global virtualDir
  • 2 ActiveDirectory
  • 3 Isolate, No global virtual directory
  • 4 NoIsolation, Share all
  • 5 Custom

詳細は下記を参照

https://msdn.microsoft.com/ja-jp/library/ff713756(v=vs.90).aspx

MS WEBサイト+PSチュートリアル

https://technet.microsoft.com/ja-jp/library/ee155440.aspx

人手によるIIS-FTP構築

https://www.iis.net/learn/publish/using-the-ftp-service/configuring-ftp-user-isolation-in-iis-7

FTPサイト起動 https://peter.hahndorf.eu/blog/iisftp.html

FTPサイト構築設定 http://balamurugankailasam.blogspot.jp/2012/10/creating-ftp-site-map-ftp-folder-and.html?m=1

StackOverflow 最初に参考にしようとしたサイト

http://stackoverflow.com/questions/23522557/set-permissions-and-settings-on-iis-ftp-site-using-powershell

Technet

https://technet.microsoft.com/ja-jp/library/ee790599.aspx

FTPユーザー作成

https://forums.iis.net/t/1203117.aspx

ftpユーザー分離

https://technet.microsoft.com/ja-jp/library/dd939054.aspx