0

好的,所以我正在尝试为“$Cots”中的每个项目创建一个下拉菜单并将其稍微向下移动。我有它的基础知识,以及它的基本创建,但不知道我会如何使用“controls.add(whatever)”来调用它以及信息如何链接。

我的代码:

$Script = @{}; $COTS = @{}
Function CheckProgram {
    Param ($Program)

    # Create Object
    $OutputObj = New-Object -TypeName PSobject

    $OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Program.Product
    $OutputObj | Add-Member -MemberType NoteProperty -Name LatestVersion -Value $Program.("Latest Version")
    $OutputObj | Add-Member -MemberType NoteProperty -Name SilentInstallSwitches -Value $Program.("Silent Install Switches")
    $OutputObj | Add-Member -MemberType NoteProperty -Name InstallerPath -Value $Program.("Installer Path")
    $OutputObj | Add-Member -MemberType NoteProperty -Name RegistryIdentifier -Value ($Program.Product -replace "[0-9]" -replace "  ","*")

    IF (Test-Path $Program.("Installer Path")) {
        $OutputObj | Add-Member -MemberType NoteProperty -Name InstallerPathValid -Value "Yes"
    } ELSE {
        $OutputObj | Add-Member -MemberType NoteProperty -Name InstallerPathValid -Value "No"
    }
    $OutputObj | Add-Member -MemberType NoteProperty -Name ProductArchitectures -Value $Program.Architecture

    # Populate temporary variable with infomation from registry
    $InstalledProgram = ($COTS.Programs | Where {$_.DisplayName -like $OutputObj.RegistryIdentifier})
    IF ($InstalledProgram) {
        IF ($InstalledProgram.PSPath -like "*Wow6432Node*"){
            $OutputObj | Add-Member -MemberType NoteProperty -Name InstalledArchitecture -Value "x64"
        } ELSE {
            $OutputObj | Add-Member -MemberType NoteProperty -Name InstalledArchitecture -Value "x32"
        }
        $OutputObj | Add-Member -MemberType NoteProperty -Name Installed -Value "Yes"
        $OutputObj | Add-Member -MemberType NoteProperty -Name CurrentVersion -Value $InstalledProgram.DisplayVersion
        $OutputObj | Add-Member -MemberType NoteProperty -Name UninstallString -Value $InstalledProgram.UninstallString
    } ELSE {
        $OutputObj | Add-Member -MemberType NoteProperty -Name Installed -Value "No"
    }

    # Determine what action should be taken
    IF ($OutputObj.installed -eq "Yes") {
        IF ($OutPutObj.CurrentVersion -le $OutPutObj.LatestVersion) {
            $OutputObj | Add-Member -MemberType NoteProperty -Name InstallAction -Value "Update"
            $OutPutObj.InstallOptions = @("Install","Update","Uninstall","Resinstall","None")
        } ELSEIF ($OutputObj.CurrentVersion -ge $OutputObj.LastestVersion) {
            Write-warning "Installed version of {f} greater than expected."
            $OutputObj | Add-Member -MemberType NoteProperty -Name InstallAction -Value "Error"
            $OutPutObj.InstallOptions = @("None")
        } ELSEIF ($OutputObj.CurrentVersion -eq $OutputObj.Product) {
            $OutputObj | Add-Member -MemberType NoteProperty -Name InstallAction -Value "None"
        }
    } ELSE {
        $OutputObj | Add-Member -MemberType NoteProperty -Name InstallAction -Value "Install"
        $OutPutObj.InstallOptions = @("Install")
    }

# <Example code that doesn't work>
    $OutputObj.ComboBox = New-object System.Windows.Forms.ComboBox
    $OutputObj.ComboBox.Name = ""
    $OutputObj.ComboBox.Items.AddRange($OutPutObj.InstallAction)
    $OutputObj.ComboBox.DataBindings.DefaultDataSourceUpdateMode = 0
    $OutputObj.ComboBox.Location = New-Object System.Drawing.Point(200,200)
    $OutputObj.ComboBox.Controls.Add($ComboBox)
# </Example code that doesn't work>
    ## Add to list or update ##
    [Array]$Cots.Software += $OutputObj

}
$COTS.Load = Import-csv "$PSScriptRoot\3rdPartyCots.csv"
$COTS.AllInstalled = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ | Get-ItemProperty
$COTS.AllInstalled += Get-ChildItem HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\ | Get-ItemProperty
IF (Test-path HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\) { $COTS.AllInstalled += Get-ChildItem HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\ | Get-ItemProperty}
$COTS.Programs = ($Cots.AllInstalled | Where {$_.DisplayName -ne $Null -AND $_.SystemComponent -ne "1" -AND $_.ParentKeyName -eq $Null} |Select *).GetEnumerator() | Sort-Object {"$_"}
ForEach ($Cot in $Cots.load) {CheckProgram $Cot}




function MainMenuForm {

#region Import the Assemblies
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null

# Create Forms and Items {
$MainMenu = New-Object System.Windows.Forms.Form
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

#Buttons
$HomeButton = New-Object System.Windows.Forms.Button
$ScriptSettingsButton = New-Object System.Windows.Forms.Button
$COTSButton = New-Object System.Windows.Forms.Button
$MicrosoftButton = New-Object System.Windows.Forms.Button
$ConfigureButton = New-Object System.Windows.Forms.Button
$MiscToolsButton = New-Object System.Windows.Forms.Button

#Misc Items
$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$StatusBar = New-Object System.Windows.Forms.StatusBar

#Tab Control
$TabControl = New-object System.Windows.Forms.TabControl

#Tabs
$HomeTab = New-Object System.Windows.Forms.TabPage
$ScriptSettingsTab = New-Object System.Windows.Forms.TabPage
$COTSTab = New-Object System.Windows.Forms.TabPage
$MicrosoftTab = New-Object System.Windows.Forms.TabPage
$ConfigureTab = New-Object System.Windows.Forms.TabPage
$MiscToolsTab = New-Object System.Windows.Forms.TabPage

$MicrosoftDataGrid = New-Object System.Windows.Forms.DataGridView
#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Unknown
$OnLoad_StateCorrection = { $MainMenu.WindowState = $InitialFormWindowState }
$OnLoad_MicrosoftDataGrid = { LoadMicrosoft }

#Buttons
$HomeButton_OnClick = { $TabControl.SelectTab($HomeTab) }
$ScriptSettingsButton_OnClick = { $TabControl.SelectTab($ScriptSettingsTab) }
$COTSButton_OnClick = {$TabControl.SelectTab($COTSTab)}
$MicrosoftButton_OnClick = {$TabControl.SelectTab($MicrosoftTab)}
$ConfigureButton_OnClick = {$TabControl.SelectTab($ConfigureTab)}
$MiscToolsButton_OnClick = {$TabControl.SelectTab($MiscToolsTab)}

#Load Main Menu
$MainMenu.ClientSize = New-Object System.Drawing.Size(665,474)
$MainMenu.DataBindings.DefaultDataSourceUpdateMode = 0
$MainMenu.Name = "MainMenu"
$MainMenu.Text = "Patching Script"
$MainMenu.TopMost = $True

# == Home Button == #
$HomeButton.Name = "HomeButton"
$HomeButton.Text = "Home"
$HomeButton.TabIndex = 2
$HomeButton.UseVisualStyleBackColor = $True
$HomeButton.add_Click($HomeButton_OnClick)
$HomeButton.DataBindings.DefaultDataSourceUpdateMode = 0
$HomeButton.Location = New-Object System.Drawing.Point(12,69)
$HomeButton.Size = New-Object System.Drawing.Size(100,35)

$MainMenu.Controls.Add($HomeButton)

# == Script Settings Button == #
$ScriptSettingsButton.Name = "ScriptSettingsButton"
$ScriptSettingsButton.Text = "Script Settings"
$ScriptSettingsButton.TabIndex = 3
$ScriptSettingsButton.UseVisualStyleBackColor = $True
$ScriptSettingsButton.add_Click($ScriptSettingsButton_OnClick)
$ScriptSettingsButton.DataBindings.DefaultDataSourceUpdateMode = 0
$ScriptSettingsButton.Size = New-Object System.Drawing.Size(100,35)
$ScriptSettingsButton.Location = New-Object System.Drawing.Point(12,110)

$MainMenu.Controls.Add($ScriptSettingsButton)

# == COTS Button == #
$COTSButton.Name = "COTSButton"
$COTSButton.Text = "C.O.T.S."
$COTSButton.TabIndex = 4
$COTSButton.UseVisualStyleBackColor = $True
$COTSButton.add_Click($COTSButton_OnClick)
$COTSButton.DataBindings.DefaultDataSourceUpdateMode = 0
$COTSButton.Size = New-Object System.Drawing.Size(100,35)
$COTSButton.Location = New-Object System.Drawing.Point(12,151)

$MainMenu.Controls.Add($COTSButton)

# == Microsoft Button == #
$MicrosoftButton.Name = "MicrosoftButton"
$MicrosoftButton.Text = "Microsoft Patches"
$MicrosoftButton.TabIndex = 5
$MicrosoftButton.UseVisualStyleBackColor = $True
$MicrosoftButton.add_Click($MicrosoftButton_OnClick)
$MicrosoftButton.DataBindings.DefaultDataSourceUpdateMode = 0
$MicrosoftButton.Size = New-Object System.Drawing.Size(100,35)
$MicrosoftButton.Location = New-Object System.Drawing.Point(12,192)

$MainMenu.Controls.Add($MicrosoftButton)

# == Configure Button == #
$ConfigureButton.Name = "ConfigureButton"
$ConfigureButton.Text = "Configure Script"
$ConfigureButton.TabIndex = 6
$ConfigureButton.UseVisualStyleBackColor = $True
$ConfigureButton.add_Click($ConfigureButton_OnClick)
$ConfigureButton.DataBindings.DefaultDataSourceUpdateMode = 0
$ConfigureButton.Location = New-Object System.Drawing.Point(12,233)
$ConfigureButton.Size = New-Object System.Drawing.Size(100,35)

$MainMenu.Controls.Add($ConfigureButton)

# == Misc Tools Button == #
$MiscToolsButton.Name = "MiscToolsButton"
$MiscToolsButton.Text = "Misc Tools"
$MiscToolsButton.TabIndex = 7
$MiscToolsButton.UseVisualStyleBackColor = $True
$MiscToolsButton.add_Click($MiscToolsButton_OnClick)
$MiscToolsButton.DataBindings.DefaultDataSourceUpdateMode = 0
$MiscToolsButton.Location = New-Object System.Drawing.Point(12,274)
$MiscToolsButton.Size = New-Object System.Drawing.Size(100,35)

$MainMenu.Controls.Add($MiscToolsButton)

# == Tab Control == #
$TabControl.Name = "TabControl"
$TabControl.TabIndex = 4
$TabControl.SelectedIndex = 0
$TabControl.DataBindings.DefaultDataSourceUpdateMode = 0
$TabControl.Location = New-Object System.Drawing.Point(124,65)
$TabControl.Size = New-Object System.Drawing.Size(525,375)
$TabControl.visible = $True
$MainMenu.Controls.Add($TabControl)

$TabControl.SizeMode = "Fixed"
$TabControl.ItemSize = New-Object System.Drawing.Size(0, 1)
$TabControl.Appearance = "Buttons"

## Possibly Unneeded Info
#$ScriptSettingsTab.Location = New-Object System.Drawing.Point(4,22)
#$ScriptSettingsTab.Size = New-Object System.Drawing.Size(535,374)

# == Home Tab == #
$HomeTab.Name = "HomeTab"
$HomeTab.Text = "Home"
$HomeTab.TabIndex = 1
$HomeTab.UseVisualStyleBackColor = $True
$HomeTab.DataBindings.DefaultDataSourceUpdateMode = 0

$TabControl.Controls.Add($HomeTab)

# == Script Settings Tab == #
$ScriptSettingsTab.Name = "ScriptSettingsTab"
$ScriptSettingsTab.Text = "Home"
$ScriptSettingsTab.TabIndex = 2
$ScriptSettingsTab.UseVisualStyleBackColor = $True
$ScriptSettingsTab.DataBindings.DefaultDataSourceUpdateMode = 0
$ScriptSettingsTab.BackColor = [System.Drawing.Color]::CornflowerBlue

$TabControl.Controls.Add($ScriptSettingsTab)

# == C.O.T.S. Tab == #
$COTSTab.Name = "COTSTab"
$COTSTab.Text = "Home"
$COTSTab.TabIndex = 2
$COTSTab.UseVisualStyleBackColor = $True
$COTSTab.DataBindings.DefaultDataSourceUpdateMode = 0
$COTSTab.BackColor = [System.Drawing.Color]::Blue

$TabControl.Controls.Add($COTSTab)

# == Microsoft Patches Tab == #
$MicrosoftTab.Name = "MicrosoftTab"
$MicrosoftTab.Text = "Home"
$MicrosoftTab.TabIndex = 2
$MicrosoftTab.UseVisualStyleBackColor = $True
$MicrosoftTab.DataBindings.DefaultDataSourceUpdateMode = 0
$MicrosoftTab.BackColor = [System.Drawing.Color]::Green

$TabControl.Controls.Add($MicrosoftTab)
# == Microsoft Data Grid == #
$MicrosoftDataGrid.Name
$MicrosoftDataGrid.Size = New-Object System.Drawing.Size(492,308)
$MicrosoftDataGrid.DataBindings.DefaultDataSourceUpdateMode = 0 
$MicrosoftDataGrid.HeaderForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0) 
$MicrosoftDataGrid.Name = "MicrosoftDataGrid" 
$MicrosoftDataGrid.DataMember = "" 
$MicrosoftDataGrid.TabIndex = 0 
$MicrosoftDataGrid.Location = New-Object System.Drawing.Point(13,48)

$MicrosoftDataGrid.autosize = $true
$MicrosoftTab.Controls.Add($MicrosoftDataGrid)

# == Configure Script Tab == #
$ConfigureTab.Name = "ConfigureTab"
$ConfigureTab.Text = "Home"
$ConfigureTab.TabIndex = 2
$ConfigureTab.UseVisualStyleBackColor = $True
$ConfigureTab.DataBindings.DefaultDataSourceUpdateMode = 0
$ConfigureTab.BackColor = [System.Drawing.Color]::Red

$TabControl.Controls.Add($ConfigureTab)

# == Misc Tools Tab == #
$MiscToolsTab.Name = "ConfigureTab"
$MiscToolsTab.Text = "Home"
$MiscToolsTab.TabIndex = 2
$MiscToolsTab.UseVisualStyleBackColor = $True
$MiscToolsTab.DataBindings.DefaultDataSourceUpdateMode = 0
$MiscToolsTab.BackColor = [System.Drawing.Color]::blue

$TabControl.Controls.Add($MiscToolsTab)

# == Progress Bar == #
$progressBar.Name = "progressBar"
$progressBar.DataBindings.DefaultDataSourceUpdateMode = 0
$progressBar.Location = New-Object System.Drawing.Point(589,458)
$progressBar.Size = New-Object System.Drawing.Size(75,15)
$progressBar.TabIndex = 0

$MainMenu.Controls.Add($progressBar)

# == Status bar == #
$StatusBar.Name = "StatusBar"
$StatusBar.Text = "statusBar1"
$StatusBar.TabIndex = 1
$StatusBar.Size = New-Object System.Drawing.Size(665,18)
$StatusBar.Location = New-Object System.Drawing.Point(0,456)
$StatusBar.DataBindings.DefaultDataSourceUpdateMode = 0
$StatusBar.add_PanelClick($statusBar_PanelClick)

$MainMenu.Controls.Add($StatusBar)

# == Save and Open Form == #
$InitialFormWindowState = $MainMenu.WindowState

$MainMenu.add_Load($OnLoad_StateCorrection)
$MainMenu.add_Load($OnLoad_MicrosoftDataGrid)

$MainMenu.ShowDialog()| Out-Null

} #End Function

MainMenuForm
4

1 回答 1

3

首先,您正在做一些不可能的事情:在第 6 行,您声明

$OutputObj = New-Object -TypeName PSobject

在第 54 行

$OutputObj.ComboBox = New-object System.Windows.Forms.ComboBox

您不能将此新对象分配给对象 $OutputObj 的不存在属性“组合框”。相反,你应该这样做:

# <Example code that does work>
    $ComboBox = New-object System.Windows.Forms.ComboBox
    $ComboBox.Name = ""
    $ComboBox.Items.AddRange($OutPutObj.InstallAction)
    $ComboBox.DataBindings.DefaultDataSourceUpdateMode = 0
    $ComboBox.Location = New-Object System.Drawing.Point(200,200)
    $OutputObj | Add-Member noteproperty "Combobox" $ComboBox
# </Example code that does work>

与 InstallOptions 类似——请参阅您收到的错误消息: Property 'InstallOptions' cannot be found on this object; make sure it exists and is settable. At line:50 char:20

$null实际上,如果您的对象无论如何都必须包含这些属性,为了您的利益,您可以在脚本开头定义所有属性:

# Create Object
$OutputObj = New-Object -TypeName PSobject
$OutputObj | Add-Member -MemberType NoteProperty -Name Name -Value $Program.Product
...
$OutputObj | Add-Member -MemberType NoteProperty -Name RegistryIdentifier -Value ($Program.Product -replace "[0-9]" -replace "  ","*")
$OutputObj | Add-Member NoteProperty "InstallAction" $null
$OutputObj | Add-Member NoteProperty "InstallOptions" $null
$OutputObj | Add-Member NoteProperty "ComboBox" $null
...

然后,如果它们是您的对象的一部分,您可以使用

$OutputObj.ComboBox = $ComboBox
$OutputObj.InstallAction = "Install"
...

这将使您的代码更具可读性,正如您在此改编代码中所见:

# Determine what action should be taken
IF ($OutputObj.installed -eq "Yes") {
    IF ($OutPutObj.CurrentVersion -le $OutPutObj.LatestVersion) {
        $OutputObj.InstallAction = "Update"
        $OutPutObj.InstallOptions = @("Install","Update","Uninstall","Resinstall","None")
    } ELSEIF ($OutputObj.CurrentVersion -ge $OutputObj.LastestVersion) {
        Write-warning "Installed version of {f} greater than expected."
        $OutputObj.InstallAction = "Error"
        $OutPutObj.InstallOptions = @("None")
    } ELSEIF ($OutputObj.CurrentVersion -eq $OutputObj.Product) {
        $OutputObj.InstallAction = "None"
    }
} ELSE {
    $OutputObj.InstallAction = "Install"
    $OutPutObj.InstallOptions = @("Install")
}

一旦你的代码被清理并且不会产生错误,你的问题的答案就很简单了。您可以将组合框添加到任何其他表单控件中,就像您为其余部分所做的那样,样式为

$COTSTab.Controls.Add($YourCustomObject.ComboBox)
于 2013-03-29T12:40:47.517 回答