Create FTP user (Powershell GUI)

Here is a small PowerShell script that I have written today, this is a GUI interface script that will help you create a FTP user with folder and will configure the permissions fore IIS:

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

##########################################################
##							##
##		--	SIGNATURE --			##
##							##
$Company =	'PKM-Technology'			##
$PoweredBye =	'Pouyan Khabazi'			##
$Date =		'30-05-2013'				##
$Version =	'V1'					##
##							##
##							##
##########################################################
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')

$FieldBackGround = [System.Drawing.Color]::'Transparent'
$objForm = New-Object System.Windows.Forms.Form
	$objForm.Text = $Company + ' Create FTP User ' + $Version + ' | (' + $PoweredBye + ')'
	$objForm.Size = New-Object System.Drawing.Size(400,400)
	$objForm.MinimizeBox = $false
	$objForm.MaximizeBox = $false
	$objForm.ControlBox = $false
	$objForm.StartPosition = 'CenterScreen'
	$objForm.FormBorderStyle = 'FixedDialog'
	$objForm.KeyPreview = $True
	$objForm.AllowTransparency = $true
##Styling
$Font = New-Object System.Drawing.Font('Times New Roman','9')

$LabelUserName = New-Object System.Windows.Forms.Label
	$LabelUserName.Location = New-Object System.Drawing.Size(20,150) 
	$LabelUserName.Size = New-Object System.Drawing.Size(80,20)
	$LabelUserName.BackColor = $FieldBackGround
	$LabelUserName.Text = 'User name: '
	$objForm.Controls.Add($LabelUserName)
$TextboxUserName = New-Object System.Windows.Forms.TextBox
	$TextboxUserName.Location = New-Object System.Drawing.Size(100,150) 
	$TextboxUserName.Size = New-Object System.Drawing.Size(200,25)
	$TextboxUserName.Text = ''
	$objForm.Controls.Add($TextboxUserName)

$LabelFullName = New-Object System.Windows.Forms.Label
	$LabelFullName.Location = New-Object System.Drawing.Size(20,180) 
	$LabelFullName.Size = New-Object System.Drawing.Size(80,20)
	$LabelFullName.BackColor = $FieldBackGround
	$LabelFullName.Text = 'Full name: '
	$objForm.Controls.Add($LabelFullName)
$TextboxFullName = New-Object System.Windows.Forms.TextBox
	$TextboxFullName.Location = New-Object System.Drawing.Size(100,180) 
	$TextboxFullName.Size = New-Object System.Drawing.Size(200,25)
	$TextboxFullName.Text = ''
	$objForm.Controls.Add($TextboxFullName)

$LabelPassword = New-Object System.Windows.Forms.Label
	$LabelPassword.Location = New-Object System.Drawing.Size(20,210) 
	$LabelPassword.Size = New-Object System.Drawing.Size(80,20)
	$LabelPassword.BackColor = $FieldBackGround
	$LabelPassword.Text = 'Password: '
	$objForm.Controls.Add($LabelPassword)
$TextboxPassword = New-Object System.Windows.Forms.TextBox
	$TextboxPassword.Location = New-Object System.Drawing.Size(100,210) 
	$TextboxPassword.Size = New-Object System.Drawing.Size(200,25)
	$TextboxPassword.Text = ''
    $TextboxPassword.PasswordChar = '*'
	$objForm.Controls.Add($TextboxPassword)

$ButtonCreate = New-Object Windows.Forms.Button
	$ButtonCreate.Location = New-Object System.Drawing.Size(100,320) 
	$ButtonCreate.Size = New-Object System.Drawing.Size(100,25) 
	$ButtonCreate.text = 'Create user'
	$ButtonCreate.add_click({
        $userName = $TextboxUserName.Text
        $fullName = $TextboxFullName.Text
        $password = $TextboxPassword.Text
        $ftpFolder = "C:inetpubftproot" + $userName

        $Computer = [ADSI]"WinNT://$Env:COMPUTERNAME,Computer"
        #Local user if user exict
        $x = $Computer.psbase.Children | Where-Object {$_.psbase.schemaclassname -eq "user"} | select Name
         foreach ($user in $x) {
          if ($user.Name -eq $userName){
            #Write-Host User $user.name already exists
            $d = [Windows.Forms.MessageBox]::Show('User ' + $userName + ' already exists, please select another username!', [Windows.Forms.MessageBoxButtons]::OK)
            Return
          }
        }
        # Local user account creation:
        $TestUser = $Computer.Create("User", $userName)
        $TestUser.SetPassword($password)
        $TestUser.SetInfo()
        $TestUser.FullName = $fullName
        $TestUser.SetInfo()
        $TestUser.UserFlags = 64 + 65536 # ADS_UF_PASSWD_CANT_CHANGE + ADS_UF_DONT_EXPIRE_PASSWD
        $TestUser.SetInfo()
        # Add user to local group
        $group = [ADSI]"WinNT://./FTP_USERS,group"
        $group.Add("WinNT://$userName,user")
        # Create the FTP folder
        New-Item -ItemType directory -Path $ftpFolder -Force
        # Set permission on the folder
        $inherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit"
        $propagation = [system.security.accesscontrol.PropagationFlags]"None"
        $acl = Get-Acl $ftpFolder
        $accessrule = New-Object system.security.AccessControl.FileSystemAccessRule($userName, "Modify", $inherit, $propagation, "Allow")
        $acl.AddAccessRule($accessrule)
        Set-Acl -AclObject $acl $ftpFolder

        $d = [Windows.Forms.MessageBox]::Show('User '+ $userName +' is created successfully! 
Do you want to close application?', 'Exit', [Windows.Forms.MessageBoxButtons]::YesNo)
		if ($d -eq [Windows.Forms.DialogResult]::Yes){
			$objForm.Close()
		} else {}
    })
    $objForm.controls.add($ButtonCreate)

##CLOSE BUTTON
$CancelButton = New-Object System.Windows.Forms.Button
	$CancelButton.Location = New-Object System.Drawing.Size(220,320)
	$CancelButton.Size = New-Object System.Drawing.Size(75,25)
	$CancelButton.Text = 'Close'
	$CancelButton.Add_Click({
		$d = [Windows.Forms.MessageBox]::Show('Are you shore you want to close the application?', 'Exit', [Windows.Forms.MessageBoxButtons]::YesNo)
		if ($d -eq [Windows.Forms.DialogResult]::Yes){
			$objForm.Close()
		} else {}	
	})
	$objForm.Controls.Add($CancelButton)

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Well you can use the script to start the GUI both you can also convert this to a EXE file, to convert the script to a EXE you can  download PS2EXE

Now you can run the coede below to confert the PS1 file to a EXE file

  • Start powershell in version 2 (if you are running version 3)
    powershell.exe -version 2
  • Browse to the folder where you unzipt the PS2EXE and run the code
    cd c:PS2EXE
    ps2exe.ps1 -inputFile C:ScriptsFTPuser.PS1 -outputFile C:ScriptsFTPuser.exe -noConsole

~pouyan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.