1
votes
[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()

The above code is generating the below error. my objective is collect OS name for multiple system, and present it in datagrid format

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
Not exactly an answer, but have you considered Out-GridView?Joey
You miss $ before variable name.user4003407
list = New-Object System.Collections.ArrayList should be $list = ... Also, insert requires to specify an index.David Brabant
The code in your question produces a number of errors, but not the first one you posted. Please post your actual code, not something you fabricated or typed from memory. The second error you get, because Insert() expects 2 arguments, as documented. Use Add() if you just want to append an item.Ansgar Wiechers

1 Answers

1
votes

I have found a couple of reasons here.

First you dont want to use Format-Table since it goes only as representive output.

Second you using wrong method, you should using $list.Add() instead of $list.Insert()

Here's fixed code:

[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()