开发者

Variable Expansion within another Variable (Powershell)

So I've an array-variable of servers that is dynamically created from an external script. I now need to populate my GUI form with a list of check-boxes for each se开发者_如何学Gorver. This will work as a selection mechanism when doing deployments to said servers.

As I don't know how many servers are going to be in my list, this form will have to have the check-boxes created dynamically. The issue I'm having is setting parameters of these new variables, and adding to the form. I just don't get how I can force PS to expand my iterator variable within the checkbox variable name. Here's what I have currently, which will create my variables but not apply the updates to the parameters:

$form1.Text = "Server Selection"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 235
$System_Drawing_Size.Height = 500
$form1.ClientSize = $System_Drawing_Size
$i = 0
$y = 0
$serverList | %{
    New-Variable -Name "Checkbox$i" -value (New-Object System.Windows.Forms.CheckBox)
    set-variable -name "Checkbox$i.VisualStyleBackColor" -value $true
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 104
    $System_Drawing_Size.Height = 24
    $Checkbox{i}.Size = $System_Drawing_Size
    $Checkbox{i}.TabIndex = $i
    $Checkbox{i}.text = $_
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 60
    $System_Drawing_Point.Y = 21 + $y
    $Checkbox{i}.Location = $System_Drawing_Point
    $Checkbox{i}.DataBindings.DefaultDataSourceUpdateMode = 0
    $Checkbox{i}.name = "server"
    $y = $y + 20
    $i++
    $form1.Controls.Add($Checkbox{i})


}

Anyone know how I can do this expansion or assignment correctly?


First to answer your specific question:

(Get-Variable "Checkbox$i").Value.Size = $System_Drawing_Size

But more generally, why don't you store the list of checkboxes in an array and access them by index?

Edit: Let me show you.

$Checkboxes = @()
$serverList | %{
    $Checkboxes += New-Object System.Windows.Forms.CheckBox
    $Checkboxes[-1].VisualStyleBackColor = $true
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 104
    $System_Drawing_Size.Height = 24
    $Checkboxes[-1].Size = $System_Drawing_Size
    $Checkboxes[-1].TabIndex = $i
    $Checkboxes[-1].text = $_
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 60
    $System_Drawing_Point.Y = 21 + $y
    $Checkboxes[-1].Location = $System_Drawing_Point
    $Checkboxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0
    $Checkboxes[-1].name = "server"
    $y = $y + 20
    $form1.Controls.Add($Checkboxes[-1])
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜