我正在尝试创建复选框表单/弹出窗口,它使用最终将传递给该函数的PC名称列表。现在我对数组/列表进行了硬编码。我想弹出一个多重检查列表,然后根据输入运行选择的PC上的脚本。我将进行另一项测试,以表明PC是否在线并“预先”检查该PC。我在动态创建复选框时遇到问题。
cls
function GenerateForm {
#param (
# [Object] $PCNames = $null
#)
$PCNames = @("PC1","PC2","PC3")
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$listBox1 = New-Object System.Windows.Forms.ListBox
foreach ($checkBox in $PCNames) {
$checkBox = New-Object System.Windows.Forms.CheckBox
}
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
$b1= $false
$b2= $false
$b3= $false
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
$handler_button1_Click=
{
$listBox1.Items.Clear();
foreach ($checkBox in $PCNames) {
if ($checkBox.Checked) { $listBox1.Items.Add( "$checkBox is checked" ) }
if ( !$checkBox.Checked) { $listBox1.Items.Add("No CheckBox selected....")}
}
}
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}
#----------------------------------------------
#region Generated Form Code
$form1.Text = "Primal Form"
$form1.Name = "form1"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 450
$System_Drawing_Size.Height = 236
$form1.ClientSize = $System_Drawing_Size
$button1.TabIndex = 4
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 75
$System_Drawing_Size.Height = 23
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "Run Script"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
$System_Drawing_Point.Y = 156
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button1_Click)
$form1.Controls.Add($button1)
$listBox1.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 301
$System_Drawing_Size.Height = 212
$listBox1.Size = $System_Drawing_Size
$listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox1.Name = "listBox1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 137
$System_Drawing_Point.Y = 13
$listBox1.Location = $System_Drawing_Point
$listBox1.TabIndex = 3
$form1.Controls.Add($listBox1)
#$checkBox3.UseVisualStyleBackColor = $True
#$System_Drawing_Size = New-Object System.Drawing.Size
#$System_Drawing_Size.Width = 104
#$System_Drawing_Size.Height = 24
#$checkBox3.Size = $System_Drawing_Size
#$checkBox3.TabIndex = 2
#$checkBox3.Text = "CheckBox 3"
#$System_Drawing_Point = New-Object System.Drawing.Point
#$System_Drawing_Point.X = 27
#$System_Drawing_Point.Y = 75
#$checkBox3.Location = $System_Drawing_Point
#$checkBox3.DataBindings.DefaultDataSourceUpdateMode = 0
#$checkBox3.Name = "checkBox3"
#
#$form1.Controls.Add($checkBox3)
#
#
#$checkBox2.UseVisualStyleBackColor = $True
#$System_Drawing_Size = New-Object System.Drawing.Size
#$System_Drawing_Size.Width = 104
#$System_Drawing_Size.Height = 24
#$checkBox2.Size = $System_Drawing_Size
#$checkBox2.TabIndex = 1
#$checkBox2.Text = "CheckBox 2"
#$System_Drawing_Point = New-Object System.Drawing.Point
#$System_Drawing_Point.X = 27
#$System_Drawing_Point.Y = 44
#$checkBox2.Location = $System_Drawing_Point
#$checkBox2.DataBindings.DefaultDataSourceUpdateMode = 0
#$checkBox2.Name = "checkBox2"
#
#$form1.Controls.Add($checkBox2)
foreach ($checkBox in $PCNames) {
$checkBox.UseVisualStyleBackColor = $True
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 104
$System_Drawing_Size.Height = 24
$checkBox.Size = $System_Drawing_Size
$checkBox.TabIndex = 0
$checkBox.Text = "$checkBox"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 27
$System_Drawing_Point.Y = 13
$checkBox.Location = $System_Drawing_Point
$checkBox.DataBindings.DefaultDataSourceUpdateMode = 0
$checkBox.Name = "$checkBox"
$form1.Controls.Add($checkBox)
}
#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
} #End Function
GenerateForm
我得到的错误:
The property 'UseVisualStyleBackColor' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:129 char:5
+ $checkBox.UseVisualStyleBackColor = $True
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Size' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:133 char:5
+ $checkBox.Size = $System_Drawing_Size
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'TabIndex' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:134 char:5
+ $checkBox.TabIndex = 0
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Text' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:135 char:5
+ $checkBox.Text = "$checkBox"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Location' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:139 char:5
+ $checkBox.Location = $System_Drawing_Point
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'DefaultDataSourceUpdateMode' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:140 char:5
+ $checkBox.DataBindings.DefaultDataSourceUpdateMode = 0
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
The property 'Name' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:141 char:5
+ $checkBox.Name = "$checkBox"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'UseVisualStyleBackColor' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:129 char:5
+ $checkBox.UseVisualStyleBackColor = $True
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Size' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:133 char:5
+ $checkBox.Size = $System_Drawing_Size
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'TabIndex' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:134 char:5
+ $checkBox.TabIndex = 0
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Text' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:135 char:5
+ $checkBox.Text = "$checkBox"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Location' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:139 char:5
+ $checkBox.Location = $System_Drawing_Point
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'DefaultDataSourceUpdateMode' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:140 char:5
+ $checkBox.DataBindings.DefaultDataSourceUpdateMode = 0
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
The property 'Name' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:141 char:5
+ $checkBox.Name = "$checkBox"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'UseVisualStyleBackColor' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:129 char:5
+ $checkBox.UseVisualStyleBackColor = $True
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Size' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:133 char:5
+ $checkBox.Size = $System_Drawing_Size
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'TabIndex' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:134 char:5
+ $checkBox.TabIndex = 0
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Text' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:135 char:5
+ $checkBox.Text = "$checkBox"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'Location' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:139 char:5
+ $checkBox.Location = $System_Drawing_Point
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
The property 'DefaultDataSourceUpdateMode' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:140 char:5
+ $checkBox.DataBindings.DefaultDataSourceUpdateMode = 0
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound
The property 'Name' cannot be found on this object. Verify that the property exists and can be set.
At \\util01\cs\Scripts\PowerShell\BradleyDev\CheckBox.ps1:141 char:5
+ $checkBox.Name = "$checkBox"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
答案 0 :(得分:0)
这是因为您在$checkbox
循环中重新定义了foreach
变量。这里有一点解释:
$checkbox = New-Object System.Windows.Forms.CheckBox
...
#at this point $checkbox becomes a string (one of your array items):
foreach($checkbox in $pcnames){
$checkBox.UseVisualStyleBackColor = $True #fails because String object doesnt have the property
...
}
您可以通过在foreach循环中使用另一个变量名来解决此问题。
这也是毫无意义的:
foreach ($checkBox in $PCNames) {
$checkBox = New-Object System.Windows.Forms.CheckBox
}
在这里,您只是为数组中的每个项重新定义$ checkbox,您应该在循环中创建它,为其分配属性