IIS - Managing FTP sites with PowerShell

26 June 2015
Managing FTP sites in IIS 7+ with PowerShell doesn't work quite as nicely as one would expect.
While there is a cmdlet to create a new site:
New-WebFtpSite -Name "MyFTPSite" -Port 21 -PhysicalPath C:\inetpub
to list it
ls iis:\sites
and remove it:
Remove-Website -name myftpsite
In the GUI we have an option Add FTP publishing... to an existing web site
How can we do that in PowerShell?
All we need to do is add an FTP binding to the site:
New-WebBinding "MyWebSite"-Port 23 -Protocol ftp -IPAddress *
we can then change settings directly:
Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/site[@name='MyWebSite']/ftpServer/security/ssl" -name "controlChannelPolicy" -value "SslRequireCredentialsOnly"
To find out what settings are available, set them once through the GUI or the Configuration Editor and have a look at the generated PowerShell script.

Starting or stopping it the usual way doesn't work:
stop-website myftpsite
stop-website : The object identifier does not represent a valid object.
what works is the following:
(get-Website -Name "myftpsite").ftpserver.stop()
and of course:
(get-Website -Name "myftpsite").ftpserver.start()
What about listing all FTP sites:
ls IIS:\sites | where Bindings -match "ftp"
doesn't return anything, because bindings is not just a string but a "Microsoft.IIs.PowerShell.Framework.ConfigurationElement" use the following:
Get-ChildItem iis:\sites | ForEach-Object {
  if((Get-WebBinding -Name $_.Name | Where-Object protocol -eq ftp).count -gt 0)
  {
    $_
  }
}
Adding a new IIS Manager user to give her FTP access:
$username = "susan"
$password = "imGluck"
$siteName = "ftp"
$access = "Read,Write"

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Management")  
[Microsoft.Web.Management.Server.ManagementAuthentication]::CreateUser($username, $password) 
[Microsoft.Web.Management.Server.ManagementAuthorization]::Grant($username, $siteName, $FALSE)

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

I wrote a PowerShell script that sets up FTP on IIS, using a custom account for the FTP service and enables IIS Manager Users.
Tags: IIS | IT Pro

Pages in this section

Categories

ASP.Net | Community | Development | IIS | IT Pro | Security | SQL (Server) | Tools | Web | Work on the road | Windows