Adding a click event to AWSA tool

by colt.45 at 2013-04-05 10:41:21

I am trying to add a button to collect system events. I have the button in place but am getting errors when running it:

New-Object : Multiple ambiguous overloads found for "ListViewItem" and the
argument count: "1".
At C:\Users\archec\Desktop\AWSA\AWSA.ps1:631 char:12
+ $Item = New-Object System.Windows.Forms.ListViewItem($.$Col0)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:slight_smile: [New-Object], MethodExcepti
on
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerS
hell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At C:\Users\archec\Desktop\AWSA\AWSA.ps1:632 char:79
+ ForEach ($Col in ($lvMain.Columns | ?{$
.Index -ne 0})){$Field =
$Col.Text;$I …
+

+ CategoryInfo : InvalidOperation: (:slight_smile: [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\archec\Desktop\AWSA\AWSA.ps1:632 char:79
+ ForEach ($Col in ($lvMain.Columns | ?{$_.Index -ne 0})){$Field =
$Col.Text;$I …
+

+ CategoryInfo : InvalidOperation: (:slight_smile: , RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At C:\Users\archec\Desktop\AWSA\AWSA.ps1:632 char:79
+ ForEach ($Col in ($lvMain.Columns | ?{$.Index -ne 0})){$Field =
$Col.Text;$I …
+
~~
+ CategoryInfo : InvalidOperation: (:slight_smile: [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling "Add" with "1" argument(s): "Object reference not set to an
instance of an object."
At C:\Users\archec\Desktop\AWSA\AWSA.ps1:633 char:4
+ $lvMain.Items.Add($Item)
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:slight_smile: [], MethodInvocationException
+ FullyQualifiedErrorId : NullReferenceException

I am fairly new to PoSh so if this is a simple newb fix feel free to make fun of me if you feel it is necessary. I did add properties for SystemEvents to the xml file as well. Here is the code for the click event:

$btnSysEvents_Click={
Get-ComputerName
Initialize-Listview
$SBPStatus.Text = "Retrieving System Events…"
Update-ContextMenu (Get-Variable cmsSysEvent*)
$XML.Options.SystemEvents.Property | %{Add-Column $
}
Resize-Columns
$Col0 = $lvMain.Columns[0].Text
$Info = Get-EventLog System -ComputerName $ComputerName -Newest 25 -EntryType Error,Warning | Sort TimeGenerated
Start-Sleep -m 250
if($SysError){$SBPStatus.Text = "[$ComputerName] $SysError"}
else{
$Info | %{
$Item = New-Object System.Windows.Forms.ListViewItem($.$Col0)
ForEach ($Col in ($lvMain.Columns | ?{$
.Index -ne 0})){$Field = $Col.Text;$Item.SubItems.Add($.$Field)}
$lvMain.Items.Add($Item)
}
$SBPStatus.Text = "Ready"
}

Thanks for any help you can give…
by colt.45 at 2013-04-05 12:17:06
I just tried it this way too and am still getting the same errors:

$btnSysEvents_Click={
Get-ComputerName
Initialize-Listview
$SBPStatus.Text = "Retrieving System Events..."
Update-ContextMenu (Get-Variable cmsSysEvent*)
$XML.Options.SystemEvents.Property | %{Add-Column $
}
Resize-Columns
$Col0 = $lvMain.Columns[0].Text
$Info = @()
$SystemEvents = Get-EventLog -ComputerName $computername -LogName System -EntryType Error,Warning -Newest 10
foreach ($event in $SystemEvents) {
$Member = New-Object PSObject -Property @{
TimeGenerated = $event.TimeGenerated
EntryType = $event.EntryType
Source = $event.Source
EventID = $event.EventID
}
$Info += $Member
}
Start-Sleep -m 250
if($SysError){$SBPStatus.Text = "[$ComputerName] $SysError"}
else{
$Info | %{
$Item = New-Object System.Windows.Forms.ListViewItem($.$Col0)
ForEach ($Col in ($lvMain.Columns | ?{$
.Index -ne 0})){$Field = $Col.Text;$Item.SubItems.Add($.$Field)}
$lvMain.Items.Add($Item)
}
$SBPStatus.Text = "Ready"
}
by nohandle at 2013-04-06 01:34:09
Multiple ambiguous overloads found for "ListViewItem" and the
argument count: "1".

There are more than one constructors for the type that accept one parameter and the runtime does not know which one you are trying to use -> try being more specific about the type of input data by explicitly casting them.
$Item = New-Object System.Windows.Forms.ListViewItem($
.$Col0)

The errors after that are probably just result of the object not being created (null and null reference errors).

Your code snippet is missing last bracket BTW:
$btnSysEvents_Click={
Get-ComputerName
Initialize-Listview
$SBPStatus.Text = "Retrieving System Events..."
Update-ContextMenu (Get-Variable cmsSysEvent*)
$XML.Options.SystemEvents.Property | %{Add-Column $}
Resize-Columns
$Col0 = $lvMain.Columns[0].Text
$Info = @()
$SystemEvents = Get-EventLog -ComputerName $computername -LogName System -EntryType Error,Warning -Newest 10

foreach ($event in $SystemEvents) {
$Member = New-Object PSObject -Property @{
TimeGenerated = $event.TimeGenerated
EntryType = $event.EntryType
Source = $event.Source
EventID = $event.EventID
}
$Info += $Member
}

Start-Sleep -m 250
if ($SysError)
{
$SBPStatus.Text = "[$ComputerName] $SysError"
}
else
{
$Info | %{
$Item = New-Object System.Windows.Forms.ListViewItem($
.$Col0)
ForEach ($Col in ($lvMain.Columns | ?{$.Index -ne 0}))
{
$Field = $Col.Text;$Item.SubItems.Add($
.$Field)
}
$lvMain.Items.Add($Item)
}
$SBPStatus.Text = "Ready"
}
}
by colt.45 at 2013-04-08 10:51:30
Thank you! After casting each as a String it worked fine. $btnSysEvents_Click={
Get-ComputerName
Initialize-Listview
$SBPStatus.Text = "Retrieving System Events..."
Update-ContextMenu (Get-Variable cmsSysEvent*)
$XML.Options.SystemEvents.Property | %{Add-Column $}
Resize-Columns
$Col0 = $lvMain.Columns[0].Text
$Info = @()
$SystemEvents = Get-EventLog -ComputerName $computername -LogName System -EntryType Error,Warning -Newest 10
foreach ($event in $SystemEvents) {
$Member = New-Object PSObject -Property @{
TimeGenerated = [String]$event.TimeGenerated
EntryType = [String]$event.EntryType
Source = [String]$event.Source
EventID = [String]$event.EventID
}
$Info += $Member
}
Start-Sleep -m 250
if($SysError){$SBPStatus.Text = "[$ComputerName] $SysError"}
else{
$Info | %{
$Item = New-Object System.Windows.Forms.ListViewItem($
.$Col0)
ForEach ($Col in ($lvMain.Columns | ?{$.Index -ne 0})){$Field = $Col.Text;$Item.SubItems.Add($.$Field)}
$lvMain.Items.Add($Item)
}
$SBPStatus.Text = "Ready"
}
}


TimeGenerated = [String]$event.TimeGenerated
EntryType = [String]$event.EntryType
Source = [String]$event.Source
EventID = [String]$event.EventID