qshinoの日記

Powershell関係と徒然なこと

IIS-FTP userisolation by Powershell

PowerShellによるIIS FTPサイト構築

前提

  1. ユーザー分離はvdir非共有 $iso=3
  2. Read/Write可
  3. Win2012R2以降

情報取得

ls IIS:\
ls IIS:Sites\
ls IIS:\Sites\xx
$site = Get-Item IIS:\Sites\xx
$site
$ftp = Get-WebConfiguration -pspath IIS:\Sites\xx
$ftp

コード

$site = "site"
$path = "IIS:\Sites\$site"
$root = "C:\inetpub\ftproot"
$ldir   = "$root\LocalUser"
$anon = "$ldir\Public"
$iso = 3 # userisolation.mode No global vdir

Import-Module WebAdministration

mkdir $anon
icacls $anon /grant IUSR:(OI)(CI)R /T

New-WebFtpSite -Name "$site" -Port 21 -PhysicalPath $root

# ssl
Set-ItemProperty "$path" -Name ftpServer.security.ssl.controlChannelPolicy -Value 0
Set-ItemProperty "$path" -Name ftpServer.security.ssl.dataChannelPolicy -Value 0

# Basic Auth
Set-ItemProperty "$path" -Name ftpServer.security.authentication.basicAuthentication.enabled -Value $true

# user Isolation
Set-ItemProperty "$path" -Name ftpserver.userisolation.mode -Value $iso

# All Users:"read"/"write"
Add-WebConfiguration "/system.ftpServer/security/authorization" -value @{accessType="Allow";roles="";permissions="Read,Write";users="*"} -PSPath IIS:\ -location "$site"

# Restart site
Restart-WebItem "$path"

表示

$site = "site"
$path = "IIS:\Sites\$site"
$root = "C:\inetpub\ftproot"
$ldir   = "$root\LocalUser"
$anon = "$ldir\Public"
$iso = 3 # userisolation.mode No global vdir
icalcs $root
icacls $ldir
icacls $anon
ls IIS:\Sites\$site
# ssl.control
Get-ItemProperty "$path" -Name ftpServer.security.ssl.controlChannelPolicy
# ssl.data
Get-ItemProperty "$path" -Name ftpServer.security.ssl.dataChannelPolicy
# auth
Get-ItemProperty "$path" -Name ftpServer.security.authentication.basicAuthentication.enabled
# permission
$perm = Get-WebConfiguration "/system.ftpServer/security/authorization
$perm
# isolation
Get-ItemProperty "$path" -Name ftpserver.userisolation.mode

削除

Remove-WebSite -Name $site

バックアップ

$backupname="poi"
Backup-WebConfiguration -Name $backupname

リストア

$backupname="poi"
Restore-WebConfiguration -Name $backupname

作成

下記のそれぞれ作成方法

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

サイト

方法1 FTP

New-WebFtpSite -Name "$site" -Port 21 -PhysicalPath $root

方法1 http

New-Website -name "MyNewWebSite" -PhysicalPath "$env:systemdrive\inetpub\MyNewWebSite" -port 81

方法2

New-Item iis:\Sites\TestSite -bindings @{protocol="http";bindingInformation=":80:TestSite"} -physicalPath c:\test

アプリケーション

New-Item 'IIS:\Sites\Default Web Site\DemoApp' -physicalPath c:\test -type Application

仮想ディレクト

New-Item 'IIS:\Sites\Default Web Site\DemoVirtualDir1' -type VirtualDirectory -physicalPath c:\test\virtualDirectory1

アプリケーションプール

New-item IIS:\AppPools\DemoAppPool

纏めると

ディレクトリ作成

New-Item C:\DemoSite -type Directory
New-Item C:\DemoSite\DemoApp -type Directory
New-Item C:\DemoSite\DemoVirtualDir1 -type Directory
New-Item C:\DemoSite\DemoVirtualDir2 -type Directory 

コンテンツコピー

Set-Content C:\DemoSite\Default.htm "DemoSite Default Page"
Set-Content C:\DemoSite\DemoApp\Default.htm "DemoSite\DemoApp Default Page"
Set-Content C:\DemoSite\DemoVirtualDir1\Default.htm "DemoSite\DemoVirtualDir1 Default Page"
Set-Content C:\DemoSite\DemoVirtualDir2\Default.htm "DemoSite\DemoApp\DemoVirtualDir2 Default Page"

アプリケーションプール作成

New-Item IIS:\AppPools\DemoAppPool

サイト/vdir作成

New-Item IIS:\Sites\site -physicalPath C:\poi -bindings @{protocol="http";bindingInformation=":8080:"}
Set-ItemProperty IIS:\Sites\site -name applicationPool -value DemoAppPool
New-Item IIS:\Sites\DemoSite\DemoApp -physicalPath C:\DemoSite\DemoApp -type Application
Set-ItemProperty IIS:\sites\DemoSite\DemoApp -name applicationPool -value DemoAppPool
New-Item IIS:\Sites\DemoSite\DemoVirtualDir1 -physicalPath C:\DemoSite\DemoVirtualDir1 -type VirtualDirectory
New-Item IIS:\Sites\DemoSite\DemoApp\DemoVirtualDir2 -physicalPath C:\DemoSite\DemoVirtualDir2 -type VirtualDirectory

ユーザー分離モード

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

参考

バックアップ&リストア

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

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

IISでファイルアクセス権を与えるべきアカウント

https://www.compnet.jp/wordpress/archives/1219

IISアプリケーションプールと権限

http://blog.shibayan.jp/entry/20150127/1422369253

IIS7.0 組み込みユーザーとグループアカウント

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

ms site.start

https://msdn.microsoft.com/ja-jp/library/microsoft.web.administration.site.start(v=vs.90).aspx