tri_94 Posted June 14, 2019 Posted June 14, 2019 Hi there Just working on a project. To create a script to change permissions on folders. I've created the script and can get it to do what I require. However I would now like to make a GUI for this. I've created a basic one using Poshgui.com which I will neaten up once I know I can get it to do what I require. my plan Firstly I would like to have a text box that I can type a file path Then did have checkbox which I changed to radio buttons for selecting the required permissions. (hence why the code states $Checkbox) so you would type file path, then choose permissions and at the bottom is a button to process the action. I can get the radio buttons to work If I repeat the code changing the variable for permissions, however I would prefer it to update a variable which I had tried with $combo and then apply that way? Also I would like to add a way to change from adding permissions to removing the permissions (again I've got the variable in the code below just # out) Can anyone please help me with this? Thanks Nick <# This form was created using POSHGUI.com a free online gui designer for PowerShell .NAME Untitled #> Add-Type -AssemblyName System.Windows.Forms [system.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = '400,400' $Form.text = "Form" $Form.TopMost = $false $Label1 = New-Object system.Windows.Forms.Label $Label1.text = "label" $Label1.AutoSize = $true $Label1.width = 25 $Label1.height = 10 $Label1.location = New-Object System.Drawing.Point(5,7) $Label1.Font = 'Microsoft Sans Serif,10' $TextBox1 = New-Object system.Windows.Forms.TextBox $TextBox1.multiline = $false $TextBox1.width = 382 $TextBox1.height = 20 $TextBox1.location = New-Object System.Drawing.Point(6,35) $TextBox1.Font = 'Microsoft Sans Serif,10' $Label2 = New-Object system.Windows.Forms.Label $Label2.text = "label" $Label2.AutoSize = $true $Label2.width = 25 $Label2.height = 10 $Label2.location = New-Object System.Drawing.Point(9,70) $Label2.Font = 'Microsoft Sans Serif,10' $CheckBox1 = New-Object system.Windows.Forms.RadioButton $CheckBox1.text = "Read" $CheckBox1.AutoSize = $false $CheckBox1.width = 95 $CheckBox1.height = 20 $CheckBox1.location = New-Object System.Drawing.Point(9,105) $CheckBox1.Font = 'Microsoft Sans Serif,10' $CheckBox2 = New-Object system.Windows.Forms.Radiobutton $CheckBox2.text = "ReadAndExecute" $CheckBox2.AutoSize = $false $CheckBox2.width = 150 $CheckBox2.height = 20 $CheckBox2.location = New-Object System.Drawing.Point(9,133) $CheckBox2.Font = 'Microsoft Sans Serif,10' $CheckBox3 = New-Object system.Windows.Forms.Radiobutton $CheckBox3.text = "Write" $CheckBox3.AutoSize = $false $CheckBox3.width = 150 $CheckBox3.height = 20 $CheckBox3.location = New-Object System.Drawing.Point(9,162) $CheckBox3.Font = 'Microsoft Sans Serif,10' $CheckBox4 = New-Object system.Windows.Forms.Radiobutton $CheckBox4.text = "Modify" $CheckBox4.AutoSize = $false $CheckBox4.width = 150 $CheckBox4.height = 20 $CheckBox4.location = New-Object System.Drawing.Point(9,192) $CheckBox4.Font = 'Microsoft Sans Serif,10' $CheckBox5 = New-Object system.Windows.Forms.Radiobutton $CheckBox5.text = "FullControl" $CheckBox5.AutoSize = $false $CheckBox5.width = 150 $CheckBox5.height = 20 $CheckBox5.location = New-Object System.Drawing.Point(9,222) $CheckBox5.Font = 'Microsoft Sans Serif,10' #$CheckBox6 = New-Object system.Windows.Forms.CheckBox #$CheckBox6.text = "checkBox" #$CheckBox6.AutoSize = $false #$CheckBox6.width = 150 #$CheckBox6.height = 20 #$CheckBox6.location = New-Object System.Drawing.Point(9,253) #$CheckBox6.Font = 'Microsoft Sans Serif,10' $Button1 = New-Object system.Windows.Forms.Button $Button1.text = "Create Log File" $Button1.width = 375 $Button1.height = 30 $Button1.location = New-Object System.Drawing.Point(8,285) $Button1.Font = 'Microsoft Sans Serif,10' $Form.controls.AddRange(@($Label1,$TextBox1,$Label2,$CheckBox1,$CheckBox2,$CheckBox3,$CheckBox4,$CheckBox5,$Button1)) $CheckBox1.Add_Click({ $combo = "Read" }) $CheckBox2.Add_Click({ $combo = "ReadAndExecute" }) $CheckBox3.Add_Click({ $combo = "Write" }) $CheckBox4.Add_Click({ $combo = "Modify" }) $CheckBox5.Add_Click({ $combo = "FullControl" }) Write-Host $combo $DirPath = "d:\test" $LogPath = "d:\test\FolderPermissions.csv" $Button1.Add_Click({ $DirPath = "d:\test" $LogPath = "d:\test\FolderPermissions.csv" $HomeFolders = Get-ChildItem d:\test\ -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($Username,$combo,'ContainerInherit,ObjectInherit', 'None', 'Allow') $Acl.SetAccessRule($Ar) #$acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } $FolderPath = dir -Directory -Path $DirPath -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath #Write your logic code here }) [void]$Form.ShowDialog()
tri_94 Posted June 14, 2019 Author Posted June 14, 2019 (edited) Hi again I've changed the layout. In a fashion this is how i'd like it to work. I'm not sure how to get button 1 and 2 working as needed. Also it doesn't seem to do anything when I try it. Anyone that can help would be really grateful. Thanks Nick Code below. <# This form was created using POSHGUI.com a free online gui designer for PowerShell .NAME Untitled #> Add-Type -AssemblyName System.Windows.Forms [system.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = '400,400' $Form.text = "Form" $Form.TopMost = $false $Label1 = New-Object system.Windows.Forms.Label $Label1.text = "Please enter folder path and press Enter" $Label1.AutoSize = $true $Label1.width = 250 $Label1.height = 10 $Label1.location = New-Object System.Drawing.Point(11,20) $Label1.Font = 'Microsoft Sans Serif,10' $TextBox1 = New-Object system.Windows.Forms.TextBox $TextBox1.multiline = $false $TextBox1.width = 376 $TextBox1.height = 20 $TextBox1.location = New-Object System.Drawing.Point(11,41) $TextBox1.Font = 'Microsoft Sans Serif,10' $Label2 = New-Object system.Windows.Forms.Label $Label2.text = "Please select Add or Remove premissions" $Label2.AutoSize = $true $Label2.width = 250 $Label2.height = 10 $Label2.location = New-Object System.Drawing.Point(11,76) $Label2.Font = 'Microsoft Sans Serif,10' $Button1 = New-Object system.Windows.Forms.Button $Button1.text = "Add Permission" $Button1.width = 150 $Button1.height = 40 $Button1.location = New-Object System.Drawing.Point(11,99) $Button1.Font = 'Microsoft Sans Serif,10' $Button2 = New-Object system.Windows.Forms.Button $Button2.text = "Remove Permission" $Button2.width = 150 $Button2.height = 40 $Button2.location = New-Object System.Drawing.Point(213,99) $Button2.Font = 'Microsoft Sans Serif,10' $Label3 = New-Object system.Windows.Forms.Label $Label3.text = "Please choose premissions to apply" $Label3.AutoSize = $true $Label3.width = 25 $Label3.height = 10 $Label3.location = New-Object System.Drawing.Point(11,150) $Label3.Font = 'Microsoft Sans Serif,10' $RadioButton1 = New-Object system.Windows.Forms.RadioButton $RadioButton1.text = "Read" $RadioButton1.AutoSize = $true $RadioButton1.width = 104 $RadioButton1.height = 20 $RadioButton1.location = New-Object System.Drawing.Point(11,180) $RadioButton1.Font = 'Microsoft Sans Serif,10' $RadioButton2 = New-Object system.Windows.Forms.RadioButton $RadioButton2.text = "ReadAndExecute" $RadioButton2.AutoSize = $true $RadioButton2.width = 104 $RadioButton2.height = 20 $RadioButton2.location = New-Object System.Drawing.Point(11,200) $RadioButton2.Font = 'Microsoft Sans Serif,10' $RadioButton3 = New-Object system.Windows.Forms.RadioButton $RadioButton3.text = "Write" $RadioButton3.AutoSize = $true $RadioButton3.width = 104 $RadioButton3.height = 20 $RadioButton3.location = New-Object System.Drawing.Point(11,220) $RadioButton3.Font = 'Microsoft Sans Serif,10' $RadioButton4 = New-Object system.Windows.Forms.RadioButton $RadioButton4.text = "Modify" $RadioButton4.AutoSize = $true $RadioButton4.width = 104 $RadioButton4.height = 20 $RadioButton4.location = New-Object System.Drawing.Point(11,240) $RadioButton4.Font = 'Microsoft Sans Serif,10' $RadioButton5 = New-Object system.Windows.Forms.RadioButton $RadioButton5.text = "FullControl" $RadioButton5.AutoSize = $true $RadioButton5.width = 104 $RadioButton5.height = 20 $RadioButton5.location = New-Object System.Drawing.Point(11,260) $RadioButton5.Font = 'Microsoft Sans Serif,10' $Button3 = New-Object system.Windows.Forms.Button $Button3.text = "Submit" $Button3.width = 376 $Button3.height = 30 $Button3.location = New-Object System.Drawing.Point(11,300) $Button3.Font = 'Microsoft Sans Serif,10' $Form.controls.AddRange(@($Label1,$TextBox1,$Label2,$Button1,$Button2,$Label3,$RadioButton1,$RadioButton2,$RadioButton3,$RadioButton4,$RadioButton5,$Button3)) #Write your logic code here [void]$Form.ShowDialog() $DirPath = $Textbox1 $LogPath = $Textbox1"\FolderPermissions.csv" $Button1.Add_Click({ $Acl.SetAccessRule($Ar) }) $Button2.Add_Click({ $acl.RemoveAccessRule($Ar) }) $RadioButton1.Add_Click({ $Action = "Read"}) $RadioButton2.Add_Click({ $Action = "ReadAndExecute"}) $RadioButton3.Add_Click({ $Action = "Write"}) $RadioButton4.Add_Click({ $Action = "Modify"}) $RadioButton5.Add_Click({ $Action = "FullControl"}) $Button3.Add_Click({ $HomeFolders = Get-ChildItem $Testbox1 -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,$Action,'ContainerInherit,ObjectInherit' , 'None', 'Allow') #$Acl.SetAccessRule($Ar) #$acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } $FolderPath = dir -Directory -Path $DirPath -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath }) Edited June 14, 2019 by tri_94
Arthur Posted June 14, 2019 Posted June 14, 2019 You might find the following PowerShell module useful since it enables you to create nicer looking WPF GUIs (unlike PoshGUI). www.edugeek.net/forums/downloads/207486-anybox-powershell-module-customizable-wpf-guis-one-line.html
tri_94 Posted June 14, 2019 Author Posted June 14, 2019 Thank you for that I'll have a look at that next time. But can anyone help with this project?
Sephiroth Posted June 14, 2019 Posted June 14, 2019 (edited) A couple of things to look at: [void]$Form.ShowDialog() should be the last line to run, otherwise it draws the form before reading the logic. The radio buttons can be referenced as $RadioButton1.IsChecked. A default can be set by doing $RadioButton1.Checked = $True Your radio buttons should be inside a GroupBox. Add the RadioButton controls to the GroupBox and add the GroupBox control to the Form. This means you can have multiple sets of radio buttons. Personally I prefer to put my action code (button click events, et cetera) in functions an call the function on form action. To read the text from $Textbox1, you need to reference the Text value... $Textbox1.Text Personally I don't like the way that PoshGUI.com arranges the code. I prefer to write it myself and order to my own liking. That's just me though Hopefully that helps. Edited June 14, 2019 by Sephiroth
tri_94 Posted June 15, 2019 Author Posted June 15, 2019 Thanks again for your help i've used some of the ideas. I've now managed to nearly get it to work as I need. Please can anyone tell me how i can use buttons 1 and 2 to determine which line of code needs to run either $Acl.SetAccessRule($Ar) or $acl.RemoveAccessRule($Ar) Also how I can use the entered text to map the path for the log file. I've tried things like $Textbox1.text\permissions.csv but this doesn't work. I'm sure its dead straightforward for any powershell guru's. One last thing can anyone point me to a easy method to use $label4 to display the entered file path somewhere on the screen. Thanks for anyone that takes the time to read and help. Cheers Nick <# This form was created using POSHGUI.com a free online gui designer for PowerShell .NAME Untitled #> Add-Type -AssemblyName System.Windows.Forms [system.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = '400,400' $Form.text = "Form" $Form.TopMost = $false $Label1 = New-Object system.Windows.Forms.Label $Label1.text = "Please enter folder path and press Enter" $Label1.AutoSize = $true $Label1.width = 350 $Label1.height = 10 $Label1.location = New-Object System.Drawing.Point(11,20) $Label1.Font = 'Microsoft Sans Serif,10' $TextBox1 = New-Object system.Windows.Forms.TextBox $TextBox1.multiline = $false $TextBox1.width = 376 $TextBox1.height = 20 $TextBox1.location = New-Object System.Drawing.Point(11,41) $TextBox1.Font = 'Microsoft Sans Serif,10' $Label2 = New-Object system.Windows.Forms.Label $Label2.text = "Please select Add or Remove premissions" $Label2.AutoSize = $true $Label2.width = 250 $Label2.height = 10 $Label2.location = New-Object System.Drawing.Point(11,76) $Label2.Font = 'Microsoft Sans Serif,10' $Button1 = New-Object system.Windows.Forms.Button $Button1.text = "Add Permission" $Button1.width = 150 $Button1.height = 40 $Button1.location = New-Object System.Drawing.Point(11,99) $Button1.Font = 'Microsoft Sans Serif,10' $Button2 = New-Object system.Windows.Forms.Button $Button2.text = "Remove Permission" $Button2.width = 150 $Button2.height = 40 $Button2.location = New-Object System.Drawing.Point(213,99) $Button2.Font = 'Microsoft Sans Serif,10' $Label3 = New-Object system.Windows.Forms.Label $Label3.text = "Please choose premissions to apply" $Label3.AutoSize = $true $Label3.width = 25 $Label3.height = 10 $Label3.location = New-Object System.Drawing.Point(11,150) $Label3.Font = 'Microsoft Sans Serif,10' #$Label4 = New-Object system.Windows.Forms.Label #$Label4.text = "$TextBox1.Text" #$Label4.AutoSize = $true #$Label4.width = 25 #$Label4.height = 10 #$Label4.location = New-Object System.Drawing.Point(30,41) #$Label4.Font = 'Microsoft Sans Serif,10' $RadioButton1 = New-Object system.Windows.Forms.RadioButton $RadioButton1.text = "Read" $RadioButton1.AutoSize = $true $RadioButton1.width = 104 $RadioButton1.height = 20 $RadioButton1.location = New-Object System.Drawing.Point(11,180) $RadioButton1.Font = 'Microsoft Sans Serif,10' $RadioButton2 = New-Object system.Windows.Forms.RadioButton $RadioButton2.text = "ReadAndExecute" $RadioButton2.AutoSize = $true $RadioButton2.width = 104 $RadioButton2.height = 20 $RadioButton2.location = New-Object System.Drawing.Point(11,200) $RadioButton2.Font = 'Microsoft Sans Serif,10' $RadioButton3 = New-Object system.Windows.Forms.RadioButton $RadioButton3.text = "Write" $RadioButton3.AutoSize = $true $RadioButton3.width = 104 $RadioButton3.height = 20 $RadioButton3.location = New-Object System.Drawing.Point(11,220) $RadioButton3.Font = 'Microsoft Sans Serif,10' $RadioButton4 = New-Object system.Windows.Forms.RadioButton $RadioButton4.text = "Modify" $RadioButton4.AutoSize = $true $RadioButton4.width = 104 $RadioButton4.height = 20 $RadioButton4.location = New-Object System.Drawing.Point(11,240) $RadioButton4.Font = 'Microsoft Sans Serif,10' $RadioButton5 = New-Object system.Windows.Forms.RadioButton $RadioButton5.text = "FullControl" $RadioButton5.AutoSize = $true $RadioButton5.width = 104 $RadioButton5.height = 20 $RadioButton5.location = New-Object System.Drawing.Point(11,260) $RadioButton5.Font = 'Microsoft Sans Serif,10' $Button3 = New-Object system.Windows.Forms.Button $Button3.text = "Submit" $Button3.width = 376 $Button3.height = 30 $Button3.location = New-Object System.Drawing.Point(11,300) $Button3.Font = 'Microsoft Sans Serif,10' $Form.controls.AddRange(@($Label1,$TextBox1,$Label2,$Button1,$Button2,$Label3,$RadioButton1,$RadioButton2,$RadioButton3,$RadioButton4,$RadioButton5,$Button3)) #Write your logic code here $LogPath = "d:\test\FolderPermissions.csv" $Button1.Add_Click({Write-Host Button1 }) $Button2.Add_Click({Write-Host Button2 }) $RadioButton1.Add_Click({ }) $RadioButton2.Add_Click({ }) $RadioButton3.Add_Click({ }) $RadioButton4.Add_Click({ }) $RadioButton5.Add_Click({ }) function Read (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'Read','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) #$acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } $FolderPath = dir -Directory -Path $Textbox1.Text -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath } function Read+Excute (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'ReadAndExecute','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) #$acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } $FolderPath = dir -Directory -Path $Textbox1.Text -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath } function Write+Only (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'Write','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) #$acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } $FolderPath = dir -Directory -Path $Textbox1.Text -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath } function Modify (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'Modify','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) #$acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } $FolderPath = dir -Directory -Path $Textbox1.Text -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath } function FullControl (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'FullControl','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) #$acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } $FolderPath = dir -Directory -Path $Textbox1.Text -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath } $Button3.Add_Click({ if ($RadioButton1.Checked -eq $true) { Read } if ($RadioButton2.Checked -eq $true) { Read+Excute } if ($RadioButton3.Checked -eq $true) { Write+Only } if ($RadioButton4.Checked -eq $true) { Modify } if ($RadioButton5.Checked -eq $true) { FullControl } }) [void]$Form.ShowDialog()
Sephiroth Posted June 17, 2019 Posted June 17, 2019 To use the buttons, you should be able to use this code: Function Get_RB { If ($RadioButton1.Checked) { $RB = "Read" } ElseIf ($RadioButton2.Checked) { $RB = "ReadAndExecute" } ElseIf ($RadioButton3.Checked) { $RB = "Write" } ElseIf ($RadioButton4.Checked) { $RB = "Modify" } ElseIf ($RadioButton5.Checked) { $RB = "FullControl" } Return $RB } Function Button1_OnClick { $RB = Get_RB $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,$RB,'ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } Function Button2_OnClick { $RB = Get_RB $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,$RB,'ContainerInherit,ObjectInherit' , 'None', 'Allow') $acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } You will need to set the Add_Click events for the buttons to these functions. You shouldn't then need button 3, as clicking on Button1 or Button2 will do everything. Let me know how it goes! 1
tri_94 Posted June 17, 2019 Author Posted June 17, 2019 Thanks again for your help. I had used what you said before and recreated it. Using groupbox etc. Finished code is below. <# This form was created using POSHGUI.com a free online gui designer for PowerShell .NAME Permissions #> #Created by tri_94 Add-Type -AssemblyName System.Windows.Forms [system.Windows.Forms.Application]::EnableVisualStyles() $FormName = New-Object system.Windows.Forms.Form $FormName.ClientSize = '395,540' $FormName.text = "Form" $FormName.TopMost = $false $FormName.icon = "Icons8-Windows-8-Security-Password-2.ico" #$FormNameImage = [system.drawing.image]::FromFile("img.jpg") #$FormName.BackgroundImage = $FormNameImage $Label1 = New-Object system.Windows.Forms.Label $Label1.text = "Please enter folder path" $Label1.AutoSize = $true $Label1.width = 25 $Label1.height = 10 $Label1.location = New-Object System.Drawing.Point(10,20) $Label1.Font = 'Tahoma,10,12' $TextBox1 = New-Object system.Windows.Forms.TextBox $TextBox1.multiline = $false $TextBox1.width = 374 $TextBox1.height = 20 $TextBox1.location = New-Object System.Drawing.Point(10,40) $TextBox1.Font = 'Tahoma,10,12' $Button1 = New-Object system.Windows.Forms.Button $Button1.text = "Click to view Files `&& Directories" $Button1.width = 374 $Button1.height = 30 $Button1.location = New-Object System.Drawing.Point(10,65) $Button1.Font = 'Tahoma,10,12' $Label2 = New-Object system.Windows.Forms.Label $Label2.text = "Below is a list of directories" $Label2.AutoSize = $true $Label2.width = 25 $Label2.height = 10 $Label2.location = New-Object System.Drawing.Point(11,100) $Label2.Font = 'Tahoma,10,12' $OutputBox = New-Object system.Windows.Forms.TextBox $OutputBox.width = 374 $OutputBox.height = 172 $OutputBox.location = New-Object System.Drawing.Point(10,120) $OutputBox.MultiLine = $True $OutputBox.ScrollBars = "Vertical" $Button2 = New-Object system.Windows.Forms.Button $Button2.text = "Check current permissions" $Button2.width = 374 $Button2.height = 30 $Button2.location = New-Object System.Drawing.Point(10,295) $Button2.Font = 'Tahoma,10,12' $Label3 = New-Object system.Windows.Forms.Label $Label3.text = "Click below if you wish to remove permissions" $Label3.AutoSize = $true $Label3.width = 25 $Label3.height = 10 $Label3.location = New-Object System.Drawing.Point(10,335) $Label3.Font = 'Tahoma,10,12' $Button3 = New-Object system.Windows.Forms.Button $Button3.text = "Remove Permissions" $Button3.width = 374 $Button3.height = 30 $Button3.location = New-Object System.Drawing.Point(10,355) $Button3.Font = 'Tahoma,10,12' $Label4 = New-Object system.Windows.Forms.Label $Label4.text = "Please select premissions to ADD" $Label4.AutoSize = $true $Label4.width = 25 $Label4.height = 10 $Label4.location = New-Object System.Drawing.Point(10,390) $Label4.Font = 'Tahoma,10,12' $Groupbox1 = New-Object system.Windows.Forms.Groupbox $Groupbox1.height = 62 $Groupbox1.width = 364 $Groupbox1.text = "" $Groupbox1.location = New-Object System.Drawing.Point(10,415) $RadioButton1 = New-Object system.Windows.Forms.RadioButton $RadioButton1.text = "Read" $RadioButton1.AutoSize = $true $RadioButton1.width = 104 $RadioButton1.height = 60 $RadioButton1.location = New-Object System.Drawing.Point(10,8) $RadioButton1.Font = 'Tahoma,10,12' $RadioButton2 = New-Object system.Windows.Forms.RadioButton $RadioButton2.text = "ReadAndExecute" $RadioButton2.AutoSize = $true $RadioButton2.width = 104 $RadioButton2.height = 20 $RadioButton2.location = New-Object System.Drawing.Point(120,8) $RadioButton2.Font = 'Tahoma,10,12' $RadioButton3 = New-Object system.Windows.Forms.RadioButton $RadioButton3.text = "Write" $RadioButton3.AutoSize = $true $RadioButton3.width = 104 $RadioButton3.height = 20 $RadioButton3.location = New-Object System.Drawing.Point(264,8) $RadioButton3.Font = 'Tahoma,10,12' $RadioButton4 = New-Object system.Windows.Forms.RadioButton $RadioButton4.text = "Modify" $RadioButton4.AutoSize = $true $RadioButton4.width = 104 $RadioButton4.height = 20 $RadioButton4.location = New-Object System.Drawing.Point(10,36) $RadioButton4.Font = 'Tahoma,10,12' $RadioButton5 = New-Object system.Windows.Forms.RadioButton $RadioButton5.text = "FullControl" $RadioButton5.AutoSize = $true $RadioButton5.width = 104 $RadioButton5.height = 20 $RadioButton5.location = New-Object System.Drawing.Point(120,36) $RadioButton5.Font = 'Tahoma,10,12' $Button4 = New-Object system.Windows.Forms.Button $Button4.text = "Add Permissions" $Button4.width = 167.50 $Button4.height = 30 $Button4.location = New-Object System.Drawing.Point(10,490) $Button4.Font = 'Tahoma,10,12' $Button5 = New-Object system.Windows.Forms.Button $Button5.text = "Create Log File" $Button5.width = 167.50 $Button5.height = 30 $Button5.location = New-Object System.Drawing.Point(211,490) $Button5.Font = 'Tahoma,10,12' $FormName.controls.AddRange(@($Label1,$TextBox1,$Button1,$Label2,$Outputbox,$Button2,$Groupbox1,$Button3,$Label3,$Label4,$Button4,$Button5)) $Groupbox1.controls.AddRange(@($RadioButton1,$RadioButton2,$RadioButton3,$RadioButton4,$RadioButton5)) #Write your logic code here #$LogPath = "D:\test\FolderPermissions.csv" $Filename = "FolderPermissions.csv" $Location = $TextBox1.Text $LogPath = $Location+$Filename function GetDirectories { # Variable to store what user types into Input textbox $Input = $Textbox1.Text # Set path to user's input Set-Location $Input # Variable to store results of actioning the Input $Result = $Input | Get-ChildItem | Out-String # Assign Result to OutputBox $Outputbox.Text = $Result } function GetPermissions {$FolderPath = Get-ChildItem -Directory -Path $TextBox1.Text -Recurse -Force $Output = @() ForEach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName ForEach ($Access in $Acl.Access) { $Properties = [ordered]@{'Folder Name'=$Folder.FullName;'Group/User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Output += New-Object -TypeName PSObject -Property $Properties } } $Output | Out-GridView } # Add Read Premissions function ReadAd (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'Read','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } # Add Read + Excute Premissions function Read+ExcuteAd (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'ReadAndExecute','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } # Add Write Premissions function Write+OnlyAd (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'Write','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } # Add Modify Premissions function ModifyAd (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'Modify','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } # Add FullControl Premissions function FullControlAd (){ $HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'FullControl','ContainerInherit,ObjectInherit' , 'None', 'Allow') $Acl.SetAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } # Remove Premissions function Remove (){$HomeFolders = Get-ChildItem $Textbox1.Text -Directory foreach ($HomeFolder in $HomeFolders) { $Path = $HomeFolder.FullName $Acl = (Get-Item $Path).GetAccessControl('Access') $Username = $HomeFolder.Name $Ar = New-Object System.Security.AccessControl.FileSystemAccessRule ($Username,'FullControl','ContainerInherit,ObjectInherit' , 'None', 'Allow') $acl.RemoveAccessRule($Ar) Set-Acl -path $Path -AclObject $Acl } } # Create Log of Premissions function Log (){$FolderPath = dir -Directory -Path $Textbox1.Text -Recurse -Force $Report = @() Foreach ($Folder in $FolderPath) { $Acl = Get-Acl -Path $Folder.FullName foreach ($Access in $acl.Access) { $Properties = [ordered]@{'FolderName'=$Folder.FullName;'AD Group or User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited} $Report += New-Object -TypeName PSObject -Property $Properties } } $Report | Export-Csv -path $LogPath } $Button1.Add_Click({GetDirectories }) $Button2.Add_Click({GetPermissions }) $Button3.Add_Click({ Remove }) $RadioButton1.Add_Click({ }) $RadioButton2.Add_Click({ }) $RadioButton3.Add_Click({ }) $RadioButton4.Add_Click({ }) $RadioButton5.Add_Click({ }) $Button4.Add_Click({ if ($RadioButton1.Checked -eq $true) { ReadAd } if ($RadioButton2.Checked -eq $true) { Read+ExcuteAd } if ($RadioButton3.Checked -eq $true) { Write+OnlyAd } if ($RadioButton4.Checked -eq $true) { ModifyAd } if ($RadioButton5.Checked -eq $true) { FullControlAd } }) $Button5.Add_Click({ Log }) #Notes #Subfolders and Files only: #InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit #PropagationFlags.InheritOnly #This Folder, Subfolders and Files: #InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit #PropagationFlags.None #This Folder, Subfolders and Files: #InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, #PropagationFlags.NoPropagateInherit #This folder and subfolders: #InheritanceFlags.ContainerInherit, #PropagationFlags.None #Subfolders only: #InheritanceFlags.ContainerInherit, #PropagationFlags.InheritOnly #This folder and files: #InheritanceFlags.ObjectInherit, #PropagationFlags.None #This folder and files: #InheritanceFlags.ObjectInherit, #PropagationFlags.NoPropagateInherit [void]$FormName.ShowDialog()
Sephiroth Posted June 18, 2019 Posted June 18, 2019 Glad you got this sorted. WinForms is a pain to get your head around.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now