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 の存在しないプロパティ「Combobox」に割り当てることはできません。代わりに、次のように実行する必要があります。

# <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 に答える