winforms 列出AD组,创建CSV

kxxlusnw  于 2023-02-13  发布在  其他
关注(0)|答案(1)|浏览(96)

我尝试创建一个表单,询问共享名称、搜索深度和输出文件名,并创建一个CSV文件(这将需要进一步处理,但现在这并不重要)。
我有图形设计和脚本,但我不能合并他们实际上一起工作。
下面是代码:

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

$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Folder Group Details v1.6"
$Form.Size = New-Object System.Drawing.Size(700,400)
$Form.StartPosition = "Manual"
$Form.Location =  New-Object System.Drawing.Size(90,90)
$Form.KeyPreview = $True
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"

$Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Bold)

$Form.Width = $objImage.Width
$Form.Height = $objImage.Height
 
# Title
$Title = New-Object System.Windows.Forms.label
$Title.Location = New-Object System.Drawing.Size(150,10)
$Title.Size = New-Object System.Drawing.Size(500,70)
$Title.BackColor = "Transparent"
$Title.ForeColor = "darkblue"
$Title.Text = "Create a CSV file for the given path and depth"
$Form.Controls.Add($Title)
$Title.Font = $Font

# Start button
$Start_Button = New-Object System.Windows.Forms.Button
$Start_Button.Location = New-Object System.Drawing.Size(50,300)
$Start_Button.Size = New-Object System.Drawing.Size(100,30)
$Start_Button.Text = "Start"
$Start_Button.Font = $Font
$Start_Button.Add_Click($Button_Click)
$Form.Controls.Add($Start_Button)

#Cancel button
$Cancel_Button = New-Object System.Windows.Forms.Button
$Cancel_Button.Location = New-Object System.Drawing.Size(550,300)
$Cancel_Button.Size = New-Object System.Drawing.Size(100,30)
$Cancel_Button.Text = "Cancel"
$Cancel_Button.Font = $Font
$Cancel_Button.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form.Controls.Add($Cancel_Button)

$arg0_label = New-Object System.Windows.Forms.Label
$arg0_label.Location = New-Object System.Drawing.Point(50,80)
$arg0_label.Size = New-Object System.Drawing.Size(280,20)
$arg0_label.Text = 'Please enter the root of path:'
$Form.Controls.Add($arg0_label)

$arg0_textBox = New-Object System.Windows.Forms.TextBox
$arg0_textBox.Location = New-Object System.Drawing.Point(80,100)
$arg0_textBox.Size = New-Object System.Drawing.Size(250,30)
$arg0_textBox.Multiline = $False
$arg0_textbox.AcceptsReturn = $False
$Form.Controls.Add($arg0_textBox)

$arg2_label = New-Object System.Windows.Forms.Label
$arg2_label.Location = New-Object System.Drawing.Point(50,130)
$arg2_label.Size = New-Object System.Drawing.Size(280,20)
$arg2_label.Text = 'Please enter the depth of search:'
$Form.Controls.Add($arg2_label)

$arg2_textBox = New-Object System.Windows.Forms.TextBox
$arg2_textBox.Location = New-Object System.Drawing.Point(80,150)
$arg2_textBox.Size = New-Object System.Drawing.Size(250,30)
$arg2_textBox.Multiline = $False
$arg2_textbox.AcceptsReturn = $False
$Form.Controls.Add($arg2_textBox)

$arg1_label = New-Object System.Windows.Forms.Label
$arg1_label.Location = New-Object System.Drawing.Point(50,180)
$arg1_label.Size = New-Object System.Drawing.Size(280,20)
$arg1_label.Text = 'Please enter the output file full path and name:'
$Form.Controls.Add($arg1_label)

$arg1_textBox = New-Object System.Windows.Forms.TextBox
$arg1_textBox.Location = New-Object System.Drawing.Point(80,200)
$arg1_textBox.Size = New-Object System.Drawing.Size(250,30)
$arg1_textBox.Multiline = $False
$arg1_textbox.AcceptsReturn = $False
$Form.Controls.Add($arg1_textBox)
#$outfile = "$arg1_textBox.Text"

# Button event
$Button_Click = {

    $RootPath = $arg0_textBox.Text
    $OutFile = $arg1_textBox.Text
    $recurse_max = $arg2_textBox.Text
    $recurse_current = 0
    $Header = "Folder Path,IdentityReference,FileSystemRights,IsInherited"
    
    Remove-Item $OutFile
    
    Add-Content -Value $Header -Path $OutFile 
    
    function print_acl
    {
        $RootPath = $arg0_textBox.Text
        $recurse_current = $arg1_textBox.Text
        Write-Host "$RootPath : $recurse_current"
        if ($RootPath -ne "" -and $null -ne $RootPath ) {       
            $ACLs = get-acl $RootPath | ForEach-Object { $_.Access  }
            Foreach ($ACL in $ACLs){
                $rights = $ACL.FileSystemRights -replace ", ", ":"
                $OutInfo = $RootPath + "," + $ACL.IdentityReference  + "," + $rights + "," + $ACL.IsInherited
                Add-Content -Value $OutInfo -Path $OutFile
            }
            
            if ($recurse_current -lt $recurse_max) {    
                $Folders = Get-ChildItem $RootPath | Where-Object {$_.psiscontainer -eq $true}
                foreach ($Folder in $Folders){
                    $recurse_current = $arg1_textBox.Text + 1
                    print_acl $Folder.Fullname $recurse_current
                }
            }
        }
    }
    
    print_acl $RootPath $recurse_current
    
}

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

问题是我不明白为什么开始按钮什么都不做。我做错了什么?我是这个领域的初学者,很抱歉我这么差劲。谢谢。

wgx48brx

wgx48brx1#

不需要为Get-ChildItem创建自己的递归函数-它有一个-Depth参数

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

$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Folder Group Details v1.6"
$Form.Size = '700,400'
$Form.StartPosition = "Manual"
$Form.Location =  '90,90'
$Form.KeyPreview = $True
$Form.MaximumSize = $Form.Size
$Form.MinimumSize = $Form.Size
$Form.MinimizeBox = $True
$Form.MaximizeBox = $False
$Form.WindowState = "Normal"
$Form.SizeGripStyle = "Hide"

$Font = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Bold)

$Form.Width = $objImage.Width
$Form.Height = $objImage.Height
 
# Title
$Title = New-Object System.Windows.Forms.label
$Title.Location = '150,10'
$Title.Size = '500,70'
$Title.BackColor = "Transparent"
$Title.ForeColor = "darkblue"
$Title.Text = "Create a CSV file for the given path and depth"
$Form.Controls.Add($Title)
$Title.Font = $Font

# Start button
$Start_Button = New-Object System.Windows.Forms.Button
$Start_Button.Location = '50,300'
$Start_Button.Size = '100,30'
$Start_Button.Text = "Start"
$Start_Button.Font = $Font
$Start_Button.Add_Click($Button_Click)
$Form.Controls.Add($Start_Button)

#Cancel button
$Cancel_Button = New-Object System.Windows.Forms.Button
$Cancel_Button.Location = '550,300'
$Cancel_Button.Size = '100,30'
$Cancel_Button.Text = "Cancel"
$Cancel_Button.Font = $Font
$Cancel_Button.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form.Controls.Add($Cancel_Button)

$arg0_label = New-Object System.Windows.Forms.Label
$arg0_label.Location = '50,80'
$arg0_label.Size = '280,20'
$arg0_label.Text = 'Please enter the root of path:'
$Form.Controls.Add($arg0_label)

$arg0_textBox = New-Object System.Windows.Forms.TextBox
$arg0_textBox.Location = '80,100'
$arg0_textBox.Size = '250,30'
$arg0_textBox.Multiline = $False
$arg0_textbox.AcceptsReturn = $False
$Form.Controls.Add($arg0_textBox)

$arg2_label = New-Object System.Windows.Forms.Label
$arg2_label.Location = '50,130'
$arg2_label.Size = '280,20'
$arg2_label.Text = 'Please enter the depth of search:'
$Form.Controls.Add($arg2_label)

$arg2_textBox = New-Object System.Windows.Forms.TextBox
$arg2_textBox.Location = '80,150'
$arg2_textBox.Size = '250,30'
$arg2_textBox.Multiline = $False
$arg2_textbox.AcceptsReturn = $False
$Form.Controls.Add($arg2_textBox)

$arg1_label = New-Object System.Windows.Forms.Label
$arg1_label.Location = '50,180'
$arg1_label.Size = '280,20'
$arg1_label.Text = 'Please enter the output file full path and name:'
$Form.Controls.Add($arg1_label)

$arg1_textBox = New-Object System.Windows.Forms.TextBox
$arg1_textBox.Location = '80,200'
$arg1_textBox.Size = '250,30'
$arg1_textBox.Multiline = $False
$arg1_textbox.AcceptsReturn = $False
$Form.Controls.Add($arg1_textBox)

# Button event
$Button_Click = {

    $RootPath = $arg0_textBox.Text
    $OutFile = $arg1_textBox.Text
    $recurse_max = $arg2_textBox.Text

    $Results = Get-ChildItem -Path $RootPath -Depth $recurse_max | Where-Object {$_.psiscontainer -eq $true} | ForEach-Object {
        $FileDetail = $_
        $ACLs = get-acl $_.FullName | ForEach-Object { $_.Access  }
        $ACLs | ForEach-Object {
            $rights = $_.FileSystemRights -replace ", ", ":"
            [pscustomobject]@{Folder=$FileDetail.Name;Path=$FileDetail.FullName;IdentityReference=$_.IdentityReference;FileSystemRights=$rights;IsInherited=$_.IsInherited}
        }
    }
    $Results | Export-Csv -Path $OutFile -NoTypeInformation    
}

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

相关问题