在GUI窗体上获取Get-WmiObject -Class Win32_Product输出

时间:2017-01-19 06:22:58

标签: powershell powershell-v2.0 powershell-v3.0 powershell-v4.0

您好我是PowerShell的新手,我在GUI窗体上查看Get-WmiObject -Class Win32_Product输出。 提前致谢。

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

标签:卸载 功能:卸载

Start-Process Powershell -verb runas #Load Windows Forms assembly [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 = 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 ="卸载" $ form.Controls.Add($ button)$ form.Controls.Add($ dataGridView)#选择合适的列$ dataGridView.Columns.Insert(0,(New-Object System.Windows.Forms.DataGridViewButtonCell))$ dataGridView.ColumnCount = 8 $ dataGridView.ColumnHeadersVisible = $ true $ dataGridView.Columns [0] .Name ="卸载" $ dataGridView.Columns [1] .Name ="描述" $ dataGridView.Columns [2] .Name =" IdentifyingNumber" $ dataGridView.Columns [3] .Name =" Name" $ dataGridView.Columns [4] .Name ="供应商" $ dataGridView.Columns [5] .Name =" Version" $ dataGridView.Columns [6] .Name =" Caption" $ dataGridView.Columns [7] .Name =" InstallLocation" $ dataGridView.Columns [0] .width = 40 $ dataGridView.Columns [1] .width = 200#获取项目列表<#Get-WmiObject -Class Win32_Product | foreach {$ dataGridView.Rows.Add($ .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-Host $ dataGridView.Rows [ $ rowIndex] .Cells [$ columnIndex0] .value Write-Host $ dataGridView.Rows [$ rowIndex] .Cells [$ columnIndex1] .value Write-Host $ dataGridView.Rows [$ rowIndex] .Cells [$ columnIndex5] .value# $ IdentifyNumber = $ dataGridView.Rows [$ rowIndex] .Cells [$ ClassKey] .value#$ Name = $ dataGridView.Rows [$ rowIndex] .Cells [$ columnIndex0] .value#$ classKey =' IdentifyingNumber = $ IdentifyingNumber.value,名称= $ Name.value,版本= $ Version.value' #Write-Host $ classKey#([wmi]“\ $ server \ root \ cimv2:Win32_Product。$ classKey”)。uninstall()} $ Uninstall = $ dataGridView.Add_CellMouseClick({gridClick})#显示表单[void] $ form.ShowDialog()

2 个答案:

答案 0 :(得分:3)

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

gwmi -Class win32_product | Out-GridView

并且您还可以获得自定义输出,如XML和CSV以及json和其他形式,并使用特殊软件。

答案 1 :(得分:0)

这可能有点过分,但你总是可以创建一个自定义的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()