Issue with Variable $event.SourceEventArgs.NewEvent.TargetInstance.State

Aug 31, 2011 at 5:35 AM

Guys, with your example I did this code  to change the State of the service in a listview using wmi events. But to not create a event to start and to stop, I am trying to use the variable $event.SourceEventArgs.NewEvent.TargetInstance.State. But for some reason does not works :

New-ListView -Name MonitorSQLService -ItemsSource {
   ## Please excuse the ridiculous PowerShell syntax for constructing generic collections with its casting
   New-Object System.Collections.ObjectModel.ObservableCollection[PSObject](,([PSObject[]](&{
      New-Object PSObject -Prop @{ Name="SQL Server";          State=(Get-Service "MSSQLSERVER").status}
      New-Object PSObject -Prop @{ Name="SQL Server Agent";    State=(Get-Service "SQLSERVERAGENT").status}
   })))
} -View {
   New-GridView -Columns {
      New-GridViewColumn -Header Name -DisplayMember { Binding Name }
      New-GridViewColumn -Header State -DisplayMember { Binding State }

   }
} -AsJob
$query = "select * from __InstanceModificationEvent within 5 where TargetInstance ISA 'Win32_Service' and TargetInstance.Name='MSSQLSERVER' and (TargetInstance.State='Stopped' or TargetInstance.State='Running')"

Register-WMIEvent -Query $query  -Action {
	Get-Job MonitorSQLService | Update-WPFJob {
		$Window.Content.ItemsSource | Where { $_.Name -eq "SQL Server" } | % {
			$_.State =$event.SourceEventArgs.NewEvent.TargetInstance.State
		} -
	
	    [System.Windows.Data.CollectionViewSource]::GetDefaultView( $Window.Content.ItemsSource ).Refresh();
	}
	
	Get-Job MonitorSQLService | Update-WPFJob {
		foreach($item in $Window.Content.ItemsSource) { 
		# Don't trust anyone over 35
			if($item.State -eq "Stopped") {
				$ViewItem = $Window.Content.ItemContainerGenerator.ContainerFromItem( $item )
				$ViewItem.Background = "Pink"
			} else {
				$ViewItem = $Window.Content.ItemContainerGenerator.ContainerFromItem( $item )
				$ViewItem.Background = "white"
			}
		  
	   }
	}

}

if I use this code, the variable is displayed but the $_.state does not show the value, then I believe it is something with  Get-Job MonitorSQLService | Update-WPFJob. (I dont know)

Register-WMIEvent -Query $query  -Action {
    $State = $event.SourceEventArgs.NewEvent.TargetInstance.State ;
	Write-Host $state
	Get-Job MonitorSQLService | Update-WPFJob {
		$Window.Content.ItemsSource | Where { $_.Name -eq "SQL Server" } | % {
			$_.State =$($State)
		} 
	
	    [System.Windows.Data.CollectionViewSource]::GetDefaultView( $Window.Content.ItemsSource ).Refresh();
	}
	
	Get-Job MonitorSQLService | Update-WPFJob {
		foreach($item in $Window.Content.ItemsSource) { 
		# Don't trust anyone over 35
			if($item.State -eq "Stopped") {
				$ViewItem = $Window.Content.ItemContainerGenerator.ContainerFromItem( $item )
				$ViewItem.Background = "Pink"
			} else {
				$ViewItem = $Window.Content.ItemContainerGenerator.ContainerFromItem( $item )
				$ViewItem.Background = "white"
			}
		  
	   }
	}

}

 I did a workaround using 2 events, but can you help me with this issue ?

 Monitoring SQL Server Service with WMI Events and ShowUI

Big Thanks :)