lundi 20 avril 2015

PowerShell Windows Form

I am trying to get some dynamic array values into a PowerShell windows Form. When I use the same command on the PowerShell console it works but in form I get a weird .dot net class returned. Below I mark the code that results in wrong output.

Import-Module NetAdapter
$nics = Get-NetAdapter | Select-Object name, interfacedescription,macaddress

function GenerateForm
{
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$frmVMTask = New-Object System.Windows.Forms.Form
$groupBox1 = New-Object System.Windows.Forms.GroupBox
$btnCancel = New-Object System.Windows.Forms.Button
# $btnOK = New-Object System.Windows.Forms.Button
$ChkVMTask = New-Object System.Windows.Forms.CheckedListBox

$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState


$handler_ChkVMTask_itemCheckChanged =
{
    $VMTask = $ChkVMTask.CheckedItems
    If ($ChkVMTask.CheckedItems.count -gt 1)
    {
        $buttons = [system.windows.forms.messageboxbuttons]::ok;
        [System.Windows.Forms.MessageBoxDefaultButton]::Button1;
        $icon = [system.windows.forms.messageboxicon]::error;

        $tasks = [System.Windows.Forms.MessageBox]::Show("

############################
# Too Many VM Tasks are selected. #
#############################`n
Please Select 1 item only.
"
        , "VM Task Error Summary", $buttons, $icon)
    }
    else
    {
        $VMTask = $ChkVMTask.CheckedItems
    }
}
{
    # Close the form if Cancel clicked
    $frmVMTask.close()

}

$groupBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 16
$System_Drawing_Point.Y = 16
$groupBox1.Location = $System_Drawing_Point
$groupBox1.Name = "groupBox1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 940
$System_Drawing_Size.Width = 800
$groupBox1.Size = $System_Drawing_Size
$groupBox1.TabIndex = 0
$groupBox1.TabStop = $False
$groupBox1.Text = "NIC Automation Task"
$groupBox1.add_Enter($handler_groupBox1_Enter)

$frmVMTask.Controls.Add($groupBox1)

$btnCancel.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 280
$System_Drawing_Point.Y = 430
$btnCancel.Location = $System_Drawing_Point
$btnCancel.Name = "btnCancel"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 20
$System_Drawing_Size.Width = 75
$btnCancel.Size = $System_Drawing_Size
$btnCancel.TabIndex = 15
$btnCancel.Text = "Cancel"
$btnCancel.UseVisualStyleBackColor = $True
$btnCancel.add_Click($handler_btnCancel_Click)

$groupBox1.Controls.Add($btnCancel)

$groupBox1.Controls.Add($lblTask)
$ChkVMTask.[Windows.Forms.SelectionMode]::One;
$ChkVMTask.CheckOnClick = $True;

$ChkVMTask.DataBindings.DefaultDataSourceUpdateMode = 0
$ChkVMTask.FormattingEnabled = $True
foreach ($nic in $nics)
{

Below is where output is code and not same as get-netadapter

    $ChkVMTask.Items.Add("$Nic") | Out-Null
}

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 20
$System_Drawing_Point.Y = 54
$ChkVMTask.Location = $System_Drawing_Point
$ChkVMTask.Name = "ChkVMTask"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 158
$System_Drawing_Size.Width = 722
$ChkVMTask.Size = $System_Drawing_Size
$ChkVMTask.TabIndex = 1
$ChkVMTask.Tag = ""
$ChkVMTask.add_SelectedIndexChanged($handler_ChkVMTask_itemCheckChanged)
$groupBox1.Controls.Add($ChkVMTask)

#Save the initial state of the form
$InitialFormWindowState = $frmVMTask.WindowState
#Init the OnLoad event to correct the initial state of the form
$frmVMTask.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$frmVMTask.ShowDialog() | Out-Null
$frmVMTask.close() #new parent test close visible and hidden work perfectly. close not yet.

} #End Function

#Call the Function
GenerateForm

Aucun commentaire:

Enregistrer un commentaire