none
Create a folder with permissions with powershell RRS feed

  • Question

  • Hi all,

    I am trying to create a ps script with UI that does the following:

    -Create a folder in  a particular directory

    -Assigns read or write permissions for a list of users to that folder.(the user list is gotten from an AD directory)

    -The user is allowed to select these users through a checkbox list in the UI

    -That folder is created with those permissions for those users only

    So far I have been able to create the UI for this and get the list of AD users to be displayed.

    The part of how to create the folder and set permissions for the users that I ticked is where I'm getting stuck. Here is the script so far.Any help would be greatly appreciated!

    Set-ExecutionPolicy Unrestricted -Scope Process #unrestricted needed to work on file server.

    $file_output = "C:\test"  #file path where to output the result

    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    [System.Windows.Forms.Application]::EnableVisualStyles() #without this, UI uses classic theme (no eye candy)

    $objForm = New-Object System.Windows.Forms.Form 
    $objForm.Text = "Create folder"
    $objForm.Size = New-Object System.Drawing.Size(700,600) 
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
        {$x=$objTextBox.Text;$objForm.Close()}})
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
        {$objForm.Close()}})

    $OKButton = New-Object System.Windows.Forms.Button
    $OKButton.Location = New-Object System.Drawing.Size(235,480)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$x=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)


    $CancelButton = New-Object System.Windows.Forms.Button
    $CancelButton.Location = New-Object System.Drawing.Size(350,480)
    $CancelButton.Size = New-Object System.Drawing.Size(75,23)
    $CancelButton.Text = "Cancel"
    $CancelButton.Add_Click({$objForm.Close()})
    $objForm.Controls.Add($CancelButton)


    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(275,20) 
    $objLabel.Size = New-Object System.Drawing.Size(280,20) 
    $objLabel.Text = "Enter folder name"
    $objForm.Controls.Add($objLabel) 

    $readlabel = New-Object System.Windows.Forms.label
    $readlabel.Location = New-Object System.Drawing.Size(20,80)
    $readlabel.Size = New-Object System.Drawing.Size(300,23)
    $readlabel.Text = "Select users for Read access"
    $objform.Controls.Add($readlabel)

    $writelabel = New-Object System.Windows.Forms.label
    $writelabel.Location = New-Object System.Drawing.Size(400,80)
    $writelabel.Size = New-Object System.Drawing.Size(220,23)
    $writelabel.Text = "Select users for Write access"
    $objForm.Controls.Add($writelabel)

    $objTextBox = New-Object System.Windows.Forms.TextBox 
    $objTextBox.Location = New-Object System.Drawing.Size(190,40) 
    $objTextBox.Size = New-Object System.Drawing.Size(260,20) 
    $objForm.Controls.Add($objTextBox) 

    $users = Get-ADUser -SearchBase "OU=Users,OU=Company,DC=phoenix,DC=local" -Filter {name -like "*"} | select -ExpandProperty Name | sort-object 

    $CheckedListBoxread = New-Object System.Windows.Forms.CheckedListBox
    $CheckedListBoxread.Location = New-Object System.Drawing.Size(20,110)
    $CheckedListBoxread.size = New-Object System.Drawing.Size(300,350) 
    $CheckedListBoxread.CheckOnClick = $true #so we only have to click once to check a box
    $CheckedListBoxread.Items.AddRange($users)
    $CheckedListBoxread.ClearSelected()
    $CheckedListBoxread.g
    $objForm.Controls.Add($CheckedListBoxread)

    $CheckedListBoxwrite = New-Object System.Windows.Forms.CheckedListBox
    $CheckedListBoxwrite.Location = New-Object System.Drawing.Size(350,110)
    $CheckedListBoxwrite.size = New-Object System.Drawing.Size(300,350) 
    $CheckedListBoxwrite.CheckOnClick = $true #so we only have to click once to check a box
    $CheckedListBoxwrite.Items.AddRange($users)
    $CheckedListBoxwrite.ClearSelected()
    $CheckedListBoxwrite.g
    $objForm.Controls.Add($CheckedListBoxwrite)
    #put form in front of other windows
    $objForm.TopMost = $True


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

    $objForm.Close()

    Thursday, April 6, 2017 12:51 PM

Answers

All replies

  • Start by learning how to create a folder and set permissions at the command line.  Don't complicate things with a form which is a technology you do not know.  Start with the basics.

    help New-Item -full
    help Set-Acl -full

    Follow and try the examples until you understand how to use the CmdLets.


    \_(ツ)_/

    Thursday, April 6, 2017 6:07 PM
  • hi jrv thanks for the tip.unfortunately in this case I can't do that.This will be a script used by non-technical folks so needs a UI and I need to do all the permissions stuff in the backend. Thanks!
    Thursday, April 6, 2017 6:11 PM
  • jrv's direction on the help command should show you how to create folders and set permissions. Do you still need help beyond that?

    Also, functions would be nice.

    Thursday, April 6, 2017 6:16 PM
  • You miss the point.  TO create the script you need to learn how to do this.  Learning at a prompt is interactive so it will help you learn.  Once you understand how to do a task you can then learn Forms and how to add it to the form.

    Start by learning how to do the task and forget about forms until you know the task.  Next learn forms and how to use code from a form.

    You are doing everything backwards.  Learn first - build later.


    \_(ツ)_/

    Thursday, April 6, 2017 6:17 PM
  • That helps to set the permission and folder, but I still need to know how to implement this in a form where a user selects the users from a checklist. 
    Thursday, April 6, 2017 6:37 PM
  • I will do that and move on from there. thanks
    Thursday, April 6, 2017 6:38 PM
  • That is why I posted the link.  It will give you articles on how to create a form and how to use controls and events.


    \_(ツ)_/

    Thursday, April 6, 2017 6:57 PM
  • Here is an example of how to use checked lists: https://1drv.ms/u/s!AjiiPtIUqzK_hYwaMkcm68WN2p0p_A

    \_(ツ)_/

    • Marked as answer by xejd28 Thursday, April 6, 2017 7:02 PM
    Thursday, April 6, 2017 6:59 PM