1
[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
4
  • Not exactly an answer, but have you considered Out-GridView?
    – Joey
    Apr 14, 2016 at 6:00
  • 3
    You miss $ before variable name. Apr 14, 2016 at 6:10
  • 1
    list = New-Object System.Collections.ArrayList should be $list = ... Also, insert requires to specify an index. Apr 14, 2016 at 8:40
  • 1
    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. Apr 14, 2016 at 9:06

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.