简体   繁体   English

从带有文件打开对话框的文件夹中获取 acl

[英]get acl from folders with file open dialog

I have built a little gui for getting the acl permissions for folders.我已经建立了一个小 gui 来获取文件夹的 acl 权限。 with the path button i want to specify the folder path with a folder browser dialog and with the permissions button i want to get the acl.使用路径按钮,我想使用文件夹浏览器对话框指定文件夹路径,使用权限按钮,我想获取 acl。 unfortunately the permissions button don't work because it can't get the folder path from the get-folder function.不幸的是,权限按钮不起作用,因为它无法从 get-folder 函数中获取文件夹路径。 what's wrong with the function?功能有什么问题?

#################################################### Functions #######################################################
$path = Function Get-Folder ($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
    $Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
    $Ordnername.Description = "Ordner auswählen"
    $Ordnername.rootfolder = "MyComputer"
    if($Ordnername.ShowDialog() -eq "OK")
    {
        $Ordner += $Ordnername.SelectedPath
    }
    return $Ordner
}
 



        ############################################## GetPermissions function
 
          
        function GetPermissions{

            #$Folder = get-folder
            $Result = (Get-ACL $path).access | Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags | Out-string
            $outputBox.Text = $Result

        }


        function Close{
            $Form.Close()
        }

        



###################### CREATING PS GUI TOOL #############################

    #### Form settings #################################################################
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")  

    $Form = New-Object System.Windows.Forms.Form
    $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
    $Form.Text = "Folder Permissions"    
    $Form.Size = New-Object System.Drawing.Size(1120,330)  
    $Form.StartPosition = "CenterScreen" #loads the window in the center of the screen
    $Form.BackgroundImageLayout = "Zoom"
    $Form.MinimizeBox = $False
    $Form.MaximizeBox = $False
    $Form.WindowState = "Normal"
    $Form.SizeGripStyle = "Hide"
    $Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
    $Form.Icon = $Icon

    #### Input window with "Folder Path" label ##########################################
    #$InputBox = New-Object System.Windows.Forms.TextBox 
    #$InputBox.Location = New-Object System.Drawing.Size(10,50) 
    #$InputBox.Size = New-Object System.Drawing.Size(180,20) 
    #$Form.Controls.Add($InputBox)
    #$Label2 = New-Object System.Windows.Forms.Label
    #$Label2.Text = "Folder Path:"
    #$Label2.AutoSize = $True
    #$Label2.Location = New-Object System.Drawing.Size(15,30) 
    #$Form.Controls.Add($Label2)

    #### Group boxes for buttons ########################################################
    $groupBox = New-Object System.Windows.Forms.GroupBox
    $groupBox.Location = New-Object System.Drawing.Size(10,95) 
    $groupBox.size = New-Object System.Drawing.Size(180,180)
    $groupBox.text = "Controls:" 
    
    $Form.Controls.Add($groupBox) 

###################### BUTTONS ##########################################################

    #### Path ###################################################################
    $Path = New-Object System.Windows.Forms.Button
    $Path.Location = New-Object System.Drawing.Size(10,10)
    $Path.Size = New-Object System.Drawing.Size(150,60)
    $Path.Text = "Path"
    $Path.Add_Click({Get-folder})
    $Path.Cursor = [System.Windows.Forms.Cursors]::Hand
    $Form.Controls.Add($Path)


    #### Permissions ###################################################################
    $Permissions = New-Object System.Windows.Forms.Button
    $Permissions.Location = New-Object System.Drawing.Size(15,25)
    $Permissions.Size = New-Object System.Drawing.Size(150,60)
    $Permissions.Text = "Permissions"
    $Permissions.Add_Click({GetPermissions})
    $Permissions.Cursor = [System.Windows.Forms.Cursors]::Hand
    $groupBox.Controls.Add($Permissions)

    #### Close ###################################################################
    $Close = New-Object System.Windows.Forms.Button
    $Close.Location = New-Object System.Drawing.Size(15,100)
    $Close.Size = New-Object System.Drawing.Size(150,60)
    $Close.Text = "Close"
    $Close.Add_Click({Close})
    $Close.Cursor = [System.Windows.Forms.Cursors]::Hand
    $groupBox.Controls.Add($Close)


###################### END BUTTONS ######################################################

    #### Output Box Field ###############################################################
    $outputBox = New-Object System.Windows.Forms.RichTextBox
    $outputBox.Location = New-Object System.Drawing.Size(200,20) 
    $outputBox.Size = New-Object System.Drawing.Size(900,265)
    $outputBox.Font = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
    $outputBox.MultiLine = $True
    $outputBox.ScrollBars = "Vertical"
    $Form.Controls.Add($outputBox)

    ##############################################

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

The reason for that mainly has to do with scoping , plus you should not write the function as $path = Function ... making it a call to the function.其原因主要与作用域有关,而且您不应该将函数编写为$path = Function ...使其成为对函数的调用。

Also, $Ordner += $Ordnername.SelectedPath is wrong, because you have never defined what $Ordner is in the function.此外, $Ordner += $Ordnername.SelectedPath是错误的,因为您从未在函数中定义$Ordner是什么。

Below a rewrite of your code where I took the liberty to change some variable names to make them more self-explanatory:在重写您的代码下面,我冒昧地更改了一些变量名称以使它们更加不言自明:

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

###################### CREATING PS GUI TOOL #############################

# define your selected path variable and initialize to nothing
$SelectedPath = $null

#################################################### Functions #######################################################
function Get-Folder {
    $Ordnername = New-Object System.Windows.Forms.FolderBrowserDialog
    $Ordnername.Description = "Ordner auswählen"
    $Ordnername.rootfolder = "MyComputer"
    if($Ordnername.ShowDialog() -eq "OK") {
        $script:SelectedPath = $Ordnername.SelectedPath  # use script scoping here
        $outputBox.Text = $script:SelectedPath
    }
}

############################################## GetPermissions function
function Get-Permissions {
    $outputBox.Text = ''  # clear the textbox
    if (-not [string]::IsNullOrWhiteSpace($script:SelectedPath)) {
        $Result = (Get-ACL $script:SelectedPath).Access |   # use script scoping here
                   Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags | Out-string
        $outputBox.Text = $script:SelectedPath + "`r`n`r`n" + $Result
    }
}

#### Form settings #################################################################

$Form                       = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle       = [System.Windows.Forms.FormBorderStyle]::FixedSingle #modifies the window border
$Form.Text                  = "Folder Permissions"
$Form.Size                  = New-Object System.Drawing.Size(1120,330)
$Form.StartPosition         = "CenterScreen" #loads the window in the center of the screen
$Form.BackgroundImageLayout = "Zoom"
$Form.MinimizeBox           = $False
$Form.MaximizeBox           = $False
$Form.WindowState           = "Normal"
$Form.SizeGripStyle         = "Hide"
$Icon                       = [System.Drawing.Icon]::ExtractAssociatedIcon((Join-Path -Path $PSHOME -ChildPath 'powershell.exe'))
$Form.Icon                  = $Icon

#### Group boxes for buttons ########################################################
$groupBox          = New-Object System.Windows.Forms.GroupBox
$groupBox.Location = New-Object System.Drawing.Size(10,95)
$groupBox.size     = New-Object System.Drawing.Size(180,180)
$groupBox.text     = "Controls:"
$Form.Controls.Add($groupBox)

###################### BUTTONS ##########################################################

#### Path button ###################################################################
$PathButton          = New-Object System.Windows.Forms.Button
$PathButton.Location = New-Object System.Drawing.Size(10,10)
$PathButton.Size     = New-Object System.Drawing.Size(150,60)
$PathButton.Text     = "Path"
$PathButton.Cursor   = [System.Windows.Forms.Cursors]::Hand
$PathButton.Add_Click({Get-Folder})
$Form.Controls.Add($PathButton)

#### Permissions button ###################################################################
$Permissions          = New-Object System.Windows.Forms.Button
$Permissions.Location = New-Object System.Drawing.Size(15,25)
$Permissions.Size     = New-Object System.Drawing.Size(150,60)
$Permissions.Text     = "Permissions"
$Permissions.Cursor   = [System.Windows.Forms.Cursors]::Hand
$Permissions.Add_Click({Get-Permissions})
$groupBox.Controls.Add($Permissions)

#### Close ###################################################################
$Close          = New-Object System.Windows.Forms.Button
$Close.Location = New-Object System.Drawing.Size(15,100)
$Close.Size     = New-Object System.Drawing.Size(150,60)
$Close.Text     = "Close"
$Close.Cursor   = [System.Windows.Forms.Cursors]::Hand
$Close.Add_Click({$Form.Close()})
$groupBox.Controls.Add($Close)


###################### END BUTTONS ######################################################

#### Output Box Field ###############################################################
$outputBox            = New-Object System.Windows.Forms.RichTextBox
$outputBox.Location   = New-Object System.Drawing.Size(200,20)
$outputBox.Size       = New-Object System.Drawing.Size(900,265)
$outputBox.Font       = New-Object System.Drawing.Font("Consolas", 8 ,[System.Drawing.FontStyle]::Regular)
$outputBox.MultiLine  = $True
$outputBox.ScrollBars = "Vertical"
$Form.Controls.Add($outputBox)

##############################################

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

# destroy the form from memory
$Form.Dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM