【问题标题】:Get Get-WmiObject -Class Win32_Product Output on GUI Form在 GUI 表单上获取 Get-WmiObject -Class Win32_Product 输出
【发布时间】:2017-01-19 06:22:58
【问题描述】:

您好,我是 Powershell 新手。我正在查看 GUI 表单上的 Get-WmiObject -Class Win32_Product 输出。 提前致谢。

以下是代码,我需要为按钮添加标签并为每个按钮分配一个功能。

标签:卸载 功能:卸载 Start-Process Powershell -verb runas # 加载 Windows 窗体程序集 [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [void][System.Windows.Forms.Application]::EnableVisualStyles( ) # 创建一个 GUI $form = New-Object System.Windows.Forms.Form $form.Size = New-Object System.Drawing.Size(920,500) $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]:: Fixed3D $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen $dataGridView = New-Object System.Windows.Forms.DataGridView $dataGridView.Size = New-Object System.Drawing.Size(900,400) $button =新对象 System.Windows.Forms.Button $button.Location = 新对象 System.Drawing.Size(400,420) $button.Size = 新对象 System.Drawing.Size(75,25) $button.text = " Uninstall" $form.Controls.Add($button) $form.Controls.Add($dataGridView) # 选择合适的列 $dataGridView.Columns.Insert(0, (New-Object System.Windows.Forms.DataGridViewButtonCell)) $dataGridView .ColumnCount = 8 $dataGr idView.ColumnHeadersVisible = $true $dataGridView.Columns[0].Name = "卸载" $dataGridView.Columns[1].Name = "描述" $dataGridView.Columns[2].Name = "IdentifyingNumber" $dataGridView.Columns[ 3].Name = "名称" $dataGridView.Columns[4].Name = "供应商" $dataGridView.Columns[5].Name = "版本" $dataGridView.Columns[6].Name = "标题" $dataGridView。 Columns[7].Name = "InstallLocation" $dataGridView.Columns[0].width = 40 $dataGridView.Columns[1].width = 200 # 获取项目列表 .Check,$.Description,$.IdentifyingNumber,$.Name,$.Vendor,$.Version,$.Caption,$.InstallLocation) | out-null }#> # 刷新函数 gridClick(){ $rowIndex = $dataGridView.CurrentRow.Index $columnIndex0 = $dataGridView.ColumnIndex+1 $columnIndex1 = $dataGridView.ColumnIndex+2 $columnIndex2 = $dataGridView.ColumnIndex+3 $ columnIndex3 = $dataGridView.ColumnIndex+4 $columnIndex5 = $dataGridView.ColumnIndex+5 #Write-Host $rowIndex #Write-Host $columnIndex0 #Write-Host $dataGridView.Rows[$rowIndex].Cells[0].value Write-主机 $dataGridView.Rows[$rowIndex].Cells[$columnIndex0].value 写入主机 $dataGridView.Rows[$rowIndex].Cells[$columnIndex1].value 写入主机 $dataGridView.Rows[$rowIndex].Cells[ $columnIndex5].value #$IdentifyNumber = $dataGridView.Rows[$rowIndex].Cells[$ClassKey].value #$Name = $dataGridView.Rows[$rowIndex].Cells[$columnIndex0].value #$classKey = ' IdentificationNumber=$IdentifyingNumber.value,Name=$Name.value,Version=$Version.value' #Write-Host $classKey #([wmi]”\$server\root\cimv2:Win32_Product.$classKey”).uninstall( ) } $Uninstall = $dataGridView.Add_CellMouseClick({gridClick} ) # 显示表单 [void]$form.ShowDialog()

【问题讨论】:

  • 哪个 Powershell 版本?标记特定版本意味着需要在该版本上工作的解决方案。由于 PS2/3/4 完全不同,标记所有这些可能无关紧要。
  • 您在寻找什么样的 GUI 表单? Out-GridView 够好吗?
  • 感谢您的回复.. Out-Gridview 对我来说是公平的,但 Matt Szadziul ans 是我正在寻找的。​​span>

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


【解决方案1】:

您可以使用此方法查看 GUI 网格:

gwmi -Class win32_product | Out-GridView

您还可以获得自定义输出,如 XML、CSV 和 json 等格式,并为此使用特殊软件。

【讨论】:

    【解决方案2】:

    这可能有点矫枉过正,但您始终可以创建自定义 GUI 表单并在 gridview 控件中绘制输出,如下所示:

    # Load Windows Forms assembly
    
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
    [void][System.Windows.Forms.Application]::EnableVisualStyles()
    
    # Create a GUI
    
    $form = New-Object System.Windows.Forms.Form
    $form.Size = New-Object System.Drawing.Size(920,500)
    $form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
    $dataGridView = New-Object System.Windows.Forms.DataGridView
    $dataGridView.Size = New-Object System.Drawing.Size(900,400)
    $button = New-Object System.Windows.Forms.Button
    $button.Location = New-Object System.Drawing.Size(400,420)
    $button.Size = New-Object System.Drawing.Size(75,25)
    $button.Text = "Refresh"
    $form.Controls.Add($button)
    $form.Controls.Add($dataGridView)
    
    # Select appropriate columns
    
    $dataGridView.ColumnCount = 7
    $dataGridView.ColumnHeadersVisible = $true
    $dataGridView.Columns[0].Name = "Description"
    $dataGridView.Columns[1].Name = "IdentifyingNumber"
    $dataGridView.Columns[2].Name = "Name"
    $dataGridView.Columns[3].Name = "Vendor"
    $dataGridView.Columns[4].Name = "Version"
    $dataGridView.Columns[5].Name = "Caption"
    $dataGridView.Columns[6].Name = "InstallLocation"
    
    $dataGridView.Columns[0].width = 240
    
    # Get a list of items
    
    Get-WmiObject -Class Win32_Product | foreach {
        $dataGridView.Rows.Add($_.Description,$_.IdentifyingNumber,$_.Name,$_.Vendor,$_.Version,$_.Caption,$_.InstallLocation) | out-null
    }
    
    # Refresh
    
    $button.Add_Click({
    
        $dataGridView.Rows.Clear()
    
        start-sleep -s 1
    
    Get-WmiObject -Class Win32_Product | foreach {
        $dataGridView.Rows.Add($_.Description,$_.IdentifyingNumber,$_.Name,$_.Vendor,$_.Version,$_.Caption,$_.InstallLocation) | out-null
    }
    
    })
    
    # Add a cell click function
    
    function cellClick(){
    $rowIndex = $dataGridView.CurrentRow.Index
    $columnIndex = $dataGridView.CurrentCell.ColumnIndex
    $value = $dataGridView.Rows[$rowIndex].Cells[$columnIndex].value
    write-host $value
    }
    
    $dataGridView.Add_CellMouseClick({cellClick})
    
    # Show the form
    
    [void]$form.ShowDialog()
    

    【讨论】:

    • 很高兴我能帮上忙 :)
    • 嘿,马特,你能帮我从网格中读取选定单元格的各个数据吗?我已经修改了需要选择项目并卸载应用程序的脚本。我坚持阅读所选项目。
    • Hi Deep :) 我添加了一个函数,用于检索当前列和当前行的单元格值。如果您通过 ISE 启动脚本,所选值将被写入控制台(变量值)。希望有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 2022-01-13
    • 1970-01-01
    • 2014-08-22
    • 2013-10-02
    相关资源
    最近更新 更多