提问者:小点点

你能让我知道这段代码有什么问题吗?


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

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)

$ColName = @{Expression={$_.CSNAME};Label="SERVER NAME"},
           @{Expression={$_.Caption};Label="OS NAME"; width =25},
           @{Expression={$_.OSArchitecture};Label="OS TYPE"}
$out = Get-WmiObject Win32_OperatingSystem -ComputerName "suman-pc" |
       Format-Table -HideTableHeaders $ColName

list = New-Object System.Collections.ArrayList
$list.Insert($out)

$dataGridView = New-Object System.Windows.Forms.DataGridView -Property @{
  Size=New-Object System.Drawing.Size(800,400)
  ColumnHeadersVisible = $true
  DataSource = $list
}

$form.Controls.Add($dataGridView)
$form.ShowDialog()

上面的代码正在产生下面的错误。我的目标是收集多个系统OS名称,并以数据网格格式呈现

New-List : Cannot bind parameter 'MarkerOffset'. Cannot convert value
"System.Collections.ArrayList" to type "System.Double". Error: "Input string
was not in a correct format."
At C:\Users\Suman\AppData\Local\Temp\4abe9ca5-3580-4c58-918b-9f1f721c1f32.ps1:10 char:19
+ list = New-Object System.Collections.ArrayList
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-List], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,AutoGenerateCmdlets669798327.NewListCommand

Cannot find an overload for "Insert" and the argument count: "1".
At :\Users\Suman\AppData\Local\Temp\4abe9ca5-3580-4c58-918b-9f1f721c1f32.ps1:11 char:1
+ $list.Insert($out)
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest

共1个答案

匿名用户

我在这里找到了几个原因。

首先,您不想使用Format-Table,因为它仅作为代表性输出。

第二,你使用了错误的方法,你应该使用$list. Add()而不是$list.插入()

这是固定代码:

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

$form = New-Object System.Windows.Forms.Form
$form.Size = New-Object System.Drawing.Size(900,600)

$ColName = @{Expression={$_.CSNAME};Label="SERVER NAME"},
           @{Expression={$_.Caption};Label="OS NAME";},
           @{Expression={$_.OSArchitecture};Label="OS TYPE"}
$out = Get-WmiObject Win32_OperatingSystem -ComputerName "suman-pc" | Select-Object $ColName

$list = New-Object System.Collections.ArrayList
[void]$list.Add($out)

$dataGridView = New-Object System.Windows.Forms.DataGridView -Property @{
  Size=New-Object System.Drawing.Size(800,400)
  ColumnHeadersVisible = $true
  DataSource = $list
}

$form.Controls.Add($dataGridView)
$form.ShowDialog()