Powershell 如何让Scriptblock向表单中添加元素?

Powershell 如何让Scriptblock向表单中添加元素?,powershell,Powershell,我有一个启动表单并允许用户进行下拉选择的脚本 然后将所选内容放入数组中 然后,我需要在while循环中验证数组的5个值,如果所有值都存在,则会显示提交按钮 While循环在scriptblock中运行,但是由于这本质上是一个单独的代码,我无法让它将submit按钮添加到原始表单中 这是我的密码 $BuildTech = "x:\Scripts\PowerShell\BuildTech.txt" $Acrobat = "x:\Scripts\PowerShell\Acrobat.txt" $Par

我有一个启动表单并允许用户进行下拉选择的脚本

然后将所选内容放入数组中

然后,我需要在while循环中验证数组的5个值,如果所有值都存在,则会显示提交按钮

While循环在scriptblock中运行,但是由于这本质上是一个单独的代码,我无法让它将submit按钮添加到原始表单中

这是我的密码

$BuildTech = "x:\Scripts\PowerShell\BuildTech.txt"
$Acrobat = "x:\Scripts\PowerShell\Acrobat.txt"
$Partition = "x:\Scripts\Powershell\Drives.txt"
$MSOffver = "x:\Scripts\Powershell\OfficeVer.txt"
$DepCode = "x:\Scripts\Powershell\DepCodes.txt"

$CompName = $Null

[array]$BuildTechDropArray = Get-Content $BuildTech
[array]$AcroDropArray = Get-Content $Acrobat
[array]$PartitionDropArray = Get-Content $Partition
[array]$MSOffVerDropArray = Get-Content $MSOffver
[array]$DepCodeDropArray = Get-Content $DepCode

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

[System.Windows.Forms.Application]::EnableVisualStyles();

$Form = New-Object System.Windows.Forms.Form

$Radio1 = New-Object System.Windows.Forms.RadioButton
$Radio2 = New-Object System.Windows.Forms.RadioButton
$Radio3 = New-Object System.Windows.Forms.RadioButton
$Radio4 = New-Object System.Windows.Forms.RadioButton
$Radio5 = New-Object System.Windows.Forms.RadioButton
$Radio6 = New-Object System.Windows.Forms.RadioButton

$Radio1.Checked = $True

Function Get-OptimalSize()
{
    Param ([int64]$sizeInBytes)
    switch ($sizeInBytes)
    {
        { $sizeInBytes -ge 1TB } { "{0:n$sigDigits}" -f ($sizeInBytes/1TB) + " TB"; break }
        { $sizeInBytes -ge 1GB } { "{0:n$sigDigits}" -f ($sizeInBytes/1GB) + " GB"; break }
        { $sizeInBytes -ge 1MB } { "{0:n$sigDigits}" -f ($sizeInBytes/1MB) + " MB"; break }
        { $sizeInBytes -ge 1KB } { "{0:n$sigDigits}" -f ($sizeInBytes/1KB) + " KB"; break }
        Default { "{0:n$sigDigits}" -f $sizeInBytes + " Bytes" }
    }
}

$diskdrive = gwmi win32_diskdrive
$a =
foreach ($drive in $diskdrive)
{

    $InterFaceType = "$($Drive.InterfaceType)"
    If (!($InterFaceType -eq "USB"))
    {
        $Size = "$($drive.size)"
        $DriveModel = "`nDrive: $($drive.deviceid.substring(4))  Model: $($drive.model)"
        $Size = Get-OptimalSize $Size
        $DriveModel + "     " + $Size
        [Array]$a = $a + $Result
    }
}

$a = $a | Sort-Object

$Drive1 = $a[0] 
$Drive2 = $a[1]
$Drive3 = $a[2]
$Drive4 = $a[3]
$Drive5 = $a[4]
$Drive6 = $a[5]

If (-not ($Drive1 -eq $null)) { $Drive1 = $Drive1.trim() }
If (-not ($Drive2 -eq $null)) { $Drive2 = $Drive2.trim() }
If (-not ($Drive3 -eq $null)) { $Drive3 = $Drive3.trim() }
If (-not ($Drive3 -eq $null)) { $Drive4 = $Drive4.trim() }
If (-not ($Drive3 -eq $null)) { $Drive5 = $Drive5.trim() }
If (-not ($Drive3 -eq $null)) { $Drive6 = $Drive6.trim() }

If($a[0]){
    $DiskID1 = $a[0].SubString(21,1)
    $Radio1.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15    
    $Radio1.Size = $System_Drawing_Size
    $Radio1.TabIndex = 8
    $Radio1.Text = $Drive1
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 255
    $Radio1.Location = $System_Drawing_Point
    $Radio1.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio1.Name = "Radio1"
    $Form.Controls.Add($Radio1)

}
If($a[1]){
    $DiskID2 = $a[1].SubString(21,1)
    $Radio2.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio2.Size = $System_Drawing_Size
    $Radio2.TabIndex = 9
    $Radio2.Text = $Drive2
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 275
    $Radio2.Location = $System_Drawing_Point
    $Radio2.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio2.Name = "Radio2"
    $Form.Controls.Add($Radio2)
}
If($a[2]){
    $DiskID3 = $a[2].SubString(21,1)
    $Radio3.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio3.Size = $System_Drawing_Size
    $Radio3.TabIndex = 10
    $Radio3.Text = $Drive3
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 295
    $Radio3.Location = $System_Drawing_Point
    $Radio3.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio3.Name = "Radio3"
    $Form.Controls.Add($Radio3)
}
If($a[3]){
    $DiskID4 = $a[3].SubString(21,1)
    $Radio4.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio4.Size = $System_Drawing_Size
    $Radio4.TabIndex = 11
    $Radio4.Text = $Drive4
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 315
    $Radio4.Location = $System_Drawing_Point
    $Radio4.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio4.Name = "Radio4"
    $Form.Controls.Add($Radio4)
}
If($a[4]){  
    $DiskID5 = $a[4].SubString(21,1)
    $Radio5.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio5.Size = $System_Drawing_Size
    $Radio5.TabIndex = 12
    $Radio5.Text = $Drive5
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 335
    $Radio5.Location = $System_Drawing_Point
    $Radio5.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio5.Name = "Radio5"
    $Form.Controls.Add($Radio5)
}
If($a[5]){
    $DiskID6 = $a[5].SubString(21,1)
    $Radio6.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio6.Size = $System_Drawing_Size
    $Radio6.TabIndex = 13
    $Radio6.Text = $Drive6
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 355
    $Radio6.Location = $System_Drawing_Point
    $Radio6.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio6.Name = "Radio6"
    $Form.Controls.Add($Radio6)
}

    $Radio7 = New-Object System.Windows.Forms.RadioButton
    $Radio7.Location = New-Object System.Drawing.Point(8,8)
    $Radio7.Size = New-Object System.Drawing.Size(140,27)
    $Radio7.BackColor = [System.Drawing.Color]::’Transparent’
    $Radio7.Checked = $True
    $Radio7.Text = "WINDOWS 7"
    $Radio7.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio7.Name = "Radio7"
    #$Radio7.TabIndex = 6
    #$groupbox1.Controls.Add($Radio7)

    $Radio8 = New-Object System.Windows.Forms.RadioButton
    $Radio8.BackColor = [System.Drawing.Color]::’Transparent’
    $Radio8.ForeColor = [System.Drawing.Color]::'Red'
    $Radio8.Location = New-Object System.Drawing.Point(8,28)
    $Radio8.Size = New-Object System.Drawing.Size(170,27)

    #$Radio8.TabIndex = 10
    $Radio8.Text = "WINDOWS 7 UPGRADE"

    $Radio8.DataBindings.DefaultDataSourceUpdateMode = 0
$Radio8.Name = "Radio8"
#$Radio8.TabIndex = 7
    #$groupbox1.Controls.Add($Radio8)


$InitialFormWindowState = $Form.WindowState
$Form.add_Load($OnLoadForm_StateCorrection)

$LANDESK = [system.drawing.image]::FromFile("x:\Scripts\Powershell\logo-lg.gif")
$PS = [system.drawing.image]::FromFile("x:\Scripts\Powershell\PS2.png")
$PictureBox = New-Object Windows.Forms.PictureBox
$PictureBox.Image = $LANDESK
$PictureBox.Location = new-object System.Drawing.Size(12, 372)
$PictureBox.Width = $LANDESK.Size.Width
$PictureBox.Height = $LANDESK.Size.Height
$Form.Controls.Add($PictureBox)

$PictureBox2 = New-Object Windows.Forms.PictureBox
$PictureBox2.Image = $PS
$PictureBox2.Location = new-object System.Drawing.Size(588, 365)
$Form.Controls.Add($PictureBox2)

$Form.BackColor = 'White'
$Form.width = 698
$Form.height = 465

$Form.Text = "PowerShell Provisioning Tool                                                                                                                                 Gary Stewart 2014"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.ControlBox = $False
$Form.topmost = $true

$MainLabel = new-object System.Windows.Forms.Label
$MainLabel.BackColor = [System.Drawing.Color]::’Transparent’
$MainLabel.Location = new-object System.Drawing.Size(10,5)
$MainLabel.size = new-object System.Drawing.Size(500,20)
$MainLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 14.25, 0, 3, 0)
$MainLabel.ForeColor = 'DimGray'
$MainLabel.Text = "PLEASE CUSTOMISE YOUR WINDOWS INSTALL"
$Form.Controls.Add($MainLabel)

$MainLabelWarning = new-object System.Windows.Forms.Label
$MainLabelWarning.BackColor = [System.Drawing.Color]::’Transparent’
$MainLabelWarning.Location = new-object System.Drawing.Size(10,352)
$MainLabelWarning.size = new-object System.Drawing.Size(450,20)
$MainLabelWarning.ForeColor = [System.Drawing.Color]::'Red'
$MainLabelWarning.Font = New-Object System.Drawing.Font(“Tahoma”,9.25, 0,3,0)
$MainLabelWarning.Text = "PLEASE ENSURE ALL OPTIONS ARE SELECTED BEFORE CLICKING SUBMIT"
$Form.Controls.Add($MainLabelWarning)

$BuildTechDrop = New-Object System.Windows.Forms.ComboBox
$BuildTechDrop.Location = New-Object System.Drawing.Point(110,45)
$BuildTechDrop.Size = New-Object System.Drawing.Size(198,21)
$BuildTechDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$BuildTechDrop.tabIndex = 0
$Form.Controls.Add($BuildTechDrop)

$BuildTechDropLabel = new-object System.Windows.Forms.Label
$BuildTechDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$BuildTechDropLabel.Location = new-object System.Drawing.Size(12,47)
$BuildTechDropLabel.size = new-object System.Drawing.Size(90,20)
$BuildTechDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$BuildTechDropLabel.Text = "Build Tech"
$Form.Controls.Add($BuildTechDropLabel)

$AcroDrop = New-Object System.Windows.Forms.ComboBox
$AcroDrop.Location = New-Object System.Drawing.Point(110,75)
$AcroDrop.Size = New-Object System.Drawing.Size(198,21)
$AcroDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$AcroDrop.tabIndex = 1
$Form.Controls.Add($AcroDrop)

$AcroDropLabel = new-object System.Windows.Forms.Label
$AcroDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$AcroDropLabel.Location = new-object System.Drawing.Size(12,75)
$AcroDropLabel.size = new-object System.Drawing.Size(95,20)
$AcroDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$AcroDropLabel.Text = "Adobe Acrobat"
$Form.Controls.Add($AcroDropLabel)

$PartitionDrop = New-Object System.Windows.Forms.ComboBox
$PartitionDrop.Location = New-Object System.Drawing.Point(495,45)
$PartitionDrop.Size = New-Object System.Drawing.Size(166,21)
$PartitionDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$PartitionDrop.tabIndex = 2
$Form.Controls.Add($PartitionDrop)

$PartitionDropLabel = new-object System.Windows.Forms.Label
$PartitionDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$PartitionDropLabel.Location = new-object System.Drawing.Size(375,47)
$PartitionDropLabel.size = new-object System.Drawing.Size(90,20)
$PartitionDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$PartitionDropLabel.Text = "Partitioning"
$Form.Controls.Add($PartitionDropLabel)

$MSOffVerDrop = New-Object System.Windows.Forms.ComboBox
$MSOffVerDrop.Location = New-Object System.Drawing.Point(495,75)
$MSOffVerDrop.Size = New-Object System.Drawing.Size(166,21)
$MSOffVerDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$MSOffVerDrop.tabIndex = 3
$Form.Controls.Add($MSOffVerDrop)

$MSOffVerDropLabel = new-object System.Windows.Forms.Label
$MSOffVerDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$MSOffVerDropLabel.Location = new-object System.Drawing.Size(375,75)
$MSOffVerDropLabel.size = new-object System.Drawing.Size(120,20)
$MSOffVerDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$MSOffVerDropLabel.Text = "MS Office Version"
$Form.Controls.Add($MSOffVerDropLabel)

$DepCodeDrop = New-Object System.Windows.Forms.ComboBox
$DepCodeDrop.Location = New-Object System.Drawing.Point(110,105)
$DepCodeDrop.Size = New-Object System.Drawing.Size(551,21)
$DepCodeDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$DepCodeDrop.tabIndex = 4
$Form.Controls.Add($DepCodeDrop)

$DepCodeDropLabel = new-object System.Windows.Forms.Label
$DepCodeDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$DepCodeDropLabel.Location = new-object System.Drawing.Size(12,107)
$DepCodeDropLabel.size = new-object System.Drawing.Size(80,20)
$DepCodeDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$DepCodeDropLabel.Text = "Dep Code"
$Form.Controls.Add($DepCodeDropLabel)

$BarcodeInputLabel = new-object System.Windows.Forms.Label
$BarcodeInputLabel.BackColor = [System.Drawing.Color]::’Transparent’
$BarcodeInputLabel.Location = new-object System.Drawing.Size(12,135)
$BarcodeInputLabel.size = new-object System.Drawing.Size(95,21)
$BarcodeInputLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$BarcodeInputLabel.Text = "Insert Barcode"
$Form.Controls.Add($BarcodeInputLabel)

$BarcodeInput = New-Object System.Windows.Forms.TextBox 
$BarcodeInput.Location = New-Object System.Drawing.Size(110,135) 
$BarcodeInput.Size = New-Object System.Drawing.Size(198,21) 
$BarcodeInput.tabIndex = 5
$Form.Controls.Add($BarcodeInput)

$UpgradeOSCheck = new-object System.Windows.Forms.CheckBox
$UpgradeOSCheck.Location = New-Object System.Drawing.Point(15,160)
$UpgradeOSCheck.Size = New-Object System.Drawing.Size(286, 24)
$UpgradeOSCheck.Text = "     WINDOWS 7 UPGRADE"
$UpgradeOSCheck.tabIndex = 6
$Form.Controls.Add($UpgradeOSCheck)

$App1Box = New-Object System.Windows.Forms.CheckBox
$App1Box.BackColor = [System.Drawing.Color]::’Transparent’
$App1Box.Location = New-Object System.Drawing.Size(15,180)
$App1Box.Size = New-Object System.Drawing.Size(250,24)
$App1Box.Text = "     INSTALL CISCO ANYCONNECT CLIENT"
$App1Box.TabIndex = 7
$Form.Controls.Add($App1Box)

$App2Box = New-Object System.Windows.Forms.CheckBox
$App2Box.BackColor = [System.Drawing.Color]::’Transparent’
$App2Box.Location = New-Object System.Drawing.Size(15,200)
$App2Box.Size = New-Object System.Drawing.Size(250,24)
$App2Box.Text = "     INSTALL AVAMAR CLIENT"
$App2Box.TabIndex = 7
$Form.Controls.Add($App2Box)

$FormatLabel = new-object System.Windows.Forms.Label
$FormatLabel.BackColor = [System.Drawing.Color]::’Transparent’
$FormatLabel.Location = new-object System.Drawing.Size(12,230)
$FormatLabel.size = new-object System.Drawing.Size(340,20)
$FormatLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 10.75, 0, 3, 0)
$FormatLabel.ForeColor = 'DimGray'
$FormatLabel.Text = "PLEASE SELECT DISK TO INSTALL OS ONTO"
$Form.Controls.Add($FormatLabel)

$WarningLabel = new-object System.Windows.Forms.Label
$WarningLabel.BackColor = [System.Drawing.Color]::’Transparent’
$WarningLabel.Location = new-object System.Drawing.Size(10,410)
$WarningLabel.size = new-object System.Drawing.Size(450,10)
$WarningLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 7, 0, 3, 0)
$WarningLabel.ForeColor = 'SteelBlue'
$WarningLabel.Text = "This tool is provided as is. It is not affiliated with LANDESK in any way. Thank you! "
$Form.Controls.Add($WarningLabel)


ForEach ($Item in $DepCodeDropArray) {
    $DepCodeDrop.Items.Add($Item)
}

ForEach ($Item in $MSOffVerDropArray) {
    $MSOffVerDrop.Items.Add($Item)
}

ForEach ($Item in $PartitionDropArray) {
    $PartitionDrop.Items.Add($Item)
}
ForEach ($Item in $AcroDropArray) {
    $AcroDrop.Items.Add($Item)
}
ForEach ($Item in $BuildTechDropArray) {
    $BuildTechDrop.Items.Add($Item)
}

$button = New-Object Windows.Forms.Button
$button.text = "SUBMIT"
$button.Location = new-object System.Drawing.Size(12, 423)
$button.size = new-object System.Drawing.Size(245, 37)
$button.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 12.75, 0, 3, 0)
$button.BackColor = 'SteelBlue'
$button.ForeColor = "White"
$button.tabIndex = 14



    $DepCodeDrop_SelectedIndexChanged=
{

    $DepCodeChoice = $DepCodeDrop.SelectedItem.ToString()
    $DepCodeChoice
    Write-Host $DepCodeChoice
    [System.Windows.Forms.MessageBox]::Show("STARTING SCRIPTBLOCK")

    #Start-Job -ScriptBlock { CheckValues } -Args InitializationScript $CheckValues
    Start-Job -ScriptBlock $CheckValues

}
    $MSOffVerDrop_SelectedIndexChanged=
{

    $MSOffVerChoice = $MSOffVerDrop.SelectedItem.ToString()
    $MSOffVerChoice
    #$MSOffVerChoice = $True
    Write-Host $MSOffVerChoice

    }
    $PartitionDrop_SelectedIndexChanged=
    {

    $PartitionChoice = $PartitionDrop.SelectedItem.ToString()
    $PartitionChoice
    #$PartitionChoice = $True
    Write-Host $PartitionChoice
    }
    $AcroDrop_SelectedIndexChanged=
{


    $AcroChoice = $AcroDrop.SelectedItem.ToString()
    $AcroChoice
    #$AcroChoice = $True
    Write-Host $AcroChoice

}
    $BuildTechDrop_SelectedIndexChanged=
{
    $BuildTechChoice = $BuildTechDrop.SelectedItem.ToString()
    $BuildTechChoice
    #$BuildTechChoice = $True
    Write-Host $BuildTechChoice
}

    $DepCodeDrop.add_SelectedIndexChanged($DepCodeDrop_SelectedIndexChanged)
    $MSOffVerDrop.add_SelectedIndexChanged($MSOffVerDrop_SelectedIndexChanged)
    $PartitionDrop.add_SelectedIndexChanged($PartitionDrop_SelectedIndexChanged)
    $AcroDrop.add_SelectedIndexChanged($AcroDrop_SelectedIndexChanged)
    $BuildTechDrop.add_SelectedIndexChanged($BuildTechDrop_SelectedIndexChanged)





$CheckValues = {
        function CheckValues
    {
        [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
        [System.Windows.Forms.MessageBox]::Show("RUNNING SCRIPTBLOCK")
        $button.Visible = $true
        $RequiredValues = @($BuildTechChoice, $AcroChoice, $MSOffVerChoice, $DepCodeChoice, $PartitionChoice)
        Write-Host $RequiredValues

        while ($RequiredValues -lt 4) { }

        $Form.controls.add($button)
        $Form.height = 500
        $Form.Width = 695

    }


}

$button.add_click({ $Form.Close()
})

[void]$Form.showdialog()
我想在scriptblock看到数组中的5个值时显示该按钮

我仍在测试while循环是否有效,scriptblock是否能够看到阵列

即使我在循环之前添加add按钮,我也不会在表单上获得该按钮

如何使用scriptblock将按钮添加到主窗体?我需要将什么传递到scriptblock才能实现这一点


谢谢

好的,在调用表单打开之前,我删除了所有后台作业内容,并确保在表单中添加了按钮(但不可见)。我所做的是创建一个计时器事件,它将跟踪每个选项的更改(每个选项=$True),然后当所有选项都为True时,它将显示按钮。计时器处于1秒刻度(1000ms),因此按钮显示可能会有轻微延迟。我还消除了每次向组合框添加项目时出现的恼人数字

以下是完整的代码:

$BuildTech = "x:\Scripts\PowerShell\BuildTech.txt"
$Acrobat = "x:\Scripts\PowerShell\Acrobat.txt"
$Partition = "x:\Scripts\Powershell\Drives.txt"
$MSOffver = "x:\Scripts\Powershell\OfficeVer.txt"
$DepCode = "x:\Scripts\Powershell\DepCodes.txt"

$CompName = $Null

[array]$BuildTechDropArray = Get-Content $BuildTech
[array]$AcroDropArray = Get-Content $Acrobat
[array]$PartitionDropArray = Get-Content $Partition
[array]$MSOffVerDropArray = Get-Content $MSOffver
[array]$DepCodeDropArray = Get-Content $DepCode

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

[System.Windows.Forms.Application]::EnableVisualStyles();

$Form = New-Object System.Windows.Forms.Form

$Radio1 = New-Object System.Windows.Forms.RadioButton
$Radio2 = New-Object System.Windows.Forms.RadioButton
$Radio3 = New-Object System.Windows.Forms.RadioButton
$Radio4 = New-Object System.Windows.Forms.RadioButton
$Radio5 = New-Object System.Windows.Forms.RadioButton
$Radio6 = New-Object System.Windows.Forms.RadioButton

$Radio1.Checked = $True

Function Get-OptimalSize()
{
    Param ([int64]$sizeInBytes)
    switch ($sizeInBytes)
    {
        { $sizeInBytes -ge 1TB } { "{0:n$sigDigits}" -f ($sizeInBytes/1TB) + " TB"; break }
        { $sizeInBytes -ge 1GB } { "{0:n$sigDigits}" -f ($sizeInBytes/1GB) + " GB"; break }
        { $sizeInBytes -ge 1MB } { "{0:n$sigDigits}" -f ($sizeInBytes/1MB) + " MB"; break }
        { $sizeInBytes -ge 1KB } { "{0:n$sigDigits}" -f ($sizeInBytes/1KB) + " KB"; break }
        Default { "{0:n$sigDigits}" -f $sizeInBytes + " Bytes" }
    }
}

$diskdrive = gwmi win32_diskdrive
$a =
foreach ($drive in $diskdrive)
{

    $InterFaceType = "$($Drive.InterfaceType)"
    If (!($InterFaceType -eq "USB"))
    {
        $Size = "$($drive.size)"
        $DriveModel = "`nDrive: $($drive.deviceid.substring(4))  Model: $($drive.model)"
        $Size = Get-OptimalSize $Size
        $DriveModel + "     " + $Size
        [Array]$a = $a + $Result
    }
}

$a = $a | Sort-Object

$Drive1 = $a[0] 
$Drive2 = $a[1]
$Drive3 = $a[2]
$Drive4 = $a[3]
$Drive5 = $a[4]
$Drive6 = $a[5]

If (-not ($Drive1 -eq $null)) { $Drive1 = $Drive1.trim() }
If (-not ($Drive2 -eq $null)) { $Drive2 = $Drive2.trim() }
If (-not ($Drive3 -eq $null)) { $Drive3 = $Drive3.trim() }
If (-not ($Drive3 -eq $null)) { $Drive4 = $Drive4.trim() }
If (-not ($Drive3 -eq $null)) { $Drive5 = $Drive5.trim() }
If (-not ($Drive3 -eq $null)) { $Drive6 = $Drive6.trim() }

If($a[0]){
    $DiskID1 = $a[0].SubString(21,1)
    $Radio1.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15    
    $Radio1.Size = $System_Drawing_Size
    $Radio1.TabIndex = 8
    $Radio1.Text = $Drive1
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 255
    $Radio1.Location = $System_Drawing_Point
    $Radio1.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio1.Name = "Radio1"
    $Form.Controls.Add($Radio1)

}
If($a[1]){
    $DiskID2 = $a[1].SubString(21,1)
    $Radio2.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio2.Size = $System_Drawing_Size
    $Radio2.TabIndex = 9
    $Radio2.Text = $Drive2
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 275
    $Radio2.Location = $System_Drawing_Point
    $Radio2.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio2.Name = "Radio2"
    $Form.Controls.Add($Radio2)
}
If($a[2]){
    $DiskID3 = $a[2].SubString(21,1)
    $Radio3.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio3.Size = $System_Drawing_Size
    $Radio3.TabIndex = 10
    $Radio3.Text = $Drive3
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 295
    $Radio3.Location = $System_Drawing_Point
    $Radio3.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio3.Name = "Radio3"
    $Form.Controls.Add($Radio3)
}
If($a[3]){
    $DiskID4 = $a[3].SubString(21,1)
    $Radio4.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio4.Size = $System_Drawing_Size
    $Radio4.TabIndex = 11
    $Radio4.Text = $Drive4
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 315
    $Radio4.Location = $System_Drawing_Point
    $Radio4.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio4.Name = "Radio4"
    $Form.Controls.Add($Radio4)
}
If($a[4]){  
    $DiskID5 = $a[4].SubString(21,1)
    $Radio5.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio5.Size = $System_Drawing_Size
    $Radio5.TabIndex = 12
    $Radio5.Text = $Drive5
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 335
    $Radio5.Location = $System_Drawing_Point
    $Radio5.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio5.Name = "Radio5"
    $Form.Controls.Add($Radio5)
}
If($a[5]){
    $DiskID6 = $a[5].SubString(21,1)
    $Radio6.BackColor = [System.Drawing.Color]::’Transparent’
    $System_Drawing_Size = New-Object System.Drawing.Size
    $System_Drawing_Size.Width = 500
    $System_Drawing_Size.Height = 15
    $Radio6.Size = $System_Drawing_Size
    $Radio6.TabIndex = 13
    $Radio6.Text = $Drive6
    $System_Drawing_Point = New-Object System.Drawing.Point
    $System_Drawing_Point.X = 15
    $System_Drawing_Point.Y = 355
    $Radio6.Location = $System_Drawing_Point
    $Radio6.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio6.Name = "Radio6"
    $Form.Controls.Add($Radio6)
}

    $Radio7 = New-Object System.Windows.Forms.RadioButton
    $Radio7.Location = New-Object System.Drawing.Point(8,8)
    $Radio7.Size = New-Object System.Drawing.Size(140,27)
    $Radio7.BackColor = [System.Drawing.Color]::’Transparent’
    $Radio7.Checked = $True
    $Radio7.Text = "WINDOWS 7"
    $Radio7.DataBindings.DefaultDataSourceUpdateMode = 0
    $Radio7.Name = "Radio7"
    #$Radio7.TabIndex = 6
    #$groupbox1.Controls.Add($Radio7)

    $Radio8 = New-Object System.Windows.Forms.RadioButton
    $Radio8.BackColor = [System.Drawing.Color]::’Transparent’
    $Radio8.ForeColor = [System.Drawing.Color]::'Red'
    $Radio8.Location = New-Object System.Drawing.Point(8,28)
    $Radio8.Size = New-Object System.Drawing.Size(170,27)

    #$Radio8.TabIndex = 10
    $Radio8.Text = "WINDOWS 7 UPGRADE"

    $Radio8.DataBindings.DefaultDataSourceUpdateMode = 0
$Radio8.Name = "Radio8"
#$Radio8.TabIndex = 7
    #$groupbox1.Controls.Add($Radio8)


$InitialFormWindowState = $Form.WindowState
$Form.add_Load($OnLoadForm_StateCorrection)

$LANDESK = [system.drawing.image]::FromFile("x:\Scripts\Powershell\logo-lg.gif")
$PS = [system.drawing.image]::FromFile("x:\Scripts\Powershell\PS2.png")
$PictureBox = New-Object Windows.Forms.PictureBox
$PictureBox.Image = $LANDESK
$PictureBox.Location = new-object System.Drawing.Size(12, 372)
$PictureBox.Width = $LANDESK.Size.Width
$PictureBox.Height = $LANDESK.Size.Height
$Form.Controls.Add($PictureBox)

$PictureBox2 = New-Object Windows.Forms.PictureBox
$PictureBox2.Image = $PS
$PictureBox2.Location = new-object System.Drawing.Size(588, 365)
$Form.Controls.Add($PictureBox2)

$Form.BackColor = 'White'
$Form.width = 695
$Form.height = 500

$Form.Text = "PowerShell Provisioning Tool                                                                                                                                 Gary Stewart 2014"
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.ControlBox = $False
$Form.topmost = $true

$MainLabel = new-object System.Windows.Forms.Label
$MainLabel.BackColor = [System.Drawing.Color]::’Transparent’
$MainLabel.Location = new-object System.Drawing.Size(10,5)
$MainLabel.size = new-object System.Drawing.Size(500,20)
$MainLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 14.25, 0, 3, 0)
$MainLabel.ForeColor = 'DimGray'
$MainLabel.Text = "PLEASE CUSTOMISE YOUR WINDOWS INSTALL"
$Form.Controls.Add($MainLabel)

$MainLabelWarning = new-object System.Windows.Forms.Label
$MainLabelWarning.BackColor = [System.Drawing.Color]::’Transparent’
$MainLabelWarning.Location = new-object System.Drawing.Size(10,352)
$MainLabelWarning.size = new-object System.Drawing.Size(450,20)
$MainLabelWarning.ForeColor = [System.Drawing.Color]::'Red'
$MainLabelWarning.Font = New-Object System.Drawing.Font(“Tahoma”,9.25, 0,3,0)
$MainLabelWarning.Text = "PLEASE ENSURE ALL OPTIONS ARE SELECTED BEFORE CLICKING SUBMIT"
$Form.Controls.Add($MainLabelWarning)

$BuildTechDrop = New-Object System.Windows.Forms.ComboBox
$BuildTechDrop.Location = New-Object System.Drawing.Point(110,45)
$BuildTechDrop.Size = New-Object System.Drawing.Size(198,21)
$BuildTechDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$BuildTechDrop.tabIndex = 0
$Form.Controls.Add($BuildTechDrop)

$BuildTechDropLabel = new-object System.Windows.Forms.Label
$BuildTechDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$BuildTechDropLabel.Location = new-object System.Drawing.Size(12,47)
$BuildTechDropLabel.size = new-object System.Drawing.Size(90,20)
$BuildTechDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$BuildTechDropLabel.Text = "Build Tech"
$Form.Controls.Add($BuildTechDropLabel)

$AcroDrop = New-Object System.Windows.Forms.ComboBox
$AcroDrop.Location = New-Object System.Drawing.Point(110,75)
$AcroDrop.Size = New-Object System.Drawing.Size(198,21)
$AcroDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$AcroDrop.tabIndex = 1
$Form.Controls.Add($AcroDrop)

$AcroDropLabel = new-object System.Windows.Forms.Label
$AcroDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$AcroDropLabel.Location = new-object System.Drawing.Size(12,75)
$AcroDropLabel.size = new-object System.Drawing.Size(95,20)
$AcroDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$AcroDropLabel.Text = "Adobe Acrobat"
$Form.Controls.Add($AcroDropLabel)

$PartitionDrop = New-Object System.Windows.Forms.ComboBox
$PartitionDrop.Location = New-Object System.Drawing.Point(495,45)
$PartitionDrop.Size = New-Object System.Drawing.Size(166,21)
$PartitionDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$PartitionDrop.tabIndex = 2
$Form.Controls.Add($PartitionDrop)

$PartitionDropLabel = new-object System.Windows.Forms.Label
$PartitionDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$PartitionDropLabel.Location = new-object System.Drawing.Size(375,47)
$PartitionDropLabel.size = new-object System.Drawing.Size(90,20)
$PartitionDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$PartitionDropLabel.Text = "Partitioning"
$Form.Controls.Add($PartitionDropLabel)

$MSOffVerDrop = New-Object System.Windows.Forms.ComboBox
$MSOffVerDrop.Location = New-Object System.Drawing.Point(495,75)
$MSOffVerDrop.Size = New-Object System.Drawing.Size(166,21)
$MSOffVerDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$MSOffVerDrop.tabIndex = 3
$Form.Controls.Add($MSOffVerDrop)

$MSOffVerDropLabel = new-object System.Windows.Forms.Label
$MSOffVerDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$MSOffVerDropLabel.Location = new-object System.Drawing.Size(375,75)
$MSOffVerDropLabel.size = new-object System.Drawing.Size(120,20)
$MSOffVerDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$MSOffVerDropLabel.Text = "MS Office Version"
$Form.Controls.Add($MSOffVerDropLabel)

$DepCodeDrop = New-Object System.Windows.Forms.ComboBox
$DepCodeDrop.Location = New-Object System.Drawing.Point(110,105)
$DepCodeDrop.Size = New-Object System.Drawing.Size(551,21)
$DepCodeDrop.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
$DepCodeDrop.tabIndex = 4
$Form.Controls.Add($DepCodeDrop)

$DepCodeDropLabel = new-object System.Windows.Forms.Label
$DepCodeDropLabel.BackColor = [System.Drawing.Color]::’Transparent’
$DepCodeDropLabel.Location = new-object System.Drawing.Size(12,107)
$DepCodeDropLabel.size = new-object System.Drawing.Size(80,20)
$DepCodeDropLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$DepCodeDropLabel.Text = "Dep Code"
$Form.Controls.Add($DepCodeDropLabel)

$BarcodeInputLabel = new-object System.Windows.Forms.Label
$BarcodeInputLabel.BackColor = [System.Drawing.Color]::’Transparent’
$BarcodeInputLabel.Location = new-object System.Drawing.Size(12,135)
$BarcodeInputLabel.size = new-object System.Drawing.Size(95,21)
$BarcodeInputLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”,9.75, 0,3,0)
$BarcodeInputLabel.Text = "Insert Barcode"
$Form.Controls.Add($BarcodeInputLabel)

$BarcodeInput = New-Object System.Windows.Forms.TextBox 
$BarcodeInput.Location = New-Object System.Drawing.Size(110,135) 
$BarcodeInput.Size = New-Object System.Drawing.Size(198,21) 
$BarcodeInput.tabIndex = 5
$Form.Controls.Add($BarcodeInput)

$UpgradeOSCheck = new-object System.Windows.Forms.CheckBox
$UpgradeOSCheck.Location = New-Object System.Drawing.Point(15,160)
$UpgradeOSCheck.Size = New-Object System.Drawing.Size(286, 24)
$UpgradeOSCheck.Text = "     WINDOWS 7 UPGRADE"
$UpgradeOSCheck.tabIndex = 6
$Form.Controls.Add($UpgradeOSCheck)

$App1Box = New-Object System.Windows.Forms.CheckBox
$App1Box.BackColor = [System.Drawing.Color]::’Transparent’
$App1Box.Location = New-Object System.Drawing.Size(15,180)
$App1Box.Size = New-Object System.Drawing.Size(250,24)
$App1Box.Text = "     INSTALL CISCO ANYCONNECT CLIENT"
$App1Box.TabIndex = 7
$Form.Controls.Add($App1Box)

$App2Box = New-Object System.Windows.Forms.CheckBox
$App2Box.BackColor = [System.Drawing.Color]::’Transparent’
$App2Box.Location = New-Object System.Drawing.Size(15,200)
$App2Box.Size = New-Object System.Drawing.Size(250,24)
$App2Box.Text = "     INSTALL AVAMAR CLIENT"
$App2Box.TabIndex = 7
$Form.Controls.Add($App2Box)

$FormatLabel = new-object System.Windows.Forms.Label
$FormatLabel.BackColor = [System.Drawing.Color]::’Transparent’
$FormatLabel.Location = new-object System.Drawing.Size(12,230)
$FormatLabel.size = new-object System.Drawing.Size(340,20)
$FormatLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 10.75, 0, 3, 0)
$FormatLabel.ForeColor = 'DimGray'
$FormatLabel.Text = "PLEASE SELECT DISK TO INSTALL OS ONTO"
$Form.Controls.Add($FormatLabel)

$WarningLabel = new-object System.Windows.Forms.Label
$WarningLabel.BackColor = [System.Drawing.Color]::’Transparent’
$WarningLabel.Location = new-object System.Drawing.Size(10,410)
$WarningLabel.size = new-object System.Drawing.Size(450,10)
$WarningLabel.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 7, 0, 3, 0)
$WarningLabel.ForeColor = 'SteelBlue'
$WarningLabel.Text = "This tool is provided as is. It is not affiliated with LANDESK in any way. Thank you! "
$Form.Controls.Add($WarningLabel)


ForEach ($Item in $DepCodeDropArray) {
    [void]$DepCodeDrop.Items.Add($Item)
}

ForEach ($Item in $MSOffVerDropArray) {
    [void]$MSOffVerDrop.Items.Add($Item)
}

ForEach ($Item in $PartitionDropArray) {
    [void]$PartitionDrop.Items.Add($Item)
}
ForEach ($Item in $AcroDropArray) {
    [void]$AcroDrop.Items.Add($Item)
}
ForEach ($Item in $BuildTechDropArray) {
    [void]$BuildTechDrop.Items.Add($Item)
}

$Script:button = New-Object Windows.Forms.Button
$button.text = "SUBMIT"
$button.Location = new-object System.Drawing.Size(12, 423)
$button.size = new-object System.Drawing.Size(245, 37)
$button.Font = New-Object System.Drawing.Font(“Tahoma Bold”, 12.75, 0, 3, 0)
$button.BackColor = 'SteelBlue'
$button.ForeColor = "White"
$button.tabIndex = 14
$button.Visible = $False
$Form.controls.add($button)



    $DepCodeDrop_SelectedIndexChanged=
{
    $Script:DepCodeChoice = $True
    Write-Host $DepCodeChoice
}
    $MSOffVerDrop_SelectedIndexChanged=
{

    $Script:MSOffVerChoice = $True
    Write-Host $MSOffVerChoice

    }
    $PartitionDrop_SelectedIndexChanged=
    {

    $Script:PartitionChoice = $True
    Write-Host $PartitionChoice
    }
    $AcroDrop_SelectedIndexChanged=
{
    $Script:AcroChoice = $True
    Write-Host $AcroChoice

}
    $BuildTechDrop_SelectedIndexChanged=
{
    $Script:BuildTechChoice = $True
    Write-Host $BuildTechChoice
}

    $DepCodeDrop.add_SelectedIndexChanged($DepCodeDrop_SelectedIndexChanged)
    $MSOffVerDrop.add_SelectedIndexChanged($MSOffVerDrop_SelectedIndexChanged)
    $PartitionDrop.add_SelectedIndexChanged($PartitionDrop_SelectedIndexChanged)
    $AcroDrop.add_SelectedIndexChanged($AcroDrop_SelectedIndexChanged)
    $BuildTechDrop.add_SelectedIndexChanged($BuildTechDrop_SelectedIndexChanged)

#Create a timer to watch for all selections to be true
$timer = New-Object System.Windows.Forms.Timer $objForm
$timer.Interval = 1000  # once per second
$timer.Add_Tick({
    If ($Script:AcroChoice -AND $Script:DepCodeChoice -AND $Script:PartitionChoice -AND $Script:BuildTechChoice -AND $Script:MSOffVerChoice) {
        $Script:button.Visible=$True
    }
})
$timer.Start()


$button.add_click({ 
    $Form.Close()
    $timer.Stop()
    $timer.Dispose()
    Remove-Variable AcroChoice,DepCodeChoice,PartitionChoice,BuildTechChoice,MSOffVerChoice,button -Scope Script
})
[void]$Form.ShowDialog()

我对表单不是很在行,但我很确定您需要事先添加按钮,并设置按钮的.Visible=$false,然后你把脚本改成$true试试看,我就可以添加按钮了,但是脚本块中的$button.visible=$true不能使按钮可见,所以我认为这是同一个问题,脚本块不能修改表单?如果我在主表单脚本中运行循环,那么表单将挂起并且无法修改,因此会出现scriptblock。不像我最初想的那么简单!啊,不,它不能。你会希望函数在调用时返回一些表单可以看到的内容,并在需要时使按钮可见。我知道你只提供了部分代码,但你能显示所有代码(在这里或其他地方),以便我们可以查看它并找到一个好的解决方案吗?虽然很长,但是的,我会尝试一下,谢谢,这就像做梦一样。。。非常感谢你!以及快速反应。