PoshCode Logo PowerShell Code Repository

IIS FTP Site Creation (modification of post by view diff)
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/685"></script>download | new post

Automatically creates a Local User on server, Local directory on server, and Virtual directory in IIS based on User Inputs. Also sets user flags to read/write on directory and sets password options for user

  1. "       -------------------------------------------"
  2. "       ##     FTP VIRTUAL DIRECTORY CREATION SCRIPT     ##"
  3. ""
  4. ""
  5. "       ## This script will create a new username, password, local directory, and virtual directory for a client "
  6. ""
  7. "       ## Please enter the following information "
  8. ""
  9. "       -------------------------------------------"
  10.  
  11.  
  12. ### PowerShell Script
  13. ### Create local User Acount
  14.  
  15. $AccountName = Read-Host "Please enter user account name (i.e. krisp)"
  16. $FullName = Read-Host "Please enter the full name (i.e. Kris)"
  17. $Description = Read-Host "Please enter the description (i.e. Krisp FTP Login)"
  18. $Password = Read-Host "Please enter a password"
  19. $Computer = "FTPSERVER"
  20.  
  21. "Creating user on $Computer"
  22.  
  23. # Access to Container using the COM library
  24. $Container = [ADSI] "WinNT://$Computer"
  25.  
  26. # Create User
  27. $objUser = $Container.Create("user", $Accountname)
  28. $objUser.Put("Fullname", $FullName)
  29. $objUser.Put("Description", $Description)
  30.  
  31. # Set Password
  32. $objUser.SetPassword($Password)
  33.  
  34. # Save Changes
  35. $objUser.SetInfo()
  36.  
  37. # Add User Flags
  38. # The numbers are bitwise - 65536 is Password Never Expires ; 64 is User Cannot Change Password
  39.  
  40. $objUser.userflags = 65536 -bor 64
  41. $objUser.SetInfo()
  42.  
  43. "User $AccountName created!"
  44. " ------------------------"
  45.  
  46.  
  47.  
  48. #       ---Create FTP local directory---
  49.  
  50. "Creating directory D:\FTP\$AccountName"
  51.  
  52. New-Item D:\FTP\$AccountName -type directory 
  53. Start-Sleep -Seconds 5
  54. "Directory $AccountName created!"
  55. " ------------------------"
  56.  
  57.  
  58. #       ---Set Permissions on Folder
  59.  
  60. "Setting Permissions on D:\FTP\$AccountName"
  61.  
  62. $colRights = [System.Security.AccessControl.FileSystemRights]"Modify"
  63. $Inherit = [System.Security.AccessControl.InheritanceFlags]"ContainerInherit, ObjectInherit"
  64. $Propagate = [System.Security.AccessControl.PropagationFlags]::None
  65. $objType =[System.Security.AccessControl.AccessControlType]::Allow
  66. $User = New-Object System.Security.Principal.NTAccount("$Computer\$AccountName")
  67. $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($User, $colRights , $Inherit, $Propagate, $objType)
  68.  
  69. $objACL = Get-Acl "D:\FTP\$AccountName"
  70.  
  71. $objACL.AddAccessRule($objACE)
  72.  
  73. Set-Acl "D:\FTP\$AccountName" $objACL
  74.  
  75. Start-Sleep -Seconds 5
  76.  
  77. "Permissions Successfully Applied!"
  78. " ------------------------"
  79.  
  80.  
  81.  
  82.  
  83. #       ---Add User to FTP Users Local Group
  84.  
  85. "Adding User to FTP Users Group"
  86.  
  87.  
  88. $group = [ADSI]"WinNT://$computer/FTP Users"
  89. $group.add("WinNT://$computer/$AccountName")
  90.  
  91. "User Added!"
  92. "-------------------------"
  93.  
  94.  
  95.  
  96.  
  97.  
  98. #       ---Create Temporary File with Username
  99.  
  100. $accountname | Out-File name.txt     # <----IIS part won't take direct input, wants to read from file.  Need to Research
  101.  
  102.  
  103.  
  104.  
  105. #       ---Create Virtual Directory in IIS---
  106.  
  107. "Creating virtual directory in IIS"
  108. Start-Sleep -Seconds 5
  109. foreach ($a in get-content name.txt)
  110. {
  111. $server = $env:computername
  112. $service = New-Object System.DirectoryServices.DirectoryEntry("IIS://$server/MSFTPSVC")
  113. $site = $service.psbase.children |Where-Object { $_.ServerComment -eq 'IntFTP' }
  114. $site = New-Object System.DirectoryServices.DirectoryEntry($site.psbase.path+"/Root") for IIS 7.
  115. $virtualdir = $site.psbase.children.Add("$a","IIsFtpVirtualDir")
  116. $virtualdir.psbase.CommitChanges()
  117. $virtualdir.put("Path","D:\FTP\$a")
  118. $virtualdir.put("AccessRead",$true)
  119. $virtualdir.put("AccessWrite",$true)
  120. $virtualdir.psbase.CommitChanges()
  121. $service.psbase.refreshCache() # OPTIONAL
  122.  
  123. }
  124.  
  125. "FTP site $AccountName created!"
  126. " ------------------------"

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me