Как обработать кнопку GUI для выполнения функции с аргументом, используя PowerShell? - PullRequest
0 голосов
/ 10 июня 2019

Я хочу использовать кнопку GUI для выполнения функции с аргументом. Я пробовал, но когда я нажимаю кнопку, он просит меня ввести параметры (FilePath, a_section, b_item, and store). Мне нужна помощь, я новичок в Powershell.

Function Sel_File($InitialDirectory)
{
    Add-Type -AssemblyName System.Windows.Forms
    $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
    $OpenFileDialog.Title = "Please Select File"
    $OpenFileDialog.InitialDirectory = $InitialDirectory
    $OpenFileDialog.filter = “All files (*.*)| *.*”
    If ($OpenFileDialog.ShowDialog() -eq "Cancel") 
    {
    [System.Windows.Forms.MessageBox]::Show("No File Selected. Please select a file !", "Error", 0, 
    [System.Windows.Forms.MessageBoxIcon]::Exclamation)
    }   $Global:SelectedFile = $OpenFileDialog.SafeFileName

}

#------------------------------------------------------------------------------------------------------#
$src = @"
[DllImport("KERNEL32.DLL")]
public static extern uint GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, 
System.Text.StringBuilder lpReturnedString, uint nSize, string lpFileName);
[DllImport("KERNEL32.DLL")]
public static extern uint WritePrivateProfileString(string lpAppName, string lpKeyName, 
string lpString, string lpFileName);
"@
Add-Type -MemberDefinition $src -Namespace WinApi -Name IniFileIO
#------------------------------------------------------------------------------------------------------#


#-------------------------- Function for Freezing ML --------------------------#
Function Read-File
{
    Param(
    [parameter(mandatory=$true)]$FilePath,
    [parameter(mandatory=$true)] $a_section,
    [parameter(mandatory=$true)] $b_item,
    [parameter(mandatory=$true)] $store


    )
    $input_file = $FilePath
    $ini_file = @{}

    Get-Content $input_file | ForEach-Object {
    $_.Trim()
    } | Where-Object {


    $_ -notmatch '^(;|$)'
    } | ForEach-Object {
    if ($_ -match '^\[.*\]$') {
        $section = $_ -replace '\[|\]'
        $ini_file[$section] = @{}
    } else {
        $key, $value = $_ -split '\s*=\s*', 2
        $ini_file[$section][$key] = $value
    }
    }


     #--------
     $Path_Store = $store
     $Get_Reg = $ini_file.($a_section).($b_item)
     $Get_Coun = $Get_Reg.Substring(0,3)
     #---------
     #$ID = ([System.IO.FileInfo]$Global:SelectedFile).BaseName
     $ID = $Global:SelectedFile.Substring(0,11)
     $Header = ";********** Output **********"
     $B_ID = ";Build_ID=" + $ID + "#" + "S" + $Get_Coun + "#" + "D" + $Get_Coun
     $FB = ";Feature=123123"


     if ($NB.Checked -eq $true) {$Prefix = ";ML_Prefix=" + "BNB"}
     if ($DPC.Checked -eq $true) {$Prefix = ";ML_Prefix=" + "DPC"}

     $CRM = ";CRM="  + $b_item
     $Outer = ";**********************************"
     #---------

     $Output = $Header, $B_ID, $FB, $Prefix, $CRM , $Outer | Out-File $Path_Store\A1

    }


Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.AutoSize                   = $true
$Form.text                       = "Auto GM Creation"
$Form.TopMost                    = $true
#----------------------

$ChooseML_L                      = New-Object system.Windows.Forms.Label
$ChooseML_L.text                 = "MLs"
$ChooseML_L.AutoSize             = $true
$ChooseML_L.width                = 25
$ChooseML_L.height               = 10
$ChooseML_L.location             = New-Object System.Drawing.Point(28,20)
$ChooseML_L.ForeColor            = "#000000"

$SelectML                        = New-Object system.Windows.Forms.TextBox
$SelectML.AutoSize               = $true
$SelectML.width                  = 150
$SelectML.height                 = 30
$SelectML.location               = New-Object System.Drawing.Point(120,40)
$SelectML.Text                   = "Selected ML"

$ChooseML                        = New-Object System.Windows.Forms.Button
$ChooseML.text                   = "Select File"
$ChooseML.AutoSize               = $true
$ChooseML.width                  = 90
$ChooseML.height                 = 20
$ChooseML.location               = New-Object System.Drawing.Point(28,38)
$ChooseML.ForeColor              = "#ffffff"
$ChooseML.BackColor              = "#093c76"

$ChooseML.Add_Click({Sel_File
$SelectML.Text = $Global:SelectedFile
}) 

#----------
$Apply                         = New-Object system.Windows.Forms.Button
$Apply.BackColor               = "#6996c8"
$Apply.text                    = "Apply"
$Apply.width                   = 99
$Apply.height                  = 30
$Apply.location                = New-Object System.Drawing.Point(320,190)
$Apply.Add_Click({Read-File})

#----------
$Cancel                         = New-Object system.Windows.Forms.Button
$Cancel.BackColor               = "#6996c8"
$Cancel.text                    = "Cancel"
$Cancel.width                   = 98
$Cancel.height                  = 30
$Cancel.location                = New-Object System.Drawing.Point(450,190)
$Cancel.Add_Click({$Form.Close()})

#-----------

$Prefix                      = New-Object system.Windows.Forms.Label
$Prefix.text                 = "Prefix"
$Prefix.AutoSize             = $true
$Prefix.width                = 25
$Prefix.height               = 10
$Prefix.location             = New-Object System.Drawing.Point(28,80)
$Prefix.ForeColor            = "#000000"

$NB                              = New-Object system.Windows.Forms.RadioButton
$NB.text                         = "NB"
$NB.AutoSize                     = $true
$NB.BackColor                    = "#4a90e2"
$NB.width                        = 104
$NB.height                       = 20
$NB.location                     = New-Object System.Drawing.Point(28,100)

$DPC                             = New-Object system.Windows.Forms.RadioButton
$DPC.text                        = "DPC"
$DPC.AutoSize                    = $true
$DPC.BackColor                   = "#4a90e2"
$DPC.width                       = 104
$DPC.height                      = 20
$DPC.location                    = New-Object System.Drawing.Point(100,100)

$Form.Controls.AddRange(@($ChooseML, $Prefix, $ChooseML_L, $Apply, $Cancel, $SelectML, $NB, $DPC))
[void] $Form.ShowDialog()

$cmd, $params = $args
& $cmd @params

Мое ожидание, когда я нажимаю кнопку «Apply», функция Read-File выполняется без ввода параметров. Я попробовал этот код.

...