You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell GUI获取文件夹ACL路径传参错误排查

PowerShell文件夹ACL查询GUI工具路径传值错误排查

问题描述

开发了一款用于获取文件夹ACL权限的PowerShell GUI工具,预期运行逻辑:

  • 点击「Path」按钮时,调用FolderBrowserDialog文件夹浏览对话框选择目标文件夹,记录选中的路径
  • 点击「Permissions」按钮时,读取已选路径对应的ACL权限信息,输出到界面展示
    目前工具运行异常:点击Permissions按钮时无法获取Get-Folder函数返回的选中文件夹路径,功能失效。

原实现代码

#################################################### 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()

代码错误点说明

  • 变量覆盖+赋值语法错误:开头用$path = Function Get-Folder(){}的写法本身不符合PowerShell函数定义规则,后续创建Path按钮时又执行$Path = New-Object System.Windows.Forms.Button,直接把之前的$path变量覆盖为按钮控件对象,GetPermissions中传给Get-ACL的$path实际是按钮对象,完全不是文件夹路径。
  • Get-Folder函数返回值错误:函数中未初始化$Ordner变量,选中路径后赋值给$Ordner,最终却return未正确赋值的变量,没有返回对话框选中的SelectedPath字符串。
  • 按钮点击事件未接收返回值:Path按钮绑定的点击事件仅调用Get-folder,没有将函数返回的路径存储到专用变量中,返回值直接被丢弃。
  • 变量作用域问题:事件脚本块内默认创建的变量为局部作用域,GetPermissions函数默认无法读取局部作用域的路径值,需要使用脚本级/全局变量存储选中路径。

关键修正代码

# 开头新增脚本级变量存储选中路径
$script:selectedFolderPath = $null

# 修正Get-Folder函数定义,移除错误的$path=前缀
function Get-Folder ($initialDirectory)
{
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")|Out-Null
    $folderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $folderDialog.Description = "选择文件夹"
    $folderDialog.rootfolder = "MyComputer"
    if($folderDialog.ShowDialog() -eq "OK")
    {
        return $folderDialog.SelectedPath
    }
    return $null
}

# 修正Path按钮点击事件,将返回值存入脚本级变量
$Path.Add_Click({$script:selectedFolderPath = Get-folder})

# 修正GetPermissions函数,增加空值校验,使用脚本级路径变量
function GetPermissions{
    if([string]::IsNullOrEmpty($script:selectedFolderPath)){
        $outputBox.Text = "请先选择目标文件夹"
        return
    }
    $Result = (Get-ACL $script:selectedFolderPath).access | Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags | Out-string
    $outputBox.Text = $Result
}

内容的提问来源于stack exchange,提问作者chkdsk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 14:51:35