Query FTP site information in IIS 7.X through Powershell

 

Recently I worked with one of the customer, who wanted assistance in displaying the statistical view of the FTP sites in IIS 7.X through Powershell. The statistical out should include the below information:

1. The IP address assigned to the site.

2. PORT Number

3. Website name

4. Website Identifier

5. Virtual directories under the site.

 

Pre-Requisites for running the below script:

1. We need IIS 7.X version installed on the WebServer along with the FTP feature

2. We need the Powershell to run the script.

3. Make sure that the Server Administrator has access to store and read the certificates from the Certificate Store location mentioned in the script.

How to run the script:

            1. You can modify the below script as per your environment and place it a file with extension .ps1 and run it through elevated powershell window.

            2. Alternatively, You can also run the script through PowerShell ISE.

Using the Code:

The following code snippet is the sample of importing the IIS modules

We have to import the WebAdministration module for getting access to the various IIS APIs using the below sample:

                Import-Module WebAdministration

We then need to get all the websites in the IIS server:

      $Websites = Get-ChildItem IIS:\Sites

Loop through each of the FTP sites in the collection and Retrieve the virtual directory details for the FTP site along with FTP Site Name, Site-ID, Bindings, IP, Port information

 

# Ensure to import the WebAdministration module

Import-Module WebAdministration

# Here we are getting all the websites in the IIS server

$Websites = Get-ChildItem IIS:\Sites

# Loop through each of the sites in the collection

foreach ($Site in $Websites)

{

$Binding = $Site.bindings # Extract the binding information

[string]$BindingInfo = $Binding.Collection

[string[]]$Bindings = $BindingInfo.Split(" ")

#new-variable -name 'iterate' -visibility public -value 0

$iterate = 0

Do

{

if ($Bindings -eq "ftp") # Check condition if binding is only for FTP, then perform the required logic

{

Write ("Site :- " + $Site.name + " <" + $Site.id +">")

$sitename= $Site.name

Write ("Protocol:- " + $Bindings[($iterate)])

[string[]]$Bindings2 = $Bindings[($iterate+1)].Split(":")

# Retrieve the virtual directory details for the FTP site along with FTP Site Name, Site-ID, Bindings, IP, Port information

$vdcoll = Get-WebVirtualDirectory -site $sitename | select path

Write ("IP :- " + $Bindings2[0])

Write ("Port :- " + $Bindings2[1])

Write ("Virtual Directory Path:")

Write("-----------")

foreach($VD in $vdcoll)

{

Write($VD.path)

}

Write("**************************************************************************")

}

$iterate=$iterate+2

} while ($iterate -lt ($bindings.count))

}

 

More Information

The following article will provide a few cmdlets which are available for the IIS:

 https://technet.microsoft.com/en-us/library/ee790599.aspx

 

DISCLAIMER

#The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation,

#any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors,

#or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information,

#or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages